• Smart JS Polling

    defunkt 29 Jul 2009

    While Comet may be all the rage, some of us are still stuck in web 2.0. And those of us that are use Ajax polling to see if there’s anything new on the server.

    Here at GitHub we normally do this with memcached. The web browser polls a URL which checks a memcached key. If there’s no data cached, the request returns and polls again in a few seconds. If there is data, the request returns with it and the browser merrily goes about its business. On the other end our background workers stick the goods in memcached when they’re ready.

    In this way we use memcached as a poor man’s message bus.

    Yet there’s a problem with this: if after a few Ajax polls there’s no data, there probably won’t be for a while. Maybe the site is overloaded or the queue is backed up. In those circumstances the continued polling adds additional unwanted strain to the site. What to do?

    The solution is to increment the amount of time you wait in between each poll. Really, it’s that simple. We wrote a little jQuery plugin to make this pattern even easier in our own JS. Here it is, from us to you:

    Any time you see “Loading commit data…” or “Hardcore Archiving Action,” you’re seeing smart polling. Enjoy!

  • Comments

    catgofire Wed Jul 29 20:03:23 -0700 2009

    I did this for a project a while back, but used the Fibonacci sequence instead of a *1.5 curve. You know ... just because.

    mlangenberg Wed Jul 29 22:39:01 -0700 2009

    I've heard that exponential backoff works much better with web 3.0

    adlaiff6 Thu Jul 30 04:02:11 -0700 2009

    linear increase, exponential backoff

    davidwparker Thu Jul 30 06:13:53 -0700 2009

    I dig it.

    cannikin Thu Jul 30 09:59:19 -0700 2009

    Yeah, PeriodicalUpdater does this for Prototype.

    ramses0 Thu Jul 30 12:34:12 -0700 2009

    I called them stepped intervals back in 2005.

    http://www.robertames.com/blog.cgi/entries/js-stepped-intervals.html

    Code is old-ish and depends on a global variable, feel free to steal or clean it up under the BSD license of your choice.

    I preferred being able to explicitly set timeouts so it's easier to explain and reason about:
    “20 seconds for 6 times (2 minutes)”, then “60 seconds for 5 times (5 minutes)”, then “2 minutes for 30 times (1 hour)”.

    --Robert

    visionmedia Fri Aug 28 16:34:48 -0700 2009
    cmelbye Sun Sep 13 20:18:35 -0700 2009

    While this is great for good ol' regular AJAX, it could also be used with a few modifications for reconnecting to your long-polling Comet server when it goes down. ("Oops, looks like you lost your connection. Reconnecting in 5 seconds..." and so on)

    chicagogrooves Wed Oct 28 02:17:02 -0700 2009

    Wow - this poller addresses several things I want - which prototype and scriptaculous lack - (you'd think they'd never heard of etags or if-modified-since !!) Look forward to playing with it..

    Please log in to comment.