I am writing a rails app with Juggernaut 2 for real-time push notifications and am not sure how to approach this problem. I have a number of users in a chat room and I would like to run a timer so that a push can go out to each browser in the chat room every 30 seconds. Juggernaut 2 is built on node.js, so I'm assuming I need to write this code there. I just have no idea where to start in terms of integrating this with Juggernaut 2.
我正在编写一个rails应用程序,它具有强大的实时推送通知功能,不知道该如何解决这个问题。我在聊天室里有很多用户,我想运行一个计时器,这样每隔30秒就有一个推送到聊天室的每个浏览器。神像2是建立在节点上。假设我需要写这段代码。我只是不知道从哪里开始把这个和强大的2结合起来。
3 个解决方案
#1
1
I just browsed through Juggernaut briefly so take my answer with a grain of salt...
我只是浏览了一下神像,所以对我的答案持保留态度……
- You might be interested in the Channel object (https://github.com/maccman/juggernaut/blob/master/lib/juggernaut/channel.js) You'll notice that Channel.channel is an object (think ruby's hash) of all the channels that exist. 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.
- 您可能对Channel对象(https://github.com/maccman/神像/ blob/master/lib/神像/神像/神像/channel.js)感兴趣,您会注意到这个通道。channel是存在的所有通道的对象(如ruby的散列)。您可以设置一个30秒的重复计时器(setInterval - http://nodejs.org/docs/v0.4.2/api/timers.html#setInterval)来对所有通道执行一些操作。
-
What to do in each loop iteration? Well, the link to the aforementioned Channel code has a publish 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 have to create a Message object with message.channels set to Channel.channels and if you pass that message to the publish method, it will send out to all your clients.
所以你基本上需要创建一个带有消息的消息对象。频道设置为通道。通道,如果您将消息传递给发布方法,它将发送给您的所有客户端。
As to the contents of your message, I dunno what you are using client side (socket.io? a chat client someone already built for you off Juggernaut and socket.io?) so that's up to you.
关于你的留言内容,我不知道你在用什么客户端。一个聊天客户,有人已经为你建立了神像和足球。
As for where to put the code creating the interval and firing off the callback to publish your message to all channels, you might want to check here in the code that creates the actual server listening on the given port: (https://github.com/maccman/juggernaut/blob/master/lib/juggernaut/server.js) If you attach the interval within init(), then as soon as you start the server it will be checking every 30 seconds to publish your given message to every channel
至于把代码创建回调的间隔和解雇所有渠道发布你的信息,你可能会想在这里检查代码中创建实际的服务器侦听给定端口:(https://github.com/maccman/juggernaut/blob/master/lib/juggernaut/server.js)如果你把间隔在init(),然后当你启动服务器将检查每30秒,发布您的信息给每个通道
#2
1
Here is a sample client which pushes every 30 seconds in Ruby.
这是一个在Ruby中每30秒推送一次的客户端示例。
Install your Juggernaut with Redis and Node: install ruby and rubygems, then run gem install juggernaut
and
用Redis和Node安装神像:安装ruby和rubygems,然后运行gem安装神像
#!/usr/bin/env ruby
require "rubygems"
require "juggernaut"
while 1==1
Juggernaut.publish("channel1","some Message")
sleep 30
end
#3
1
We implemented a quiz system which pushed out questions on 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 using 'spawn' to run a background process for the quiz timing so that we could still process the incoming scores.
在我们的例子中,关键是使用“spawn”来运行测试计时的后台进程,以便我们仍然可以处理输入的分数。
I have no idea how scalable this is.
我不知道它有多大的可扩展性。
#1
1
I just browsed through Juggernaut briefly so take my answer with a grain of salt...
我只是浏览了一下神像,所以对我的答案持保留态度……
- You might be interested in the Channel object (https://github.com/maccman/juggernaut/blob/master/lib/juggernaut/channel.js) You'll notice that Channel.channel is an object (think ruby's hash) of all the channels that exist. 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.
- 您可能对Channel对象(https://github.com/maccman/神像/ blob/master/lib/神像/神像/神像/channel.js)感兴趣,您会注意到这个通道。channel是存在的所有通道的对象(如ruby的散列)。您可以设置一个30秒的重复计时器(setInterval - http://nodejs.org/docs/v0.4.2/api/timers.html#setInterval)来对所有通道执行一些操作。
-
What to do in each loop iteration? Well, the link to the aforementioned Channel code has a publish 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 have to create a Message object with message.channels set to Channel.channels and if you pass that message to the publish method, it will send out to all your clients.
所以你基本上需要创建一个带有消息的消息对象。频道设置为通道。通道,如果您将消息传递给发布方法,它将发送给您的所有客户端。
As to the contents of your message, I dunno what you are using client side (socket.io? a chat client someone already built for you off Juggernaut and socket.io?) so that's up to you.
关于你的留言内容,我不知道你在用什么客户端。一个聊天客户,有人已经为你建立了神像和足球。
As for where to put the code creating the interval and firing off the callback to publish your message to all channels, you might want to check here in the code that creates the actual server listening on the given port: (https://github.com/maccman/juggernaut/blob/master/lib/juggernaut/server.js) If you attach the interval within init(), then as soon as you start the server it will be checking every 30 seconds to publish your given message to every channel
至于把代码创建回调的间隔和解雇所有渠道发布你的信息,你可能会想在这里检查代码中创建实际的服务器侦听给定端口:(https://github.com/maccman/juggernaut/blob/master/lib/juggernaut/server.js)如果你把间隔在init(),然后当你启动服务器将检查每30秒,发布您的信息给每个通道
#2
1
Here is a sample client which pushes every 30 seconds in Ruby.
这是一个在Ruby中每30秒推送一次的客户端示例。
Install your Juggernaut with Redis and Node: install ruby and rubygems, then run gem install juggernaut
and
用Redis和Node安装神像:安装ruby和rubygems,然后运行gem安装神像
#!/usr/bin/env ruby
require "rubygems"
require "juggernaut"
while 1==1
Juggernaut.publish("channel1","some Message")
sleep 30
end
#3
1
We implemented a quiz system which pushed out questions on 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 using 'spawn' to run a background process for the quiz timing so that we could still process the incoming scores.
在我们的例子中,关键是使用“spawn”来运行测试计时的后台进程,以便我们仍然可以处理输入的分数。
I have no idea how scalable this is.
我不知道它有多大的可扩展性。