Comet - ajax broadcasting chat

I was playing around with the comet concept using jetty and dojo.I tried to make a sample app.We know that the mail updating feature of Gmail.It is done using Comet,meebo chats,google docs collaborative editing.The client does have a keep-alive connection to server.One of the popular comet implementation is done using Bayeux protocol.Its supported by dojo,jetty, servers like Weblogic ,Webspehere(uses dojo) etc.Comet is similar to Reverse Ajax by DWR or HTTP Push.If we are using the MIME type "multipart/x-mixed-replace" can be used to push different newer versions of document to the connected client (I think it was introduced in earlier versions of Netscape).

Ok I will explain how I did ...

Adding dojo to client as

cometd.init({}, "cometd");
cometd.subscribe("/messages", false, "publishHandler");
publishHandler(msg) handles responses from server

In server

Bayeux b = (Bayeux)getServletContext().getAttribute(Bayeux.DOJOX_COMETD_BAYEUX);
Channel c = b.getChannel("/messages",false);
Client x = b.newClient("server_user",null);
c.publish(x,message, "new server message");

Every request mapped to comet/* will be handled by the comet implementation in jetty
In web.xml the servlet mapping is done as


I didn't use dojo.publish(...) for publishing data.Instead i just used the GET method and
using a PushServlet i pushed data back to a connected client.We can develop the application in long -polling; in which the messages are sent to the server as the 'message' parameter of a application/x-www-form-urlencoded encoded POST request ; while callback polling , will sent messages as similar to JSON-P protocol where the callback function is passed to server as jsonp parameter.For comet applications a request is made, connection is established and subsequent reaponses are recieved by client.If Ajax is used there will be frequent request and response that would overload the server.

You can download the war.
add these libs

goto http://localhost:8080/comet_sample/test.jsp --- admin console
goto http://localhost:8080/comet_sample/client.html --- client console

Need to know about bayeux ? They have a written a draft on the protocol

Another project where comet is used is Project Grizzly.Grizzly is a framework in java to build scalable servers .It uses Java NIO API.

For IBM websphere the comet implementation is done for WebSphere Platform Messaging (v6.0) included with IBM WebSphere Application Server Feature Pack for Web 2.0 Beta Program

No comments:

Post a Comment