Pushing Data to the Browser with Comet

See how adding Comet to AJAX will allow you to push data from the server to a client at an undetermined interval.

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

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.

Figure 1: 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

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.

The main advantage of long polling is that the result is returned to the client immediately when the query completes. Figure 2 shows that the second call to isQueryFinished only sleeps until the query is finished. Once data becomes available, the result is immediately returned to the browser. In most cases, long polling takes fewer requests than polling, but still can take many requests if the query takes a long period of time to complete. The biggest disadvantage of long polling is that there is a limit of one request per data transmission. You often can have an overhead of having many requests per second for applications such as chat or tailing a log file.

Figure 2: An Example of Long Polling

Long polling also has major scalability issues when used in a traditional web server. This is because, traditionally, each request requires its own thread. Handling thousands of simultaneous long polling requests requires huge amounts of memory.

Recently, modern web servers have started including a feature that is often called continuation. Continuation allows the thread of a connection to be suspended and then the thread can be used by another request. When the request awakes, a new thread is given to the original request. Currently, each Java Servlet Container uses its own proprietary implementation of continuation. When the Servlet 3 specification is finalized, continuation support in Java will be standardized. Look forward to my future article on continuations and the Servlet 3 specification.

When using long polling, you also must be careful not to exceed the two requests per domain limit. Today, only two simultaneous connections to a domain can be open at a time in a default configured browsers. If two simultaneous long polling requests are opened, no other AJAX calls can be made until the long polling request finishes. Even having just one long polling request open can cause AJAX calls to queue up, because only one AJAX call at a time can execute while the long polling request is open.

Streaming

The third and most advanced method of achieving pushing data from the server to a client at an undetermined interval is streaming, shown in Figure 3. Streaming is the second method of Comet. Streaming over HTTP is also called a forever response. HTTP is a stateless protocol, so the connection is closed after each response. It was eventually discovered that, if neither the browser nor the server closes the connection, multiple messsages could be transmitted on the same connection.

Figure 3: Streaming

Streaming has the advantage of allowing multiple data transmissions to be made on a single request. This means that there is no connection overhead with each data transmission. Many updates per second can be achieved if the connection speed is fast enough. Streaming also has the advantage that there is no delay from when data is available and when it is transmitted. The biggest disadvantage of streaming is that open standards are still being defined. Adoption of streaming has been very slow and standards are still being defined and are changing every week. If you plan on doing streaming-based Comet, you should be sure that testing is very well planned.

Conclusion

Comet will eventually break its way into the mainstream. New standards are being defined. Once these standards are in place, the momentum behind Comet will increase greatly. Look forward to my future article about Bayeux, an advanced open source protocol for doing Comet. When choosing among polling, long polling, and streaming, remember to consider issues such as response delay, standards, and scalability.

Advertiser Disclosure:
Some of the products that appear on this site are from companies from which QuinStreet receives compensation. This compensation may impact how and where products appear on this site including, for example, the order in which they appear. QuinStreet does not include all companies or all types of products available in the marketplace.