A simple chat with Juggernaut
Posted by Sandro Paganotti in
Ruby on Rails -
comments are closed
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
weepy
Posted on December 03
Justin
Posted on December 06
Soleone
Posted on January 10