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

后端 未结 1 715
走了就别回头了
走了就别回头了 2021-02-01 10:50

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\")

相关标签:
1条回答
  • 2021-02-01 11:10

    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.

    0 讨论(0)
提交回复
热议问题