2

I'm building a simple real time chat using Juggernaut, Redis, SQLite and Rails 3.1

I want to write a new message to every user when another has been disconnected (for instance he closed the window), this is to listen to the Juggernaut's client disconnected event.

Juggernaut docs says I can do this in the server side (Ruby)

Juggernaut.subscribe do |event, data|
  # Use event/data
end

Problem is that I don't know where I should put this code inside my Rails app (controller, model, observer?). I've tried to placed it into the model, however the server doesn't response to any request with that chunk of code into the model.

I think I should listen to that event from the server side because if the user was disconnected because he closed the window then I don't have a "client side" for that user.

Probably I'm missing something about how Juggernaut works. Any help will be appreciated.

jävi
  • 4,571
  • 1
  • 24
  • 32
  • they are code usage example here : https://github.com/maccman/holla/blob/original/app/models/roster.rb – Awea Aug 05 '11 at 12:14
  • Yes, i've seen that before, however that doesn't seem to be a normal rails model. That model is inherited from SuperModel::Base and not from ActiveRecord::Base. – jävi Aug 05 '11 at 12:18
  • https://github.com/maccman/supermodel, it's not totaly a normal rails project, he replace ActiveRecord by SuperModel – Awea Aug 05 '11 at 12:20
  • Yes, I know that project, it's from the Juggernaut's author. But I need to listen to the event from a normal Rails app (not using that SuperModel) – jävi Aug 05 '11 at 12:21
  • Mouarf may be using observer or something like this ... i did not know Juggernaut it's very cool :) – Awea Aug 05 '11 at 12:27
  • I've tried to do something like the model you mentioned above, using _class << self_ before calling the subscriber, but the server freezes and it doesn't get any request :( – jävi Aug 05 '11 at 12:34
  • what did you get inside your log ? – Awea Aug 05 '11 at 12:36
  • @jävi let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/2158/discussion-between-awea-and-javi) – Awea Aug 05 '11 at 12:36

1 Answers1

6

Ok, finally I'm answering myself:

I've found problem is that when the process running calls Juggernaut.subscribe it freezes until a Juggernaut event is triggered. Therefore you can't call Juggernaut from the server process, you need a new process to run that code.

My code now looks like this: Model:

class MyModel < ActiveRecord::Base

  class << self
    def subscribe
      Juggernaut.subscribe do |event, data|
        case event
          when :subscribe
            # do something
          when :unsubscribe
            # do something else
        end
      end
    end
  end

end

And then I have a ruby script myapp/scripts/juggernaut_listener:

#!/usr/bin/env ruby
require File.expand_path('../../config/environment',  __FILE__)

puts "Starting juggernaut listener"
MyModel.subscribe

So after lunching the server I need to lunch the Juggernaut listener like this:

./script/participations_listener

(Note you should give +x to the script).

Hope it's helpful to someone!.

jävi
  • 4,571
  • 1
  • 24
  • 32