HTML and CSS Reference
In-Depth Information
this period, the request times out and a new request is sent from the client. This therefore
reduces the latency for receiving notifications, but does not really reduce network traffic.
Essentially COMET is a hack. It is a set of techniques for implementing a common problem
that lacks a good solution. The Server Sent Events API on the other hand is a solution to
the underlying problem.
The Server Sent Events API is relatively simple from the point of view of the client. The
browser indicates it wishes to receive server sent events by creating an Event Source, which
in turn sends an HTTP request to the server. The HTTP request is sent to the URL specified
in the EventSource: this must be a service capable of handling Server Sent Events:
var eventSource = new EventSource("/newtasks.php");
The server will respond to this HTTP request with an HTTP response with the MIME type
of text/event-stream . The server can then begin using the connection to send messages to
the client. The client identifies the end of one message, and the start of new one, with two
empty new lines in the payload, although this detail is hidden from you as a JavaScript pro-
grammer; instead a listener is attached to the event source to access the server sent events:
eventSource.onmessage = function(event) {
The data sent by server sent events is just plain text, encoded with the UTF-8 character
encoding. The messages can of course conform to a data format standard such as XML or
JSON, but this is not a requirement.
If it is so simple to implement server sent events you may be wondering why you have not
heard more about them. Unfortunately server sent events suffer from the same problems as
1. Many web servers (including Apache) are not capable of leaving a connection open for
an extended period of time (which Server Sent Events requires) without utilizing resources
on the server. Many servers will need to change the way they perform connection manage-
ment before they can effectively support Server Sent Events.
2. Many clients are behind firewalls which prevent connections being left open for an ex-
tended period of time.
A new class of server has begun to emerge designed specifically to work with this new
paradigm; this is resolving the first of these problems. The most prominent amongst these
servers is probably Node.js. Node.js utilizes JavaScript as its programming language, prov-
ing that JavaScript can live outside the browser.
Search WWH ::

Custom Search