Comet is aweb application model in which a long-heldHTTPS request allows aweb server topush data to abrowser, without the browser explicitly requesting it.[1][2]Comet is anumbrella term, encompassing multiple techniques for achieving this interaction. All these methods rely on features included by default in browsers, such asJavaScript, rather than on non-default plugins. The Comet approach differs from theoriginal model of the web, in which a browser requests a complete web page at a time.[3]
The use of Comet techniques inweb development predates the use of the wordComet as aneologism for the collective techniques. Comet is known by several other names, includingAjax Push,[4][5]Reverse Ajax,[6]Two-way-web,[7]HTTP Streaming,[7] andHTTP server push[8]among others.[9] The termComet is not an acronym, but was coined by Alex Russell in his 2006blog post.[10][citation needed]
In recent years[when?], the standardisation and widespread support ofWebSocket andServer-sent events has rendered the Comet model obsolete.
The ability to embedJava applets into browsers (starting withNetscape Navigator 2.0 in March 1996[11]) made two-way sustained communications possible, using a rawTCP socket[12] to communicate between the browser and the server. This socket can remain open as long as the browser is at the document hosting the applet. Event notifications can be sent in any format – text or binary – and decoded by the applet.
The very first application using browser-to-browser communications was Tango Interactive,[13][failed verification] implemented in 1996–98 at the Northeast Parallel Architectures Center (NPAC) atSyracuse University usingDARPA funding. TANGO architecture has been patented by Syracuse University.[14] TANGO framework has been extensively used as a distance education tool.[15] The framework has been commercialized byCollabWorx and used in a dozen or so Command&Control and Training applications in the United States Department of Defense[citation needed].
The first set of Comet implementations dates back to 2000,[16][unreliable source?] with thePushlets,Lightstreamer, and KnowNow projects.Pushlets, a framework created by Just van den Broecke, was one of the first[17] open source implementations. Pushlets were based on server-side Java servlets, and a client-side JavaScript library. Bang Networks – aSilicon Valley start-up backed byNetscape co-founderMarc Andreessen – had a lavishly-financed attempt to create a real-time push standard for the entire web.[18]
In April 2001, Chip Morningstar began developing a Java-based (J2SE) web server which used two HTTP sockets to keep open two communications channels between the custom HTTP server he designed and a client designed byDouglas Crockford; a functioning demo system existed as of June 2001.[citation needed] The server and client used a messaging format that the founders of State Software, Inc. assented to coin asJSON following Crockford's suggestion. The entire system, the client libraries, the messaging format known as JSON and the server, became the State Application Framework, parts of which were sold and used by Sun Microsystems, Amazon.com, EDS and Volkswagen.[citation needed]
In March 2006,software engineer Alex Russell coined the term Comet in a post on his personal blog.[19] The new term was a play onAjax (Ajax andComet both being common household cleaners in the USA).[20][21][22]
In 2006, some applications exposed those techniques to a wider audience:Meebo’s multi-protocol web-based chat application enabled users to connect toAOL,Yahoo, andMicrosoft chat platforms through the browser; Google added web-based chat toGmail;JotSpot, a startup since acquired by Google, built Comet-based real-time collaborative document editing.[23] New Comet variants were created, such as the Java-basedICEfacesJSF framework (although they prefer the term "Ajax Push"[5]). Others that had previously used Java-applet based transports switched instead to pure-JavaScript implementations.[24]
Comet applications attempt to eliminate the limitations of thepage-by-page web model and traditionalpolling by offering two-way sustained interaction, using a persistent or long-lasting HTTP connection between the server and the client. Since browsers and proxies are not designed with server events in mind, several techniques to achieve this have been developed, each with different benefits and drawbacks. The biggest hurdle is theHTTP 1.1 specification, which states "this specification... encourages clients to be conservative when opening multiple connections".[25] Therefore, holding one connection open for real-time events has a negative impact on browser usability: the browser may be blocked from sending a new request while waiting for the results of a previous request, e.g., a series of images. This can be worked around by creating a distincthostname for real-time information, which is an alias for the same physical server. This strategy is an application of domain sharding.
Specific methods of implementing Comet fall into two major categories: streaming andlong polling.
An application using streaming Comet opens a singlepersistent connection from theclient browser to the server for all Cometevents. These events are incrementally handled and interpreted on the client side every time the server sends a new event, with neither side closing the connection.[3]
Specific techniques for accomplishing streaming Comet include the following:
A basic technique for dynamic web application is to use a hiddeniframe HTML element (aninline frame, which allows a website to embed one HTML document inside another). This invisible iframe is sent as achunked block, which implicitly declares it as infinitely long (sometimes called "forever frame"). As events occur, the iframe is gradually filled withscript
tags, containing JavaScript to be executed in the browser. Because browsers render HTML pages incrementally, eachscript
tag is executed as it is received. Some browsers require a specific minimum document size before parsing and execution is started, which can be obtained by initially sending 1–2 kB of padding spaces.[26]
One benefit of the iframes method is that it works in every common browser. Two downsides of this technique are the lack of a reliable error handling method, and the impossibility of tracking the state of the request calling process.[26]
TheXMLHttpRequest (XHR) object, a tool used by Ajax applications for browser–server communication, can also be pressed into service for server–browser Comet messaging by generating a custom data format for an XHR response, and parsing out each event using browser-side JavaScript; relying only on the browser firing theonreadystatechange
callback each time it receives new data.
![]() | This articlepossibly containsoriginal research. Pleaseimprove it byverifying the claims made and addinginline citations. Statements consisting only of original research should be removed.(December 2017) (Learn how and when to remove this message) |
None of the above streaming transports work across all modern browsers without negative side-effects. This forces Comet developers to implement several complex streaming transports, switching between them depending on the browser. Consequently, many Comet applications use long polling, which is easier to implement on the browser side, and works, at minimum, in every browser that supports XHR. As the name suggests, long polling requires the client to poll the server for an event (or set of events). The browser makes an Ajax-style request to the server, which is kept open until the server has new data to send to the browser, which is sent to the browser in a complete response. The browser initiates a new long polling request in order to obtain subsequent events.IETF RFC 6202 "Known Issues and Best Practices for the Use of Long Polling and Streaming in Bidirectional HTTP" compares long polling and HTTP streaming.Specific technologies for accomplishing long-polling include the following:
For the most part,XMLHttpRequest long polling works like any standard use of XHR. The browser makes an asynchronous request of the server, which may wait for data to be available before responding. The response can contain encoded data (typicallyXML orJSON) or Javascript to be executed by the client. At the end of the processing of the response, the browser creates and sends another XHR, to await the next event. Thus the browser always keeps a request outstanding with the server, to be answered as each event occurs.
While any Comet transport can be made to work acrosssubdomains, none of the above transports can be used across differentsecond-level domains (SLDs), due to browser security policies designed to preventcross-site scripting attacks.[27] That is, if the main web page is served from one SLD, and the Comet server is located at another SLD (which does not havecross-origin resource sharing enabled), Comet events cannot be used to modify the HTML and DOM of the main page, using those transports. This problem can be sidestepped by creating aproxy server in front of one or both sources, making them appear to originate from the same domain. However, this is often undesirable for complexity or performance reasons.
Unlike iframes or XMLHttpRequest objects,script
tags can be pointed at anyURI, and JavaScript code in the response will be executed in the current HTML document. This creates a potential security risk for both servers involved, though the risk to the data provider (in our case, the Comet server) can be avoided usingJSONP.
A long-polling Comet transport can be created by dynamically creatingscript
elements, and setting their source to the location of the Comet server, which then sends back JavaScript (or JSONP) with some event as its payload. Each time the script request is completed, the browser opens a new one, just as in the XHR long polling case. This method has the advantage of being cross-browser while still allowing cross-domain implementations.[27]
Browser-native technologies are inherent in the term Comet. Attempts to improve non-polling HTTP communication have come from multiple sides:
EventSource
and a new MIME typetext/event-stream
.onmessage
callback.[29]{{cite speech}}
: CS1 maint: location (link){{cite web}}
: CS1 maint: bot: original URL status unknown (link)This API has been deprecated.
Comet Daily provides information about Comet techniques.*