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

dojo.require("dojo.io.cometd");
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");
x.endBatch();

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

<servlet>
<servlet-name>cometd</servlet-name>
<servlet-class>org.mortbay.cometd.continuation.ContinuationCometdServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>cometd</servlet-name>
<url-pattern>/cometd/*</url-pattern>
</servlet-mapping>
<servlet>
<description></description>
<display-name>PushServlet</display-name>
<servlet-name>PushServlet</servlet-name>
<servlet-class>com.comet.sevlets.PushServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>PushServlet</servlet-name>
<url-pattern>/push</url-pattern>
</servlet-mapping>

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
http://repository.codehaus.org/org/mortbay/jetty/jetty-util/6.1.5/jetty-util-6.1.5.jar
http://repository.codehaus.org/org/mortbay/jetty/jetty/6.1.5/jetty-6.1.5.jar

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

http://svn.cometd.org/trunk/bayeux/bayeux.html

Another project where comet is used is Project Grizzly.Grizzly is a framework in java to build scalable servers .It uses Java NIO API. https://grizzly.dev.java.net/

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