问题描述
我曾考虑在我的网站上使用 PubNub.
I was consider using PubNub on my website.
反过来,我可以通过 PubNub 订阅频道.
In turn, I can subscribe to channels with PubNub.
但是,我需要找到一种方法来运行使用 PubNub 订阅频道的脚本.
However, I need to find a way to run a script that subscribes to channels with PubNub.
例如,根据他们的文档 http://www.pubnub.com/blog/ruby-push-api ,它指出要侦听已发布的消息,请调用 subscribe 函数.重要的是要注意:subscribe 函数是阻塞的.您需要在单独的进程中运行此函数."
For example, according to their documentation http://www.pubnub.com/blog/ruby-push-api , it states "To listen for published messages, call the subscribe function. It is important to note: the subscribe function is blocking. You will want to run this function in a separate process."
然后PubNub提供如下代码:
Then PubNub provides the following code:
## Subscribe (Listen for Messages)
pubnub.subscribe({
'channel ' => 'my_channel',
'callback' => lambda do |message|
## Message Received!!!
puts(message['my_var']) ## print message
return true ## keep listening?
end
})
我想不出将这个函数作为进程"运行的方法.
I cannot think of a way to run this function as a "process."
难道没有类似后台工作的东西吗?这是我需要的吗?
Isn't there something like background jobs? Is this what I would need?
感谢任何指导.
推荐答案
开始使用此(此时)的最佳方法是使用 Ruby EventMachine 异步 HTTP 请求方法和 PubNub HTTP REST API.
The best way to get started with this (at this time) is to use the Ruby EventMachine async HTTP request method and the PubNub HTTP REST API.
- EventMachine::Protocols::HttpClient2-> http://eventmachine.rubyforge.org/EventMachine/Protocols/HttpClient2.html一个>
PubNub HTTP REST API-> http://www.pubnub.com/tutorial/http-rest-push-api
conn = EM::Protocols::HttpClient2.connect 'pubsub.pubnub.com', 80
req = conn.get('/subscribe/SUBSCRIBE_KEY/CHANNEL/0/TIMETOKEN')
请注意,这不是一个完整的示例,而是一个很好的起点……您必须遵循 HTTP REST 指南.
Note that this is not a full example, but a good starting point... You must follow the HTTP REST Guide.
这篇关于Ruby on Rails PubNub 作为单独的进程订阅的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!