Search code examples
cometcometd

What is Comet for Real time systems?


Is Comet an Architecture style and Websockets/HTTP Streaming/SSE/BOSH/Bayeux are different implementations of this architecture style ?


Solution

  • The term "Comet" was born as a joke on the term "Ajax".

    AJAX (as Asynchronous JavaScript and XML) is a technique to perform HTTP requests and handle HTTP responses from the browser's JavaScript engine. At the time, "Ajax" was also a cleaning product.

    When techniques to perform low-latency delivery to browsers of server-side events started to be explored by developers using AJAX, the term "Comet" was coined because at the time "Comet" was another cleaning product supposedly better than the "Ajax" cleaning product, and "Comet" techniques were better than "Ajax" techniques.

    "Comet" is a series of HTTP techniques for the web used to notify clients (typically browsers) of server-side events with very small latencies (far less than a second).

    "Long Polling" is one such technique, probably the most popular and reliable. Other such techniques include the "hidden iframe", "script injection" (aka JSONP) and "infinite response" (aka "streaming") techniques (for example using HTTP/1.1's Transfer-Encoding: chunked mechanism).

    Now, Server Sent Events (SSE, aka EventSource) is a streaming technique that has been standardized, although recently has not seen much development. Rather than using the Transfer-Encoding: chunked mechanism, SSE uses Content-Type: text/event-stream that browsers implement, and a simple protocol on top of the HTTP transport mechanism. This means that the content that comes down from the server has a format - defined by this simple protocol - that needs to be parsed by the browser to be converted into events passed to the JavaScript engine, and applications have an API to be able to listen to these JavaScript events.

    BOSH (defined by XEP-0124) is a variant of the long polling technique, especially used in XMPP, and as such it is too a "Comet" technique.

    Bayeux is a transport-agnostic protocol that define channel and message semantic on top of a transport protocol such as HTTP or WebSocket. The CometD project uses Bayeux on top of HTTP and WebSocket to provide peer-to-peer, publish-subscribe and remote-call functionalities to applications. Other projects use the Bayeux protocol on top of either HTTP or WebSocket (or both).

    Finally, WebSocket is a transport protocol that has built-in bidirectional capabilities, something that HTTP does not have. However, it does not define the semantic of the payload it transports nor has metadata that describes it, so it is usually used as a transport protocol for other protocols such as Bayeux. WebSocket is not a "Comet" technique because it is not based on HTTP.

    It really depends on what you mean with the word "Architecture".

    I would say that "Comet" is a group of techniques to achieve low-latency events using HTTP.

    Long polling, HTTP chunked streaming, SSE, BOSH are some of those "Comet" techniques.

    Bayeux is a transport-agnostic protocol based on JSON used for messaging.

    WebSocket is a bidirectional transport protocol that is used in conjunction with other, typically transport agnostic, protocols like Bayeux. Its support in browsers make it a replacement for "Comet" techniques.