HTML and CSS Reference
feature, seeing as the data stream should be a background progress the user doesn't
need to consider.
The forever frame approach effectively allows for true streaming of data and
only uses a single connection.
13.2.2 Streaming XMLHttpRequest
Similar streaming to that of the forever frames is possible using the XMLHttp-
Request object. By keeping the connection open and flushing whenever new data
is available, the server can push a multipart response to the client, which enables it
to receive chunks of data several times over the same connection. Not all browsers
support the required multipart responses, meaning that this approach cannot be
easily implemented in a cross-browser manner.
HTML5 provides a couple of new ways to improve server-client communication.
One alternative is the new element, eventsource , which can be used to listen to
server-side events rather effortlessly. The element is provided with a src attribute
and an onmessage event handler. Browser support is still scarce.
Another important API in the HTML5 specification is the WebSocket API.
Once widely supported, any solution using separate connections to fetch and update
data will be mostly superfluous. Web sockets offer a full-duplex communications
channel, which can be held open for as long as required and allows true streaming
of data between client and server with proper error handling.
Our Comet implementation will use XMLHttpRequest long polling. Long polling
is an improved polling mechanism not very different from the one we have already
implemented. In long polling the client makes a request and the server keeps the
connection open until it has new data, at which point it returns the data and closes
the connection. The client then immediately opens a new connection and waits for
more data. This model vastly improves communication in those cases in which the
client needs data as soon as they're available, yet data does not appear too often. If
new data appear very often, the long polling method performs like regular polling,
and could possibly be subject to the same failing, in which clients poll too intensively.
Implementing the client side of long polling is easy. Whether or not we are
using regular or long polling is decided by the behavior of the server, wherein