Comet Slideshow example on Grizzly
http-equiv="content-type">
A Comet Slideshow example using dojo, Comet, Bayeux, on Grizzly
with Glassfish
src="http://blogs.sun.com/carolmcdonald/resource/ajax.jpg"
style="width: 96px; height: 119px;">
src="http://blogs.sun.com/carolmcdonald/resource/comet.jpg"
style="width: 115px; height: 118px;">
src="http://blogs.sun.com/carolmcdonald/resource/bayeuxtapestry1.jpg"
style="width: 125px; height: 131px;">
src="http://blogs.sun.com/carolmcdonald/resource/grizzly.jpg"
style="width: 119px; height: 113px;">
src="http://blogs.sun.com/carolmcdonald/resource/glassfishlogo.gif"
style="width: 107px; height: 86px;">
This Sample Slideshow app demonstrates the usage of the dojo Ajax
framework, Comet, Bayeux, with Grizzly and Glassfish.
href="https://techdayscode.dev.java.net/servlets/ProjectDocumentList?folderID=9492&expandFolder=9492&folderID=8546">Download
the dojo Comet Sample Application Code
dojo
class="docTextHighlight">
is an open source DHTML toolkit written in JavaScript. It includes many
utilities that go beyond Ajax, for example the dojox.comet module
simplifies programming comet applications.
href="http://alex.dojotoolkit.org/?p=545">Comet is a term coined by
Alex Russell to describe applications where the Server pushes
data to the client. For example in the diagram below on the left
you see Ajax polling which uses synchronous
requests/responses to get events from the server. Comet
uses long-lived previously-opened HTTP connections to "push" data to
the client at any time, not only in response to user input.
src="http://blogs.sun.com/carolmcdonald/resource/comethttp.gif"
style="width: 642px; height: 356px;">
Grizzly is an HTTP
framework which uses the Java™ NIO API to provide fast HTTP processing
. Grizzly provides Comet (long-lived streaming HTTP connections)
support built on top of
Grizzly's
href="http://weblogs.java.net/blog/jfarcand/archive/2006/02/grizzly_part_ii.html">Asynchronous
Request Processing (ARP). With Grizzly ARP, each
href="http://weblogs.java.net/blog/jfarcand/archive/2006/07/the_grizzly_com.html">Comet
request isn't holding onto a thread which gives
scalability.
href="http://svn.xantus.org/shortbus/trunk/bayeux/bayeux.html">Bayeux
is a protocol for routing JSON encoded events between clients and
style="font-family: monospace;"> servers in a publish
subscribe model. Grizzly provides an implementation of Bayeux,
which makes it really easy to build Comet applications with dojo, you
just configure Glassfish for Comet and configure your Web Application's
web.xml for the Grizzly Bayeux servlet then you can use the
dojox cometd publish and subscribe methods to send and receive Comet
events as described in more detail below.
src="http://blogs.sun.com/carolmcdonald/resource/grizzly2.gif"
style="width: 657px; height: 320px;">
Grizzly comes with Glassfish , or it can be used separately. To use
Comet with Glassfish you just need to add the bold
style="color: rgb(204, 0, 0); font-weight: bold;"> red line to
the
Glassfish config domain.xml:
Code Sample from: |
|
|
Package your war and deploy it on Glassfish, then every
request sent to your war's
style="font-weight: bold;">context-path
style="color: rgb(153, 0, 0); font-weight: bold;">/cometd/will be
serviced by the Grizzly Bayeux runtime.
Explanation of the usage of dojox cometd in the sample Slideshow
Application
I modified the comet chat example from
href="http://weblogs.java.net/blog/jfarcand/archive/2007/02/gcometd_introdu_1.html">
here (originally written by
href="http://blogs.webtide.com/gregw">Greg Wilkins), to share a
slideshow presentation among all subscribed
clients. The image below shows the Comet Slideshow page, which allows
the users
to share a Slideshow and chat at the same time.
src="http://blogs.sun.com/carolmcdonald/resource/cometslideshow.jpg"
style="width: 883px; height: 708px;">
Quick installation and use of dojo with Netbeans
There are 3 ways to install dojo which you can read about at in
href="http://dojotoolkit.org/book/dojo-book-0-9/part-1-life-dojo/quick-installation">the
book of dojo. A quick and easy way to use dojo with Netbeans is to
download the JavaScript libraries from
href="http://dojotoolkit.org/downloads">http://dojotoolkit.org/downloads.
Create a new NetBeans Web Applications project. Extract the dojo
toolkit into the project web directory: .../web , then rename
dojo-release-1.1.1/ to src/ this will give you the project
structure shown below. I have already done this for the sample
project so you do not have to download dojo in order to run the sample.
src="http://blogs.sun.com/carolmcdonald/resource/dojonet.JPG"
style="width: 324px; height: 468px;">
Loading base dojo and required modules into an application
In order to load dojo into your application, put the relative
path to the dojo.js file in a script element in the head
section of your HTML page as shown below:
Code Sample from: |
|
This script element will load the base dojo script which gives you
access to all the dojo functionality. The rest of the Java Script for
this application is in the file chat.js.
Next in chat.js the application specifies which dojo modules to
load, using the dojo.require function (kind of like import
in Java):
Code Sample from:
style="font-family: monospace;">chat.js |
|
Dojo is organized into three major layers: Dojo Core, Dijit, and
DojoX. DojoX builds on Dojo Core and provides newer
extensions to the Dojo toolkit. DojoX
href="http://dojotoolkit.org/book/dojo-book-0-9/part-5-dojox/cometd-client">cometd
implements a
Bayeux protocol client for use with a Bayeux server.
Initializing a connection between the dojo client and the Grizzly
BayeuxServlet
When a user first loads the slideshow application, he can enter a
username and join a slideshow session.
style="width: 378px; height: 304px;">
When a user clicks on the Join button, the join
javascript function is called. In the join
function, the call to dojox.cometd.init initialises a
connection to the given Comet server, in this case with the Glassfish
Grizzly Bayeux servlet (note
style="color: rgb(153, 0, 0); font-weight: bold;">/cometd/* style="font-weight: bold;"> is the url-pattern for the Grizzly Cometd Servlet configured in the web.xml for theapplication).
style="color: rgb(0, 0, 0);">var room = {
...
join: function(name){
dojox.cometd.init("
style="color: rgb(153, 0, 0); font-weight: bold;">/cometd");
dojox.cometd.subscribe("/chat/demo", room, "_chat");
dojox.cometd.publish("/chat/demo",
{ user: room._username,
join: true, chat :
room._username+" has joined"});
}
The dojox.cometd.subscribe
callbackline subscribes
the_chat
function to the
/chat/demo
style="font-weight: bold;"> channel. Any time a message issent to the
/chat/demo channel the _chatfunction will be called.
The dojox.cometd.publish linepublishes the message that the user (the name that was entered
with the join button) has joined the
/chat/demo channel.Subscribers
to the /chat/demo channelwill get this message.
Publishing the next slide for the Comet Slideshow
When the user clicks on the "Next Slide" button shown below, a
javascript funtion is called which publishes the url for the next slide.
src="http://blogs.sun.com/carolmcdonald/resource/elephantslide.jpg"
style="width: 614px; height: 660px;">
Code Sample from: |
|
When the user clicks on the Next Slide button, the javascript
function shown below is called. This function calls room.next passing
the url for the next slide. The function then increments the index for
the next slide. The urls for the slides are stored in the
slideUrlsarray shown below. style="color: rgb(0, 0, 0);">widget.jsonvar room = {
... _init:
function(){
var slideUrls=[
"/dojoComet/images/image0.jpg",
"/dojoComet/images/image1.jpg",
"/dojoComet/images/image2.jpg",
"/dojoComet/images/image3.jpg",
"/dojoComet/images/image4.jpg",
"/dojoComet/images/image5.jpg"];
var i=0;
element=dojo.byId(' style="font-weight: bold; color: rgb(153, 0, 0);">nextB');
element.onclick = function(){
room.next( slideUrls[i]);
if (i>=slideUrls.length){i=0;}
else {i++;}
}
element=dojo.byId(' style="font-weight: bold; color: rgb(153, 0, 0);">previousB');
element.onclick = function(){
room.next( slideUrls[i]);
if (i<=0){i=0;}
else {i--;}
} }
... The function
room.next, shown below, calls dojox.cometd.publishto publish the next slide url (input argument) to the
/chat/demochannel. Subscribers channel will get this message.to the/chat/demo
style="color: rgb(0, 0, 0);">chat.js style="color: rgb(204, 0, 0); font-weight: bold;">var room = {
...
next: function(text){
dojox.cometd.publish("/chat/demo", {slide: text});
}
...
}
When a message is published to a Bayeux
channel on the server, it is delivered to all clients
subscribed to that channel, in this case to the "/chat/demo
style="font-weight: bold;">" channel . In the room.join
function shown before
dojox.cometd.subscribe("/chat/demo", room,
"_chat") was called to subscribe the style="font-weight: bold;">_chat callback function tothe
/chat/demo
style="font-weight: bold;"> channel. The _chatcallback function, shown below, is called with thepublished message as an input argument. The
_chat callbackfunction updates the browser page by setting the slide dom
element
innerHTML to an html img tag with the slide urlfrom the published message
"<img
src='" + slideUrl + "'/>" . This updates the browser pagewith the image corresponding to the slide URL which was published.
style="color: rgb(0, 102, 0); font-weight: bold;"> style="color: rgb(0, 0, 0);">chat.js style="color: rgb(0, 102, 0); font-weight: bold;">var room = {
...
_chat: function(message){
var slide=dojo.byId('slide');
var
slideUrl=message.data.slide;
slide.innerHTML ="<img
src='" + slideUrl + "'/>";
...
}
Conclusion
This concludes the sample application which demonstrates the usage of
the dojo Ajax
framework, Comet, Bayeux, with Grizzly and Glassfish.
Running the Sample Code
The sample code is available as a NetBeans project. You can build
and run the sample code using the NetBeans IDE.
Setting Things Up
- Download
and install NetBeans 6.1 bundled with GlassFish V2
- Alternatively you can
href="https://glassfish.dev.java.net/public/downloadsindex.html">Download
and install GlassFish V2 separately. - To use
Comet with Glassfish you just need to add the bold style="color: rgb(204, 0, 0); font-weight: bold;"> red line to
the
Glassfish config domain.xml (in the directory
glassfish/domains/domain1/config ):cellspacing="0">
Code Sample from: style="color: rgb(0, 0, 0);">index.htmlblocking-enabled="false" default-virtual-server="server"
enabled="true" family="inet" id="http-listener-1" port="8080"
security-enabled="false" server-name="" xpowered-by="true">
- Bayeux and dojo are already configured in the sample code.
Open and Run the Sample code:
- Download the href="https://techdayscode.dev.java.net/servlets/ProjectDocumentList?folderID=9492&expandFolder=9492&folderID=8546">sample
code and extract its contents. You should now see the newly
extracted directory as<sample_install_dir>/dojoComet
is the directory where you unzipped the sample package. For example, if you extracted the contents to,
where<sample_install_dir>C:\on a Windows machine, then your newly created directory should be atC:\dojoComet.
- Start the NetBeans IDE. Click Open Project in the File menu and
select thedojoCometdirectory you just
unzipped.
- Build the project as follows:
- Right click the
dojoCometnode in the Projects window.
- Select Clean and Build Project.
- Right click the
- Run the project as follows:
- Right click the
dojoCometnode in
the
Projects window.
- Select Run Project.
- Right click the
When you run the project, your browser should display the opening page
of the Sample Application (at
http://localhost:8080/dojoComet/). Open
another browser and set that url to http://localhost:8080/dojoComet/ then enter a name and click on the join button in both browser windows. Then click on the next slide button in one browser window. Both browsers should get updated with the next slide.
For more Information:
- Jean-Francois Arcand's Blog on Cometd framework and its Bayeux protocol support in Grizzly
- Using the Grizzly Comet API
- dojo toolkit
- dojo cometd
- grizzly
- glassfish
- Enterprise Comet: Awaken the Grizzly!
- Asynchronous HTTP and Comet architectures
- A Comparison of Push vs Pull Ajax
- Comet and
Reverse Ajax: The Next Generation Ajax 2.0 book
- Login or register to post comments
- Printer-friendly version
- caroljmcdonald's blog
- 4997 reads






Comments
by caroljmcdonald - 2008-08-21 08:46
did you take a look at Jim Driscoll's Dead Simple Comet Example on Glassfish http://weblogs.java.net/blog/driscoll/archive/2008/05/dead_simple_com_1....by ladybytes - 2008-08-21 08:42
Hello Carol. I concentrated on working dojo comet, based in this instance. Now I'm with a problem, I hope I can guide. On the one hand I have java classes, which get data from an external source, and from there, I need to make the publication of the data itself. In the example, the issue of the publication is done in js, which does not help, that is not as accessible from my classes java to it. I have reviewed some examples, but in Jetty, and I need in Glassfish. Know of any examples that can publish data from my classes java, but always using dojo, and bayeux cometd? I hope I understand Regards M.Gracielaby caroljmcdonald - 2008-08-16 18:44
This is an exampleby igf1 - 2008-08-16 12:36
Hi Carol , I would really like to see an example of server side grizzly + dojox, do you know of any good docs regarding that topic?by caroljmcdonald - 2008-08-06 14:41
JMS provides publish/subscribe events. This JavaOne talk is about ajax push and openESB. http://developers.sun.com/learning/javaoneonline/2007/pdf/TS-8434.pdf Also take a look at this article about bayeux and jms http://cometdaily.com/2008/07/09/implementing-a-bayeux-to-jms-bridge/by ladybytes - 2008-08-06 14:28
Hello Now I have no problems with Internet Explorer, which is not serious the problem, but now found no faults. Greetings.by ladybytes - 2008-08-06 14:26
Hello Run the example and also transforms them into another application that I'm looking for, is precisely what I need, but I need help or guide, regarding how can I connect my server and push their data with other non-web applications, as java desktop applications, flash and J2ME applications. I'm working on a system publisher / subscriber, where subscribers are different applications of various kinds, including those already appointed, I will need any special or API already exists for any implementation goes straight to my data for example, an application desktop in swing . Well, waiting for his response, it was dismissed from Chile. Maria Graciela Manquehual.by caroljmcdonald - 2008-08-01 13:27
I'm using IE 7 on windows XP and its okby caroljmcdonald - 2008-08-01 13:17
I run the example with one browser internet explorer and one browser firefox, and I don't have problems. Sometimes the image loading might be a little slowby ladybytes - 2008-08-01 13:08
Hello, I have problems running the example in Internet Explorer, sometimes not shown the messages sent or the images are not loaded. In firefox entirely correct, but I'm working on a prototype based on this example and be submitted in Internet Explorer. I podrias guide in this problem? Thank you, greetings from Chile M.Graciela