Server Timers with Juggernaut 2

I am writing a rails application with Juggernaut 2 for real-time alerts and do not know how to approach this problem. I have several users in the chat room, and I would like to start a timer to click every browser in the chat every 30 seconds. Juggernaut 2 is built on node.js, so I guess I need to write this code there. I just don't know where to start in terms of integrating this with Juggernaut 2.

+3
source share
3 answers

I just looked at the Juggernaut for a while, so answer my answer with salt ...

  • You might be interested in the Channel object (https://github.com/maccman/juggernaut/blob/master/lib/juggernaut/channel.js). You will notice that Channel.channel is the object (think ruby ​​hash) of all existing channels. You can set a 30 second recurring timer (setInterval - http://nodejs.org/docs/v0.4.2/api/timers.html#setInterval ) to do something with all your channels.
  • What to do in each iteration of the loop? Well, the link to the above channel code has a publishing method:

    publish: function(message){
    var channels = message.getChannels();
    delete message.channels;
    
    for(var i=0, len = channels.length; i < len; i++) {
      message.channel = channels[i];
      var clients     = this.find(channels[i]).clients;
    
      for(var x=0, len2 = clients.length; x < len2; x++) {
        clients[x].write(message);
        }
      }  
    

    }

So, you basically need to create a Message object with the message.channels channels installed on Channel.channels, and if you pass this message to the publish method, it will send all your clients.

, , (socket.io? , Juggernaut socket.io?), .

, , , , , , , , : (https://github.com/maccman/juggernaut/blob/master/lib/juggernaut/server.js) init(), , , 30 ,

+1

, 30 Ruby.

Juggernaut Redis Node: ruby ​​ rubygems, gem install juggernaut

#!/usr/bin/env ruby
require "rubygems"
require "juggernaut"
while 1==1
 Juggernaut.publish("channel1","some Message")
 sleep 30
end
+1

We implemented a quiz system that put questions to a variable time interval. We did it as follows:

def start_quiz
  Rails.logger.info("*** Quiz starting at #{Time.now}")
  $redis.flushall  # Clear all scores from database

  quiz = Quiz.find(params[:quizz] || 1 )
  @quiz_master = quiz.user  
  quiz_questions = quiz.quiz_questions.order("question_no ASC")

  spawn_block do 
    quiz_questions.each { |q|
      Rails.logger.info("*** Publishing question #{q.question_no}.")
      time_alloc = q.question_time
      Juggernaut.publish( select_channel("/quiz_stream"), {:q_num => q.num, :q_txt => q.text :time=> time_alloc} ) 
      sleep(time_alloc)             
      scoreboard = publish_scoreboard
      Juggernaut.publish( select_channel("/scoreboard"), {:scoreboard => scoreboard} ) 
    }
  end

  respond_to do |format|
    format.all { render :nothing => true, :status => 200 }
  end
end

The key in our case was to use "spawn" to start the background process to determine the time of the quiz so that we could process incoming points.

I have no idea how scalable it is.

+1
source

All Articles