Saturday, August 1, 2009

Comet a.k.a. Reverse Ajax

OK, before we start with this post we need to explain what is Ajax to those "web developer" who have been living under a rock since the CGI-Perl scripts days.

The http protocol model includes the so called "Request-Response" cycle, so many years later each response included a full page reload, then came JavaScript and yada yada Ajax was born, so Ajax stands for Asynchronous JavaScript and XML. Ajax consist of updating certain parts of a web page asynchronously, always started by the client , without reloading the whole page so it doesn't interfere with the user experience, no awkward reloads. So, yeah Tim O'Reilly coined this with the awesome name Web 2.0, but you maybe asking what the hell is Comet?

Comet is a neologism to describe a process very similar to Ajax but in which the server starts the connection. Actually, Comet is an umbrella term for multiples techniques used to achieve this functionality, now you probably understand why some people decided to call Comet, Reverse Ajax. But why do we need Comet? why do we need something that turns the http cycle upside down? what benefits could we gain from using Comet?

This very same questions were asked by friend of mine one day when I told him what Comet was, so I came with a good example and decided to make a post about it.

The example includes making a chat online in your website, something like gtalk inside gmail, so how would you implement it? has to be real life chat with near instant responses or the users gonna complain a lot and probably never use it.

So one idea would be to use Ajax to achieve this:

So putting it simple, make a lot of Ajax calls to the server and when there is something in the queue, update the chat component in the client, does anyone else see the problem with this approach? Making calls every second will create unnecessary traffic and if we make our web app to make calls every 5 seconds what happens if a response from the other user comes right after a couple milliseconds after our call was made, the client will wait 5 seconds waiting for a response that's already on the server. Obviously we need a new approach to achieve our goal.

Second idea, use Comet:












So how about using Comet and make the server talk to us when he receives the response from the other user? We won't need all those Ajax calls, instead we would only need one call to keep the chat cycle going, the server's.

But Comet, can't be all roses, we need a way to implement it. In this example I'm not gonna code it but I'm gonna use the theory behind Ajax long polling to detect new information on the server. It consists in simply making Ajax calls wait longer than usual for a response from the server, in our example it uses a lot less Ajax calls to achieve our goal and it doesn't produce too much traffic.

Well, hope this post was of some interest to some of you, I hope you can leave your comments about where to use Comet and how much resources would that save.

This was Darryl Almonte, thanks for reading, Namaste.

No comments:

Post a Comment