views:

635

answers:

2

Hello,

I've been looking for a simpler way than Comet or Long-Polling to push some very basic ajax updates to the browser.

In my research, I've seen that people do in fact use Javascript timers to send Ajax calls at set intervals. Is this a bad approach? It almost seems too easy. Also consider that the updates I'll be sending are not critical data, but they will be monitoring a process that may run for several hours.

As an example - Is it reliable to use this design to send an ajax call every 10 seconds for 3 hours?

Thanks, Brian

+3  A: 

Generally, using timers to update content on a page via Ajax is at least as robust as relying on a long-lived stream connection like Comet. Firewalls, short DHCP leases, etc., can all interrupt a persistent connection, but polling will re-establish a client connection on each request.

The trade-off is that polling often requires more resources on the server. Even a handful of clients polling for updates every 10 seconds can put a lot more load on your server than normal interactive users, who are more likely to load new pages only every few minutes, and will spend less time doing so before moving to another site. As one data point, a simple Sinatra/Ajax toy application I wrote last year had 3-5 unique visitors per day to the normal "text" pages, but its Ajax callback URL quickly became the most-requested portion of any site on the server, including several sites with an order of magnitude (or more) higher traffic.

One way to minimize load due to polling is to separate the Ajax callback server code from the general site code, if at all possible, and run it in its own application server process. That "service middleware" service can handle polling callbacks, rather than giving up a server thread/Apache listener/etc. for what effectively amounts to a question of "are we there yet?"

Of course, if you only expect to have a small number (say, under 10) users using the poll service at a time, go ahead and start out running it in the same server process.

rcoder
Thanks. I definitely need to plan for more than 10 users. Any recommendations where to start learning how to set up a new application server process?
Brian
It's just deploying another instance of your application stack. If you're running PHP on Apache, for instance, have a separate docroot for a virtual host that only serves Ajax requests, and doesn't load the rest of your application code. For Java apps, have a standalone servlet to handle just the callbacks. Etc., etc. Minimize the footprint of your Ajax handler, and you can stretch your server resources a lot further.
rcoder
Oh I gotcha - thanks very much, very helpful.
Brian
A: 

I think that one thing that might be useful here is that polling at an unchanging interval is simple, but is often unnecessary or undesirable.

One method that I've been experimenting with lately is having positive and negative feedback on the poll. Essentially, an update is either active (changes happened) or passive (no newer changes were available, so none were needed). Updates that are passive increase the polling interval. Updates that are active set the polling interval back to the baseline value.

So for example, on this chat that I'm working on, different users post messages. The polling interval starts off at the high value of 5 seconds. If other site users are chatting, you get updated every 5 secs about it. If activity slows down, and no-one is chatting since the latest message was displayed, the polling interval gets slower and slower by about a second each time, eventually capping at once every 3 minutes. If, an hour later, someone sends a chat message again, the polling interval suddenly drops back to 5 second updates and starts slowing.

High activity -> frequent polling. Low activity -> eventually very infrequent polling.

Tchalvak