Web Chat, Cross Browser

Hi all,

I am newly learning Rails and playing with Web Chat app. I looked through what other people have done. I tested the following very naive, skeleton app. It seems to work in my local machine when I open two Firefox (or two Safari). But it does not work cross browser, eg, Firefox-Safari. Strange? What's going on? Thanks.

- index.rhtml <% form_remote_tag :url => {:action => 'say'} do %> <%= text_field_tag 'said' %> <%= submit_tag 'Say' %> <% end %>

<%= periodically_call_remote :url => {:action => 'get_new_line' }, :frequency => 1.0 %>

- chat_controller.rb def say session[:what_said] = params[:said] @line1 = session[:what_said] end

def get_new_line @line2 = session[:what_said] end

- say.rhtml <%= @line1 %>

- get_new_line.rhtml <%= @line2 %>

I would recommend going against the Rails option here as you're making a request at least once every second to an action in your application. Imagine if 10 people were in the chat, that would mean 10 requests per second.

Think of an alternative option here, like an IRC server or similar.

Campfire does it, so it does work when done right.

You could have a look at juggernaut, does use a flash file and extra process running on the server.


Best regards

Peter De Berdt

Yea just wanted to second the juggernaut option. I am using it and it works great.

Are you storing everything in the session ? The session will be stored per user (ie per browser) so it's not surprising it doesn't work.


Hmm, I see. I guess everything has to be stored in models. Thanks.

Yeah, I should try Juggernaut. But I wonder if my web host will allow push server stuff. Thanks.

Sly -

Yeah, I should try Juggernaut. But I wonder if my web host will allow push server stuff. Thanks.

if you're not adverse to having your chat scripts being stored on a public service lingr.com can be integrated easily in a day.

I've had nice success making private password protected chat rooms for a community using lingr.

You can find docs on their blog, etc.

cheers, Jodi

And a third from me. I met the developer of Juggernaut at RailsConf and saw a demo. Very slick. It uses a small flash object to keep a persistent connection open between client and server so you eliminate a lot of issues there. From a development perspective it was basically as simple as saying "open chat channel 9" (work out the details for your app) and then the client/browser just listens.

Another option, if you're shopping, is Jabber. I was leaning towards this until I ran into Juggernaut.

Good luck, AndyV

Jabber is a communication protocol. You'll still need a Comet (official term) implementation like Juggernaut to push chats out to the user or do polling like Gmail's in-browser chat client does (IIRC),


Right, Jabber won't allow a persistent connection without some "comet" alternative. I already advocated Juggernaut but here is another alternative:


That is using a more standard comet implementation called shooting_star.

Like Juggernaut, it opens a persistent connection which can be used for implementing a chat system.

I have looked at both and I chose Juggernaut but it easily could have been the wrong decision. However, juggernaut was so easy to use and it worked so I had no reason to move away from it. As long as the browser has Flash, it works like a charm.

Thank you all for great suggestions! In fact, I installed Juggernat today. I followed its README file example included below. It works, but whenever Rails call is made, alert window ( "Juggernaut: Received data: try { ... " ) pops up all the time. How can I block this?

-chat controller:

class ChatController < ApplicationController   def index   end

  def send_data     render :juggernaut do |page|       page.insert_html :top, 'chat_data', "<li>#{h params[:chat_input]}</li>"     end     render :nothing => true   end end


  <html>     <head>       <%= javascript_include_tag :defaults, :juggernaut %>       <%= juggernaut %>     </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>

Yea, I get that too. It only happens in development (on your local PC). It is for your debugging purposes. Once deployed to the server, these messages are not displayed. (Make sure on the server, the juggernaut config file is set to production mode).