dagblog.
← Archive

Here come the comets


If you thought AJAX was the technological Mount Everest of what browsers can do, development is still moving. Next up is Comet, a technology built on AJAX that squeezes the server to its silicon core. Comet, also called reverse AJAX or server push, probably won't be as revolutionary as AJAX, but it will find a place and bring its own demands on web servers.

The main difference between AJAX and Comet is how they manage HTTP connections between client and server. With AJAX, each asynchronous request means another call to the server. Comet uses an earlier HTTP connection that stays open. That lets the server push data to the client whenever it becomes available. The following diagram shows the difference.

Archive image in Here come the comets, figure 1
Image could not be loaded. Archive image in Here come the comets, figure 1
↗ Medium

Take a chat application. With AJAX, the client calls the server for new messages at regular intervals. With Comet, the HTTP connection to each client stays open, and whenever the server gets a new message it can send it to listening clients.

Comet isn't Columbus sailing into America. It builds on technology and techniques already available. Like AJAX, Comet is a concept that can be implemented in different ways. Alex Russell coined the term Comet in his article Comet: Low Latency Data for the Browser. The three basic parts of the concept are:

  • a Comet communication protocol
  • a JavaScript library that implements the protocol
  • a server that implements the protocol

The protocol defines the communication format between client and server, the events that can occur during their interaction, and the format of individual messages or events. DWR and ActiveMQ currently have proprietary implementations of a Comet protocol. The Bayeux protocol specification is an attempt to standardize it.

Bayeux and any other Comet protocol are essentially about messaging. If you know JMS, it's the familiar publisher/subscriber model. The server endpoint represents a topic that the client subscribes to by connecting to it.

A Comet protocol specification matters for interoperability and especially for the client, the browser. Browsers generally allow only two connections to a server. Bayeux was therefore designed for use across libraries. It is part of a complete Comet implementation called Cometd, which includes the protocol, a server implementation and a client in the Dojo toolkit.

Comet is a useful extension for building applications more efficiently when they used to contact the server at regular intervals. Typical examples include instant messaging applications. Comet can also help address situations such as users modifying the same data concurrently.

For Java, Comet adds another dimension. The server implementation must be asynchronous, with the request and response decoupled. Making the endpoint asynchronous is easy enough. The trouble is the Servlet API, where request and response aren't separate. As a result, every servlet container handles an asynchronous Servlet API on its own. Tomcat 6.0 has its Advance IO, Grizzly (GlassFish's HTTP connector) has Asynchronous Request Processing (ARP), and WebLogic has …

Further reading on Comet

Medium version

Related articles

Related CZ Podcast episodes