Search |
||
Getting started with Atmosphere CPR part 1: Writing the HelloWord of Comet....a Chat applicationPosted by jfarcand on March 17, 2009 at 1:19 PM PDT
Time to get started with Atmosphere CPR (Comet Portable Runtime)! In this first part, I will describe how to write a chat application and deploy in on Tomcat, Jetty and GlassFish. Note: The Atmosphere Framework have evolved since the release of that blog. Please visit our web site for an updated sample and white paper Let do the basic first. Let's use Maven 2 and create the file structure: Which will create the following structure: The next step is to add the required context.xml and save it under META-INF/ Finally, let's add Atmosphere CPR library to the pom.xml so it gets added to our WEB-INF/lib We are now ready to write our first AtmosphereHandler, which is the central piece of any Atmosphere CPR application. Let's just implement this interface As described here, implementing an AtmosphereHandler requires two methods: The onEvent method will be invoked every time a request gets mapped to it associated AtmosphereHandler. There is two way to map request to an AtmosphereHandler. By default, the name of the AtmosphereHandler will be used, e.g. assuming we name our web application chat.war, a request to http://localhost:8080/chat/ChatAtmosphereHandler will invoke the AtmophereHandler.onEvent. For the chat, let's assume we will suspend the response when the browser is sending us GET request The central piece is the AtmosphereEvent, from which we can retrieve the request and response object. Next we do some setup and then once we are ready we just need to invoke the AtmosphereEvent.suspend(), which will automatically tell Atmosphere CPR to not commit the response. Not committing the response means we can re-use it later for writing. In the current exercise, we will use the suspended response when someone enter join or enter sentence inside the chat room. Now let's assume when a user logs in or enter sentences, the browser set a POST (posting some data). So when a user logs in The important piece here is line 94. A Broadcaster's role is to publish data to the suspended responses. As soon as you broadcast data, all suspended responses will be given a chance to write the content of the broadcast. Above we just broadcast the name and also which WebServer we are running on (for demo purpose). Calling Broadcaster.broadcast() will in turn invoke your AtmosphereHandler.onMessage will all suspended response. Here let's assume we just reflect (write) what we receive: Next is when the user enter some chat messages: Here we encode the message using the JSON format so it make simple for the client's javascript to update the page. That's it for the AtmosphereHandler. You can see the complete source code here. Now let's assume we want to have a more fine grain way to map our AtmosphereHandler to the request. To achieve that, create a file called atmosphere.xml under src/main/webapp/META-INF/ and define the mapping you want: With this file, all requests to /chat will be mapped to our ChatAtmosphereHandler. Now let's explore the client side. First, let's write a very simple index.html file: Simple form that will send back to the server the login's name and the chat message entered. To update on the fly the interface as soon as our ChatAtmosphereHandler.onMessage write/send us data, let's use prototype and behavior javascript. I'm assuming you are either familiar with those framework or have basic understanding how they work. This will be defined under application.js. As soon as the user enter its login name, let's do When the user write new chat message, let's push Now when we get response, we just update the page using The way the index.html and application.js interact is simply defined by: See the complete source code here. So far so good, now we are ready to deploy our application into our favorite WebServer. Here are simple pictures from different WebServer: Glassfish v3
Jetty
Tomcat
Grizzly
Wow that was easy! Download the war or src to get started. Follow us on Twitter for daily update about the project status and ask your questions using users@atmosphere.dev.java.net technorati: atmosphere comet »
Related Topics >>
Glassfish Comments
Comments are listed in date ascending order (oldest first)
Submitted by vineetchadha on Sun, 2009-05-17 14:44.
Although the chat application works fine (missed a message or 2 in IE, maybe due to prototype/behavior issues) on tomcat 6.0.x with NIO connector the same is not true for APR connector. Is this a known limitation, because COMET in tomcat shld work for both the NIO and APR connectors? (at least according to tomcat 6.0.x documentation at http://tomcat.apache.org/tomcat-6.0-doc/aio.html)
Submitted by bertrandg on Thu, 2009-04-30 09:31.
Thank you for your interesting article, and to the team project!
I successed some experiments with a simple Groovy script (using HTTPBuilder) that acted like a browser and that communicated with the Chat server. So I was able to send messages and receive messages coming from browsers.
Thanks.
Submitted by aconran on Tue, 2009-03-17 17:29.
After deploying the war to the latest version of Tomcat when I try to login it returns with the following 500 error.
javax.servlet.ServletException: Tomcat failed to detect this is a Comet application
Please add the following content under your META-INF/context.xml of your war file.
This is already there. Any ideas?
Submitted by jedeegan on Thu, 2009-03-19 05:56.
After deploying WAR into Tomcat 6.0.18, I added file atmosphere.xml to META-INF folder. When I open http://localhost:8080/chat in browser, I got HTTP 404 error - "requested resource not available." Tomcat Manager shows context as /atmosphere-chat-0.1-ALPHA, which loads the GlassFish login page (index.html). Please advise how to run under Tomcat, or is WAR only meant for GlassFish and runs OK after modifying domain.xml per your blog? Thanks for very useful (scalable) AjaxPush/Comet implementation.
Submitted by aconran on Thu, 2009-03-19 16:07.
jedeegan - rename the war to just atmosphere-chat.war without the version information. Then put in your webapps directory.
You will also need to change the xml Connector node's protocol attribute in server.xml from HTTP/1.1 to "org.apache.coyote.http11.Http11NioProtocol".
|
||
|
|