3

I'm using the pubnub example sample chat app here: https://github.com/pubnub/ruby/tree/master/examples/pubnub_livestream

It seems to work ok, but I'm not understanding 2 things:

1) The javascript/coffeescript file here: https://github.com/pubnub/ruby/blob/master/examples/pubnub_livestream/app/assets/javascripts/streamer.js.coffee

seems to poll to get_messages every 500 ms (see the setTimeout in the worker function) - I thought the whole point of pubnub is to avoid polling. The get_messages action in my Rails controller is being called repeatedly. Could someone explain this to me?

2) There is a global callback here: https://github.com/pubnub/ruby/blob/master/examples/pubnub_livestream/config/initializers/pubnub.rb

which receives events from pubnub and creates the Message records. This seems like a strange way to do this - a global callback does not seem to be the right place to receive messages/events from pubnub. I'd like to create records upon receiving notification in my controller or even client side via a javascript event - then I can post to a controller, maybe.

Thanks.

3
  • 1): as you said, every 500 ms your Rails server is hit by your client(s), polling to get_messages This is why your action is called that often ; 2): I don't understand your question (is there any?)
    – MrYoshiji
    Jun 10, 2014 at 18:45
  • 1) I thought the whole point of using pubnub or pusher or any real time provider was to avoid polling. My question is: why does their example use polling to get messages?
    – Jim Jones
    Jun 10, 2014 at 18:47
  • 2) Why are they using a global callback to create records? Shouldn't the callback be client side, so I can respond to it?
    – Jim Jones
    Jun 10, 2014 at 18:49

1 Answer 1

3

@jimjones You can put the subscribe callback anywhere, its up to you. This is just intended as a quick sample implementation. In theory, theres nothing stopping it from going into a model (including singleton), view, or controller, except for MVC principles :)

Regarding the local polling, its just hitting the local db every 500ms. It could easily be converted into an onClick call to hit the local DB, or we can circumvent the local DB altogether, by instead implementing a PubNub history call.

2
  • Regarding the subscribe callback - ok that makes sense. Regarding the polling. When a user visits the chat page - instead of polling for messages from the db - how do i have a client side js event raised that receives a message (what another user typed in the chat box for example) from pubnub?
    – Jim Jones
    Jun 10, 2014 at 19:48
  • @JimJones you'd embed the JS shown in the code sample here, thats the fastest way: pubnub.com/docs/javascript/javascript-sdk.html -- but follow the Data Stream tutorial on the same page for the step-by-step.
    – Geremy
    Jun 10, 2014 at 20:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.