2

I've been looking around for options to automatically update my page with new data from the server. My data can change from every 10 minutes to possibly 1 hour, no way to predict this. Whenever this happens, the change must appear pretty quickly on the screen (no longer than 15 seconds).

So one option is just polling and sending a request every 15 seconds, but then sending requests to the server and getting empty results for over 30 minutes does sound a bit stupid.

But if I use long-polling, is it acceptable to keep a connection open for such long periods? What if there's no new data for nearly an hour? Also if I go with long-polling, I'll need to set some timeout, wouldn't I? But then it won't make sense to end the connection knowing that there might be new data on the server, and then re-initiating the connection. That'll be regular polling with longer intervals, wouldn't it?

Would appreciate if somebody could guide me to the best solution for my problem. Thanks in advance!

thomas
  • 1,133
  • 1
  • 12
  • 31
  • 1
    Looks like you want a [Server-sent event](https://developer.mozilla.org/en-US/docs/Server-sent_events/Using_server-sent_events) –  Apr 06 '14 at 08:07

1 Answers1

1

If you have to support older browsers, polling is your only option.

If that is the case, the HEAD method is pretty lightweight and can just return a status indicating that things have changed.

If you can support newer browsers, you can use the method @MikeW mentioned above, or you could look at WebSockets

I found this discussion which goes into much more detail about polling.

Community
  • 1
  • 1
Jeremy J Starcher
  • 23,369
  • 6
  • 54
  • 74
  • Then the old browser thing might be a problem... If I do end up polling every 15 seconds, most of the time getting empty results, how bad is that? Is it just inefficient, or is it simply wrong and "bad" programming? – thomas Apr 06 '14 at 08:30
  • There is a reason why sites like Gmail offers (offered?) a 'light' experience for older browsers -- but these are business decisions that you'll have to make. – Jeremy J Starcher Apr 06 '14 at 08:32
  • With all honesty - I don't know how to even estimate if these constant requests are bad, and carry a cost I can't handle... If I tell you the site is constantly open on around 20 browsers in our offices, sending requests every 15-20 seconds, is that really bad? It sounds like it's a lot, but I don't really know. – thomas Apr 06 '14 at 08:37