Jean-Francois Arcand works for Ning.com. Previously he has worked for Sun Microsystems where he created Grizzly (NIO Framework) , Atmosphere and was a significant contributor to the GlassFish Application Server. Jean-Francois is a DZone MVB and is not an employee of DZone and has posted 23 posts at DZone. You can read more from them at their website. View Full User Profile

Writing Comet Applications Using JRuby and the Atmosphere Framework

09.11.2009
| 4524 views |
  • submit to reddit

Writing Atmosphere's Comet based applications is simple. Imagine using JRuby instead of Java...it becomes really simple!. As with Scala, it is also possible to write Comet application with Atmosphere Framework using JRuby. A user of Atmosphere recently posted the famous chat application written using JRuby and an embedded instance of Jetty. I've decided to write the same application using the Atmosphere Spade Server, which build on top of Jersey and Grizzly (will soon support Netty and Jetty). The idea behind the Atmosphere Spade Server is to make it really simple to test and embed your Comet application. Note that any application written using the Atmosphere Spade Server is portable, e.g. it will also deploy into any Servlet Container like Tomcat, Glassfish, Weblogic etc.

Before jumping into JRuby, let's just explore the Atmosphere Spade Server main class, AtmosphereSpadeServer:

public static AtmosphereSpadeServer.build(String uri);

Invoking that simple method will automatically creates, under the hood, a Grizzly Servlet Container instance ready to receive requests based on the URI entered

AtmosphereSpadeServer.build("http://localhost:8080/")

Next step is to add your Atmosphere POJO aka AtmosphereHandler (I recommend you read that post for more details about AtmosphereHandler):

AtmosphereSpadeServer.build("http://localhost:8080/").addAtmosphereHandler(...);

And finally, you are ready to start it:

AtmosphereSpadeServer.build("http://localhost:8080/").addAtmosphereHandler(...).start();

If you are using Atmosphere Core/REST (powered by Jersey), it event simpler as you don't need to define AtmosphereHandler, but instead just tell Jersey the name of the package to look for JAX RS resources:

AtmosphereSpadeServer build("http://localhost:8080/","org.atmosphere.myresources").start()

Now let's jump into JRuby world and uses the AtmosphereSpadeServer:

  require 'java'
   
  Dir["./Java/atmosphere/atmosphere-spade-server.jar"].each { |jar| require jar }
   
  include_class 'javax.servlet.http.HttpServlet'
  include_class 'org.atmosphere.cpr.AtmosphereHandler'
  include_class 'org.atmosphere.grizzly.AtmosphereSpadeServer'
   
  #setup and start the server
  def main
    AtmosphereSpadeServer.build("http://localhost:8080/")
              .addAtmosphereHandler("", ChatPage.new())
                  .addAtmosphereHandler("/chat-stream", ChatStream.new()).start
  end

The above code starts the server using two AtmosphereHandler. The first will send back a chat.html page to be rendered by the browser

  #serve the chat page
  class ChatPage
    include AtmosphereHandler
    
    def onEvent(event)
      Kernel.load(__FILE__)
      res = event.getResponse()
      
      res.setContentType("text/html")
      res.addHeader("Cache-Control", "private")
      res.addHeader("Pragma", "no-cache")
      File.open(File.dirname(__FILE__)+'/chat.html').each { |line|
        res.getWriter().write(line)
      }
      res.getWriter().flush()
      event
   end
  end

The second one is used for suspending the response and broadcasting chat messages

  #serve the chat stream and post messages
  class ChatStream
      include AtmosphereHandler
    
      BEGIN_SCRIPT_TAG = '<script>'
      END_SCRIPT_TAG = '</script>'
    
      def onEvent(event)
          req = event.getRequest();
          res = event.getResponse();
          
          res.setContentType("text/html")
          res.addHeader("Cache-Control", "private")
          res.addHeader("Pragma", "no-cache")
          res.setCharacterEncoding("UTF-8");
         
          if (req.getMethod().upcase === "GET")
              # for IE
              res.getWriter().write("<!-- Comet  enables web " +
                                    "servers to send data without having any need " +
                                    "for the client to request it. -->\n");
              res.getWriter().flush();
              <b>event.suspend();</b>
              
          elsif (req.getMethod().upcase === "POST")
              message = req.getParameterValues("message")[0].to_s
              ip = req.getRemoteAddr().to_s
              <b>event.getBroadcaster().broadcast(
                  BEGIN_SCRIPT_TAG +
                  "window.parent.say('<small>#{ip} - 
                              #{req.getHeader("User-Agent").to_s}:
                             </small><br><b>#{message}</b>')" +
                  END_SCRIPT_TAG);</b>
                  
                  res.getWriter().write('success')
                  res.getWriter().flush();
          end     
          
          event
      end 
      
      def onMessage(event)
          writer = event.getResponse().getWriter()
          writer.write(event.getMessage().to_s.ljust(1*1024))
          writer.flush()
          event
      end 
  end

With Atmosphere, every suspended connection are represented by an instance of AtmosphereEvent, and when a Broadcast operations happens, the AtmosphereHandler.onMessage(AtmosphereEvent) will be invoked hence an application can write back the broadcasted message.

Of course, you can do much more complex application using Atmosphere and JRuby, but my goal here was just to show how easy it can be.

For any questions or to download the above sample, go to our main site and use our Nabble forum (no subscription needed) or follow us on Twitter and tweet your questions there!

Published at DZone with permission of Jean-Francois Arcand, author and DZone MVB.

(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)