Pushing Data to the Browser with Comet

AJAX has become so popular because it allows a browser to initiate a request to the server to receive or push data. Pushing data from the server to a client at an undetermined interval has been a problem that AJAX has never been able to deal with well. Simple examples of applications that AJAX does not handle well due to the need for server push are chat applications, live temperature updates, tailing log files, and calling long running queries. Comet can solve this problem. Comet is a web application architecture that allows a web server to send data to a browser without any need for the browser to request the data. Three common examples of allowing a server to push data to a browser are: polling, long polling, and streaming. Long polling and streaming are two strategies that are considered Comet.

Polling

The simplest solution to the problem of pushing data to the client at an undetermined interval is polling. Polling is achieved by using AJAX to periodically make client-based requests to the server at a fixed interval. Polling is typically implemented by using the setTimeout function in JavaScript (see Listing 1).

Listing 1: Using Polling to Asynchronously Execute A Long Running Query

function executeLongRunningQueryOnServer() { makeAJAXCallStartAsyncQueryOnServer(); checkIfQueryComplete(); } function checkIfQueryComplete() { var queryResult = AJAXCallToSeeIfQueryFinished(); if (queryResult.isFinished){ displalyResults(queryResult); } else { setTimeout("checkIfQueryComplete()",10000); } }

Listing 1 shows a simple example of how polling can be used to execute a long running query that would typically timeout if executed directly from an AJAX call. In this example, an AJAX call is made to execute a query on the server. The next step is to make an AJAX call every 10 seconds to see whether the query has completed. When the query is completed, the next AJAX call (poll) will get the results as shown in Figure 1. Polling is not considered Comet, but the experience for the client can be very similar to Comet. Before comet was discovered, polling with AJAX requests was used in place of Comet.

An Example of Polling

The greatest advantage of polling is that it is very simple. Polling only requires simple AJAX and JavaScript on the client. The server implementation is also fairly simple. Polling has many disadvantages, including unnecessary requests and the response can be delayed as much as the polling interval.

Long Polling

The second method of achieving pushing data from the server to a client at an undetermined interval is long polling. Long polling is considered Comet. Long polling is very similar to polling, except the request sleeps on the server instead of the client. If at any time data becomes available on the server, the sleeping request is awakened and the requested data is immediately returned to the client (see Listing 2). One factor you must keep in mind is that your AJAX request will time out if you sleep too long. To overcome timing out, you should only sleep for 30 to 60 seconds on the server. If the data is not available, you must repeat the long poll request until data becomes available.

Listing 2: Using Long Polling to Asynchronously Run A Long Running Query

function executeLongRunningQueryOnServer() { makeAJAXCallStartAsyncQueryOnServer(); checkIfQueryComplete(); } function checkIfQueryComplete() { //will sleep on server 30 seconds if var queryResult = AJAXCallToSeeIfQueryFinished(); if (queryResult.isFinished){ displalyResults(queryResult); } else { checkIfQueryComplete(); //no setTimeout } }

Listing 2 shows the only difference in client-side coding between polling and long polling. Long polling will immediately make another long poll request when a long poll request returns if the query has not finished.

Page 1 of 2