PubSub using cometd and dojo


Using Publish Subscribe approach one can develop a real time web application which requires a lot of data change to be displayed on a webpage. This PubSub approach using the Comet Server is much better and faster then the thousands of Ajax XHR requests made to the server. Here is a two step description:

Step one: Install and Configure Comet Server: I would like to take cometd, the ajax push engine which Bayeux protocol, the cometd engine requires a Servlet container like tomcat or jetty. The Jetty has a distribution which helps in quick setup of cometd server here is a download link(http://dist.codehaus.org/jetty/). Download the latest Jetty distribution and ensure that you have a compatible JDK installed on the machine and also ensure that you have maven (http://maven.apache.org/download.html) then extract the distribution and go to the folder contrib/cometd/demo and issue a command mvn jetty:run or <pathtomvn.exe>/mvn jetty:run… this will do some downloading ans installing and finally the server will startup and will show you at the command prompt which port it is running. Go to a web browser and type localhost:<port> and it should display comet logo along with some demo examples.let us assume that our server is running at 8282.

Step2: Client Side PubSub Code: Dojo provides some functions to use the publish/subscribe methods to transfer data from and to the server, following is the description of some important functions:

dojox.cometd.init(“http://localhost:8282/cometd&#8221;); —  this step configures and performs a handshake with the comet server which in our case is running at port 8282.

dojox.cometd.subscribe(“/apillevel/ch01″,”func1”); — this dojox function subscribes to a message channel named /apilevel/ch01 with the listener function func1() in our case and when ever there is a message it is delivered to the javascript function named func1(). It will be executed as soon as there is a message send by any other subscribed clients.

dojox.cometd.publish(“/apilevel/ch01”,message); — this dojox function publishes a messages to the channel named /apilevel/ch01 the message can be in any format plain text/JSON/XML.. etc. When this function is executed all the subscribed clients receive the message and is processed by each clients subscribed function in our case javascript function func1() above.

dojox.cometd.unsubscribe(“/apillevel/ch01”); — it just unlinks the listener function named func1() in our case.

All tough this all works fine there are many bogs like this (http://armstrongonsoftware.blogspot.com/2009/12/comet-is-dead-long-live-websockets.html) saying HTML5 Websockets is the end to comet! yes i also think to some extent it is true… why not as a developer i would like to open a long lived socket connection from a browser to the server directly and put/get messages continuously direct from the server. This last line was not meant to discourage…

enjoy cometting till stable websockets arrive!

Further Reading:

http://cometd.org/

http://blogs.webtide.com/

http://cometdaily.com/maturity.html

http://www.ibm.com/developerworks/java/library/j-jettydwr/

http://www.infoq.com/news/2010/03/websockets-bayeux

Advertisements

About Dominic

J for JAVA more about me : http://about.me/dominicdsouza
This entry was posted in Thechy Stuff. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s