Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between async servlet long poll and bayeux protocol (Comet)

What is the difference between a simple Async servlet and the Comet / Bayeux protocol?

I am trying to implement a "Server Push" (or "Reverse Ajax") kind of webpage that will receive updates from the server as and when events occur on the server. So even without the client explicitly sending a request, I need the server to be able to send responses to the specific client browser.

I understand that Comet is the umbrella term for these kind of technologies; with 'Bayeux' being the protocol. But when I looked through the servlet spec, even the 'Async servlet' seems to accomplish the same thing. I mean I can define a simple servlet with the

<async-supported>

attribute set to true in the web.xml; and that servlet will be able to asynchronously send responses to the client. I can then have a jQuery or ExtJS based ajax client that just keeps doing a

long_polling() 

call into the servlet. Something like what is described in the link below http://www.ibm.com/developerworks/web/library/wa-reverseajax1/index.html#long

So my question is this:

What is the difference between a simple Async servlet and the Comet / Bayeux protocol?

Thanks

like image 315
Basanth Roy Avatar asked Jan 10 '13 22:01

Basanth Roy


People also ask

What is Bayeux protocol?

Bayeux is a protocol for transporting asynchronous messages, primarily over HTTP. CometD is a scalable HTTP-based event routing bus that uses an AJAX push technology pattern known as Comet. It implements the Bayeux protocol.

What is Comet protocol?

Comet is a programming technique that allows a web server to send updates to clients without requiring the clients to explicitly request them. This kind of programming technique is called server push, which means that the server pushes data to the client.

Is CometD a WebSocket?

CometD is a scalable WebSocket and HTTP based event and message routing bus.


1 Answers

It is true that "Comet" is the term for these technologies, but the Bayeux protocol is used only by few implementations. A Comet technique can use any protocol it wants; Bayeux is one of them.

Having said that, there are two main differences between an async servlet solution and a Comet+Bayeux solution.

The first difference is that the Comet+Bayeux solution is independent of the protocol that transports Bayeux. In the CometD project, there are pluggable transports for both clients and servers that can carry Bayeux. You can carry it using HTTP, with Bayeux being the content of a POST request, but you can also carry it using WebSocket, with Bayeux being the payload of the WebSocket message. If you use async servlets, you cannot leverage WebSocket, which is way more efficient than HTTP.

The second difference is that async servlets only carry HTTP, and you need more than that to handle remote Comet clients.

For example, you may want to identify uniquely the clients, so that 2 tabs for the same page result in 2 different clients. To do this, you need add a "property" to the async servlet request, let's call it sessionId.

Next, you want to be able to authenticate a client; only authenticated clients can get a sessionId. But to differentiate between first requests to authenticate and others subsequent requests already authenticated, you need another property, say messageType.

Next, you want to be able to notify quickly disconnections due to network loss or other connectivity problems; so you need to come up with a heart-beat solution so that if the heart beats you know the connection is alive, if it does not beat you know it's dead, and perform rescue actions.

Next you need disconnect features. And so on.

Quickly you realize that you're building another protocol on top of HTTP.

At that point, it's better to reuse an existing protocol like Bayeux, and proven solutions like CometD (which is based on Comet techniques using the Bayeux protocol) that gives you:

  • Java and JavaScript client libraries with simple yet powerful APIs
  • Java server library to perform your application logic without the need to handle low level details such as HTTP or WebSocket via annotated services
  • Transport pluggability, both client and server
  • Bayeux protocol extensibility
  • Lazy messages
  • Clustering
  • Top performance
  • Future proof: users of CometD before the advent of WebSocket did not change a line of code to take advantage of WebSocket - all the magic was implemented in the libraries
  • Based on standards
  • Designed and maintained by web protocols experts
  • Extended documentation
  • I can continue, but you get the point :)

You don't want to use a low-level solution that ties you to HTTP only. You want to use a higher level solution that abstracts your application from the Comet technique used and from the protocol that transports Bayeux, so that your application can be written once and leverage future technology improvements. As an example of technology improvement, CometD was working well way before async servlets came into picture, and now with async servlet just became more scalable, and so your application, without the need to change a single line in the application.

By using a higher level solution you can concentrate on your application rather than on the gory details of how to write correctly an async servlet (and it's not that easy as one may think).

The answer to your question could be: you use Comet+Bayeux because you want to stand on the shoulder of giants.

like image 63
sbordet Avatar answered Oct 08 '22 02:10

sbordet