What is Comet (Programming)?

Comet is a web application design paradigm that describes a continuous, two-way interaction between a server and a web browser using native HTTP methods. It was conceived in order to provide a conceptual model for designing responsive and highly interactive web UIs without having to resort to browser plugins like Java applets.

The Comet approach turns the typical HTTP client-server model upside-down to enable a kind of reverse Ajax functionality, wherein the web server initiates a persistent HTTP connection with a client browser and actively pushes out data instead of waiting to serve responses. In other words, Comet represents a “Push-Style” or “Server-Push” mechanism in contrast with HTTP’s “Request/Response” or “Get/Pull” mechanism.

Comet Techniques

A number of HTTP techniques exist to achieve this event-driven interaction, including Ajax Push, HTTP Streaming, and HTTP Server Push. These techniques mainly rely on JavaScript to handle streamed events on the client side, while the messaging format is often in JSON or XML.

A more modern example is WebSocket, a sophisticated transport protocol that borrows from the principles of Comet yet which offers a cleaner and more standardized realization of the idea. However, WebSocket stands on its own as a distinct protocol and is not generally considered to be a form of Comet.

Comet Model

The Comet model is often used for web apps that involve multi-user collaboration where latency must be kept as low as possible for a shared realtime experience. Traditionally, it has been used to provide support for collaborative document editing and multi-protocol chat.

To apply Comet in a web application, two broad methods are available:

  • Streaming: Events are pushed from server to client over a single persistent connection, and are usually processed inside a hidden iframe on the page or via XMLHttpRequest.
  • Long Polling: The browser polls the server for new events with a persistent request that is held open until it gets a response. When new data is available, the server sends the response and a new long polling request is made by the browser for further events. Rinse and repeat for sustained interaction.

Related

Try PubNub Today