Understanding the Push – Forever-frame/polling/comet

Understanding the Push for Web

You must have heard of Push technology being used in the tech industry since a long time. And with the web being a trend, and an enourmous amount of userbase accessing websites, how do you keep the data realtime without effecting much of your network and database resource.
Push technology is one of the solutions in such cases. Push technology is not really a form of any specific tech, but a concept.
Example: Facebook live notifications or Gmail incoming mail notifications
You must have thought sometime of how do you get the notification ticks so quickly as soon as your friend pokes or likes your post.
One solution is, firing a “check my notifications” every few second of interval. That might just work good in your application, if it’s small enough or you have good cash for the server/network headaches.
The other better solution here uses comets / forever frame / polling.

Well, the forever frame technique is quite old, but just what you might need in your next app.
A common method of doing such notifications is to poll a script on the server (using ajax) on a given interval (perhaps every few seconds), to check if something has happened. However, this can be pretty network intensive, and you often make pointless requests, because nothing has happened

A common method of doing such notifications is to poll a script on the server (using ajax) on a given interval (perhaps every few seconds), to check if something has happened. However, this can be pretty network intensive, and you often make pointless requests, because nothing has happened.

The way facebook does it is using the comet approach, rather than polling on an interval, as soon as one poll completes, it issues another one. However, each request to the script on the server has an extremely long timeout, and the server only responds to the request once something has happened. You can see this happening if you bring up Firebug’s Console tab while on facebook, with requests to a script possibly taking minutes. It is quite ingenious really, since this method cuts down immediately on both the number of requests, and how often you have to send them. You effectively now have an event framework that allows the server to ‘fire’ events.

Behind this, in terms of the actual content returned from those polls, it’s a JSON response, with what appears to be a list of events, and info about them. It’s minified though, so is a bit hard to read.

In terms of the actual technology, ajax is the way to go here, because you can control request timeouts, and many other things. I’d reccommend (Stack overflow cliche here) using jQuery to do the ajax, it’ll take a lot of the cross-compability problems away. In terms of php, you could simply poll an event log database table in your php script, and only return to the client when something happens? There are, I expect, many ways of implementing this.

Implementing:

Server Side:

There appear to be a few implementations of comet libraries in php, but to be honest, it really is very simple, something perhaps like the following pseudocode:

while(!has_event_happened()) {
   sleep(5);
}

echo json_encode(get_events());
  • The has_event_happened function would just check if anything had happened in an events table or something, and then the get_events function would return a list of the new rows in the table? Depends on the context of the problem really.
  • Don’t forget to change your php max execution time, otherwise it will timeout early!

Client Side:

Take a look at the jQuery plugin for doing Comet interaction:

That said, the plugin seems to add a fair bit of complexity, it really is very simple on the client, perhaps (with jquery) something like:

function doPoll() {
   $.get("events.php", {}, function(result) {
      $.each(result.events, function(event) { //iterate over the events
          //do something with your event
      });
      doPoll();
      //this effectively causes the poll to run again as
      //soon as the response comes back
   }, 'json');
}

$(document).ready(function() {
    $.ajaxSetup({
       timeout: 1000*60//set a global ajax timeout of a minute
    });
    doPoll(); // do the first poll
});

The whole thing depends a lot on how your existing architecture is put together.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s