Ruby On Rails, Design, Simplicity, Web 2.0, Ajax, Mac and Tons of Pizza.

Dec 03

A simple chat with Juggernaut

Posted by Sandro Paganotti in Ruby on Rails - comments are closed digg this add to delicious

PUSH technology has ever been a sort of Chimera for web developers. Finally, in the last year, a bounch of solutions emerged from skilled developers; one of them, strictly related with the Rails world, was Juggernaut

How does this work ?

Juggernaut automatically includes a small .swf in the user page that keeps open a socket with a special push server included in the plugin . After registering the user to one (or more) specific channels with the variable ‘session[:juggernaut_channels]’, you can send a broadcast message through those channels by using the send_data function that accepts a javascript code as parameter:


 Juggernaut.send_data(data, session[:juggernaut_channels])

Wow, pretty cool isn’t it ?

Now let’s go further and create a small chat using Juggernaut (following the instructions in the README FILE )


gem install json
gem install eventmachine
rails chat_demo
cd chat_demo
ruby script/plugin install http://juggernaut.rubyforge.org/svn/trunk/juggernaut/
ruby script/generate controller chat

Now open the controller’s file and write:


class ChatController < ApplicationController

  def index
    session[:juggernaut_channels] = ["chat_channel"]
  end

  def send_data
    input_data = CGI.escapeHTML(params[:chat_input])
    data = "new Insertion.Top('chat_data', '<li>#{input_data}</li>');" 
    Juggernaut.send_data(data, session[:juggernaut_channels])
    render :nothing => true
  end

Last, open index.rhtml in views/chat and write:


<html>
  <head>
    <%= javascript_include_tag :defaults %>
    <%= listen_to_juggernaut_channels :chat_channel %>
  </head>
  <body>
    <%= form_remote_tag(
          :url => { :action => :send_data },
          :complete => "$('chat_input').value = ''" ) %>
      <%= text_field_tag( 'chat_input', '', { :size => 20, :id => 'chat_input'} ) %>
      <%= submit_tag "Add" %>
    </form>
    <ul id="chat_data" style="list-style:none">
    </ul>
  </body>
</html>

Ok. Done :) Now we just need to start the push server (no extra juggernaut configuration is needed for this sample) and then run the standard server with these commands:


ruby script/push_server # ( for me it worked only when launched with root user)
ruby script/server

Now open a browser, surf to 0.0.0.0:3000 and enjoy your first Juggernaut creation !

Comments

  • Phill Kenoyer

    Posted on December 03

    I've been using AFLAX for over two years doing chat the exact same way. I use a perl echo server and AFLAX socket client. I'll have to check out Juggernaut now that I've been on Rails for the past year. Thanks for the tip.
  • weepy

    Posted on December 03

    Juggernaut is great. I'm using it for live chess play on http://64squar.es. Works a treat !
  • Justin

    Posted on December 06

    Juggernaut looks awesome. This is one of those posts that doesn't address a specific need I have, but it just looks so cool that I start dreaming up scenarios where I might "need" to use it...
  • Soleone

    Posted on January 10

    @Sandro: Thanks for the article, looks really good and promising! As Justin said, this the sort of technology that just looks sweet, and you want to use it, even when you have no idea what to do with it. @weepy: Respect, I really like your application. Very clean interface!

Post a comment

Categories:

Tags:

Powered by Mephisto, Valid XHTML 1.1, Valid CSS - Supported by Wave Factory