DZone
Java Zone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
  • Refcardz
  • Trend Reports
  • Webinars
  • Zones
  • |
    • Agile
    • AI
    • Big Data
    • Cloud
    • Database
    • DevOps
    • Integration
    • IoT
    • Java
    • Microservices
    • Open Source
    • Performance
    • Security
    • Web Dev
DZone > Java Zone > Writing Comet Applications Using JRuby and the Atmosphere Framework

Writing Comet Applications Using JRuby and the Atmosphere Framework

Jean-Francois Arcand user avatar by
Jean-Francois Arcand
·
Sep. 11, 09 · Java Zone · Interview
Like (0)
Save
Tweet
5.67K Views

Join the DZone community and get the full member experience.

Join For Free

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!

application Atmosphere (architecture and spatial design) Jruby Comet (pinball) Framework

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • What Developers Need to Know About Table Partition Pruning
  • Making Your Own Express Middleware
  • Handling Multiple Browser Windows and Tabs in Selenium PHP
  • Terraform Controller: Cloud Resource Self-Service

Comments

Java Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • MVB Program
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends:

DZone.com is powered by 

AnswerHub logo