Comet is a web application architecture in which a long-held HTTP request allows a web server to push data to a browser, without the browser explicitly requesting it.
In Ajax the client pulls data from the server. With Comet the server pushes data to the client.
Other names for Comet are
Server Sent Events defines a simple Comet API in the form of a EventSource object. Server-Sent Events (SSE) is a standard describing how servers can initiate data transmission towards clients once an initial client connection has been established.
The Server-Sent Events draft standard defines an EventSource object that makes Comet applications38.1 trivial to write.
Simply pass a URL to
the EventSource()
constructor and then listen for message events on the
returned object:
var ticker = new EventSource("/stockprices"); ticker.onmessage = function(e) { var type = e.type; var data = e.data; // Now process the event type and event data strings. }
data
property
that holds whatever string the server sent as the payload for this
event.
type
property like all event objects
do. The default value is message
,
but the event source can specify
a different string for the property.
onmessage
event handler
receives all events from a given server event source, and can dispatch
them, if necessary, based on their type
property.
The Server-Sent Event protocol is straightforward:
EventSource
object)
and the
server keeps this connection open.
event: bid # sets the type of the event object data: GOOG # sets the data property data: 999 # appends a newline and more data # a blank line triggers the message event