Ajax Dojo Comet Tutorial
Markus Holzmann, an intern at Profactor of my fellow colleague Philipp Hartl, had the opportunity to experiment with Ajax during his job. He wrote a tutorial about how to push events from the server to the client. For example, display popup messages on all browsers at the same time (see screencast in full resolution here):
Read on how Markus did this:
Cometd Hello World
I’ve read Chris Bucchere’s Say Hello World to Comet and built an application based on this using a more current version of Jetty (version 6.1.5) which I embedded into a Tomcat v5.5 Server. For the developing I used Eclipse 3.2.
Start your Engines
At first you have to get the server running. As I mentioned I embedded Jetty into a Tomcat server. Therefore you have configure the libraries:
- Add the packages org.mortbay.cometd and dojox.cometd to your source folder and delete the client package in the org.mortbay.cometd package.
- Add jetty-util-6.1.5.jar, jetty-6.1.5.jar and servlet-api-2.5-6.1.5.jar to your build path.
- Copy the jetty-util-6.1.5.jar file into the /lib folder in the WEB-INF directory.
Replace the existing servlets in your web.xml - file in the WEB-INF - folder with the following servlets:
<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>
For the project I used the dojo toolkit (version 0.4.3) which has an integrated COMETd class that makes it easy to build comet projects. Download it and add it to your WebContent folder.
When you’ve done all this, the hardest piece of work for this program is already done.
Hack the Code
Now you can implement the code for the client side: You need a HTML file with a button on it. The code for this looks like this (download):
<html>
<head>
<script type="text/javascript" src="../dojo.js"></script>
<script type="text/javascript">
dojo.require("dojo.io.cometd");
cometd.init({}, "cometd");
cometd.subscribe("/hello/world", false, "publishHandler");
publishHandler = function(msg) {
alert(msg.data.test);
}
</script>
</head>
<body>
<input type="button"
onclick="cometd.publish(’/hello/world’, { test: ‘hello world’ } )"
value="Click Me!">
</body>
</html>
Line by line, the above bold code works like this:
- In the line
<script type="text/javascript" src="../dojo.js"></script>
you integrate the dojo toolkit into the project.
- To activate the cometd class of dojo:
dojo.require("dojo.io.cometd"); - Connect the server with the client:
cometd.init({}, "cometd"); - Here we say what to do when there is a subscribe event:
cometd.subscribe("/hello/world", false, "publishHandler"); - Last but not least, the publishHandler function serves as the callback function, which uses alert to show a simple message box:
publishHandler = function(msg) { alert(msg.data.test); }
Give it a Try
When you load the HTML file now, you can click on the button and an alert box saying hello world will appear:

The reason for this is that when you click the code
cometd.publish('/hello/world', { test: 'hello world' } )
is executed which publishes a text on the channel with the id /hello/world.
The funny thing is that this is able to run on any number of browsers. Everytime when a client clicks the button, on all browsers that view this page the alert box is shown. (See screencast above).
Pushing Data from Server to Client
You can also add serverside code to trigger an event. I wrote a JSP file with the following code:
<%@page import="java.util.*"%>
<%@page import="dojox.cometd.*" %>
<%
Bayeux b = (Bayeux)getServletContext().getAttribute(Bayeux.DOJOX_COMETD_BAYEUX);
Channel c = b.getChannel("/hello/world",false);
Map<String,Object> message = new HashMap<String,Object>();
message.put("test", "jsp: hello world");
c.publish(b.newClient("server_user",null),message, "new server message");
%>
When this page is loaded, an alert popup appears at the page saying jsp: hello world.
That’s it. Happy hacking!
Tags: ajax, comet, dojo, howto, programming, tutorial17 Comments
Trackbacks
- Ajax Dojo Comet Beginner Tutorial « Lean Austria
- Dylan Schiemann » Blog Archive » cometd tutorial


August 25th, 2007 at 9:38 pm
Very nice, I think Comet-type applications really have a lot of potential. I’ve heard the G-Talk application inside Gmail.com uses a Comet-connection for messaging, works like charm.
The problem is getting the webservers to adopt the idea. We really need to get a standard way of doing open-connections. Time for JEE6?
August 27th, 2007 at 5:39 pm
The only problem with Comet is that you keep the connections open for a elanged time. This could cause serious problems with performance when you have multible users. Keeping the connections open also has downsides caching etc on the browser of the end-user.
The real next big thing ™ will probably be offline-clients, something like Web Start or Adobe Apollo.
August 29th, 2007 at 8:16 am
Im not so sure about offline clients. I think the next big thing will be something like Erlang, programming languages designed for concurrent development, e.g. when you use Erlang for a webserver it should not be such a problem to keep a very large number of connections open. The Apache vs. Yaws benchmark looks interesting: http://www.sics.se/~joe/apachevsyaws.html
October 8th, 2007 at 8:54 am
I am struggling with tomcat comet api, but im not able to do it…
October 29th, 2007 at 10:19 pm
I have installed Jetty and am trying to get this example to work. The cometd examples that ship with Jetty work fine (chat and echo), however, this application does not seem to work. I keep getting the 404 error that the following file does not exist.
http://www.myserver.com:8080/cometd/examples/dojo/dojo/io/cometd.js
Any ideas?
Thx in advance.
November 6th, 2007 at 5:49 pm
Hi Martin,
I have some problems to config this project:
How did you embedded the jetty into the tomcat?
“Add the packages org.mortbay.cometd” , what is the full path of ‘ org.mortbay.cometd’.
thanks benny
November 7th, 2007 at 8:05 am
Hi tatlar & benny, sorry but I cannot help you here. This article was written by a student, not me, and he is not here any more
November 8th, 2007 at 6:53 pm
I have successfully installed the stuff into my workspace and run it.But when i debug it, i realized that a server request has been sent by the waiting clients after receiving message.What is the difference between asynchronous polling and comet in that case?Nothing should be sent to the server after first subscribing.
Regards…
December 24th, 2007 at 6:13 pm
Hi,can we use PHP as comet server?
February 20th, 2008 at 9:13 pm
How to run Jetty under Tomcat?
Take the jetty-6.1.7\webapps\cometd.war file and drop it in the Tomcat 6 Webapps directory -> The web.xml file under WEB-INF gives a lot of info.
This just works!
February 27th, 2008 at 7:40 am
I configured my workspace and made that the Client side run fine.
But i couln’t make that server side code run. Some idea?
May 11th, 2008 at 3:12 pm
I played with this a little bit. Have’nt got it working fully yet but … .
benny - org.mortbay.cometd can be found in jetty source distribution in a folder “./jetty-6.1.9_src/contrib/cometd/bayeux/src/main/java/org/mortbay/…” and in “./jetty-6.1.9_src/contrib/cometd/api/src/main/java/dojox/cometd”
tatlar - this tutorial uses old dojo release. In current realease (1.1) cometd is in different location. Hence the code is a bit different:
—————————————–
dojo.require(”dojox.cometd”);
dojox.cometd.init({}, “cometd”);
dojox.cometd.subscribe(”/hello/world”, false, “publishHandler”);
—————————————–
Now I have next JS error with dojo “this.url.match is not a function. dojo.js (line 136)” . If anybody has a clue about this I would appreciate it.
May 14th, 2008 at 8:38 am
My client side is running successfully but i can’t make my server side code run.
Can anyone help please…
May 16th, 2008 at 2:11 pm
I also get this JS error with dojo “this.url.match is not a function”.
June 23rd, 2008 at 3:52 pm
I have developed a small non-blocking Java server which can handle this type of high socket concurrency and serve async responses!
http://rupy.googlecode.com
Check it out!