Jump to content

Use Comet with Flex for Enterprise RIAs

  adfm's Photo
Posted May 04 2010 02:34 PM

Do you build Rich Internet Applications that require full duplex communication? This excerpt from Fain, Rasputnis, & Tartakovsky's Enterprise Development with Flex introduces you to Comet, a full duplex model similar to AJAX.

A number of open source and commercial implementations of Comet exist in Java and Python. They can be very different, capitalizing on nonblocking I/O, using optimized threads, or offering more efficient native sockets support.

A servlet container in Jetty works in a half-duplex mode: it opens a dedicated streaming connection for flushing the data to the client, but also allows request/responses.

The Comet model is a full duplex that uses a two-way socket implementation (like in Apache Tomcat), which extends a conventional request/response model with events that are being sent on an established HTTP connection.

With Comet, the idea is that the server provides a second model for the requests handler in addition to the conventional one. There is a dedicated open connection that receives events related to the requests. If you run a Java servlet, it will receive additional events from the server: connect, read, error, and disconnect:

connect and disconnect

Define the life span of the connection object available for communications.


Notifies the servlet of the low-level errors in the transmission protocol.


Dispatched when there is a request coming from the client; allows the server to read and process it. The server keeps connection and response objects and writes (flushes) the information to the client as needed.

Adding an event model to the server side brings symmetry to the client/server programming model and greatly simplifies the asynchronous programming. Unfortunately, existing implementations of this model are not overly reliable.


If you want to use the two-way socket model, you will need to write some custom code using the Flash NetConnection object to stream the data from the client to the server, too.

Consider how this model is different for fine-grained requests common in today’s AJAX applications. Imagine that you’re in a coffee shop with a lousy WiFi connection sporting 1-second latency for a typical eBay response implemented as a web device, watching 30 items.

With the current browser settings (two connections per domain), it would take you 15 seconds to refresh all 30 items. With six allowed browser connections, this time is reduced to five seconds, but will require a more powerful infrastructure on the server side.

With the Comet-type requests, you can send all 30 requests without waiting for a single response (the same will be done with AMF HTTP batching) and will receive all 30 responses asynchronously. Meanwhile, with HTTP batching, you would get all 30 responses at once, and need some kind of sorting adapters on both sides to distribute batch members to the proper responders.

Enterprise Development with Flex

Learn more about this topic from Enterprise Development with Flex.

If you want to use Adobe Flex to build production-quality Rich Internet Applications for the enterprise, this groundbreaking book shows you exactly what's required. You'll learn efficient techniques and best practices, and compare several frameworks and tools available for RIA development -- well beyond anything you'll find in Flex tutorials and product documentation. Through many practical examples, the authors impart their considerable experience to help you overcome challenges during your project's life cycle.

See what you'll learn

0 Replies