In web applications, traditionally web pages are delivered to the client only when the client requests them. For every client request, the browser initiates an HTTP connection to the web server, which then returns the data and the connection is closed. The drawback of this approach is that the page displayed is updated only when the user explicitly refreshes the page or moves to a new page. Since transferring entire pages takes a long time, refreshing pages introduces a long latency.
To solve this problem, Ajax can be used which allows the web browser to request only that part of the web page that has changed and update that portion accordingly. Since the overall data transferred is reduced, latency is also reduced and overall responsiveness of the web site hosting the application increases. Further, by using asynchronous background data transfer, where the user works with partly received data as the rest of the data is being retrieved, the responsiveness of the web application can be further increased.
But this practice also suffers from the problem that the client has to request some data before it will be sent by the server. This problem becomes a major hurdle when designing applications which have to wait for some event to occur at the server side, such as some other user sending some data to the server, before it can proceed, but has no information when the event will occur.
A solution would be to design the application such that it will intermittently poll the server to find out if the event has occurred. But this is not an elegant solution as the application will waste a lot of time querying for the completion of the event, thereby directly impacting the responsiveness of the application. In addition, a lot of network bandwidth will be wasted.
Comet gives the better solution for this. It allows the server to send a message to the client when the event occurs, without the client having to ask for it. Such a client will not have to check with the server periodically, rather it can continue with other work on the data generated by the event when it has been pushed by the server.
What is Comet?
- Comet is an advanced programming technique that enables web servers to send data to the client without having any need for the client to request it. It allows creation of event-driven web applications which are hosted in the browser.
- Comet is a new term but old technology; they use long-lived HTTP connections to reduce the latency with which messages are passed to the server.
- In essence, they do not poll the server occasionally; Instead the server has an open line of communication with which it can push data to the client
How Comet differs from Ajax
Comet applications can deliver data to the client at any time, not only in response to user input. The data is delivered over a single, previously-opened connection. This approach reduces the latency for data delivery significantly.
The architecture relies on a view of data which is event driven on both sides of the HTTP connection. Unlike Ajax, applications that implement the Comet style can communicate state changes with almost negligible latency. This makes it suitable for many types of monitoring and multi-user collaboration applications which would otherwise be difficult or impossible to handle in a browser without plugins.In Comet applications, servers push data in addition to client requesting it.
Similarities between Comet and Ajax
- No plugins
- Plain old HTTP
- Broad browser support
Why Is Comet Better For Users?
Regular Ajax improves the responsiveness of a UI for a single user, but at the cost of allowing the context to go “stale” for long-lived pages. Changes to data from others users is lost until a user refreshes the whole page. An application can alternately return to the “bad old days” and maintain some sort of state mechanism by which it tells client about changes since the last time they’ve communicated. The user has to either wait until they perform some action which would kick off a request to see the updated state from other users (which might impact the action they wanted to perform!) or request changes from the server at some interval (called “polling”).
Since the web is inherently multi-user, it’s pretty obvious that regular Ajax imposes usability and transparency hurdles for users. Applications that employ the Comet technique can avoid this problem by pushing updates to all clients as they happen. UI state does not go out of sync and everyone using an application can easily understand what their changes will mean for other users. Ajax improves single-user responsiveness.
Comet improves application responsiveness for collaborative, multi-user applications and does it without the performance issues associated with intermittent polling.
Applications implementing comet
Below mentioned are few familiar applications implementing comet.
- Jot Live