Ruby on Rails PubNub subscribe as separate process

社会主义新天地 提交于 2019-12-08 01:35:56

问题


I was consider using PubNub on my website.

In turn, I can subscribe to channels with PubNub.

However, I need to find a way to run a script that subscribes to channels with PubNub.

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."

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?

Appreciate any guidance.


回答1:


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.

  1. EventMachine::Protocols::HttpClient2 -> http://eventmachine.rubyforge.org/EventMachine/Protocols/HttpClient2.html
  2. 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')
    

Note that this is not a full example, but a good starting point... You must follow the HTTP REST Guide.



来源:https://stackoverflow.com/questions/8395198/ruby-on-rails-pubnub-subscribe-as-separate-process

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!