0

I am developing a rails app that allows users to post, much like facebook or stackoverflow. I want to implement an alert system that lets a user know when a new post has been added (just like here at stackoverflow).

Now, I am going to be deploying to heroku. This poses some problems, such as the fact that heroku does not support websockets. If I want websocket support, I am going to have to pay for them via Pusher or Pubnub - expensive services. However, I am not so sure that I need that kind of setup. I might be able to get away with long polling (via an ajax request every so often with a self calling setTimeout).

My question is, at what point do I need to use a push service such as Pubnub/Pusher over just an ajax interval call? Or better yet, how can I get away with an ajax interval call (calling every 30 seconds or minute maybe)? (Is it smarter to just use ajax in the begging anyway, then upgrade to a service if I get flooded with traffic?)

My updates do not need to be real time necessarily, but I would like it as soon as possible.

flyingarmadillo
  • 2,131
  • 5
  • 21
  • 37

2 Answers2

1

PubNub Live Events Notification with Rails on Heroku

You are asking about an easy way to get started with PubNub to build in a Push update (like Stackoverflow) into your web application. You use Heroku and will be sending the update events via Ruby. This is easy to accomplish with PubNub. First you need to access the PubNub Ruby Gem.

gem install pubnub

Next you need to receive updates on your web app via the PubNub JavaScript. I have a starting point for you with some links that I will attach:

Before continuing, it is important to note that the following links will only provide you with a starting point for referencing or morphing the demo into what you will need. Copy the source code (found in Stackoverflow real-time updates answer) and read the reference links to learn how to get started. Also if you need detailed help you can alway contact http://twitter.com/pubnub @PubNub on Twitter.

  1. Stackoverflow Link - Would like to use PubNub to send real-time updates to the user's web browser
  2. General Blog - PubNub setup for Facebook-like notifications and private content

These links provide examples of a Facebook-like Window Box that notifies your user with a custom message via PubNub. You can send updates to your users on their Mobile Phone or Browser. This will show your user a notification; any notification you. Next, if you want to add security – note that PubNub only provides security for Single-User Channels. Group Channels are fundamentally public broadcasts and locking down data in a broadcast mode is not available at this time.

Community
  • 1
  • 1
Stephen Blum
  • 6,498
  • 2
  • 34
  • 46
1

My question is, at what point do I need to use a push service such as Pubnub/Pusher over just an ajax interval call? Or better yet, how can I get away with an ajax interval call (calling every 30 seconds or minute maybe)? (Is it smarter to just use ajax in the begging anyway, then upgrade to a service if I get flooded with traffic?)

By using a service like Heroku it means you are offloading the pain of the web hosting environment. It means that as your service gets popular it will handle the scaling for you. If you use any form of polling you will need to scale the Heroku part of your service sooner as more resources are used up by the polling requests.

Polling can be much less efficient but it really depends on how frequently your users will see updates. If updates are reasonably frequent, so the polling request do actually return data, and realtime doesn't really matter, then polling might be an acceptable solution. But it does mean notifications won't be instant.

Whether you should use a hosted service for the realtime infrastructure part of your app really depends on:

  1. If the realtime nature of the notifications matters, and you want to use WebSockets, then your best solution is to use a hosted service.
  2. The number of users you'll have and the frequency of the updates need to be taken into account when looking at the costs you'll incur. As I said earlier, this will impact how soon you need to add Heroku Dynos to your app. If you do some sums based on your app's likely resource usage you may be able to get a rough idea if it would be more cost efficient to go with polling and pay for more Dynos or offload the realtime infrastructure to a hosted service.

Since I work for Pusher I obviously believe that the benefits of offloading your realtime infrastructure to a WebSocket-focused service are high. The benefits of a strong community, good development tooling and docs should also be taken into account when weighing up pros and cons. If cost is a problem then I know Pusher support is a good place to get in touch and ask about potential discounts/deals.

leggetter
  • 15,248
  • 1
  • 55
  • 61
  • Thank you for this answer. I would use Pusher, but the cost at this point feels pretty high for such little connections. If the service were based solely on messages sent (like the Pubnub heroku add-on), it would be an easy decision. The problem is that I am expecting a lot of users (I need connections), but not as many posts. The only reason I am not going with PubNub right off the bat is b/c their documentation is pretty poor. Thanks again for your thorough answer. I think I will poll to begin, and then move to a service if necessary. – flyingarmadillo Sep 09 '12 at 11:02
  • Oh, I should have mentioned that polling can actually add additional complexity to your code and web app architecture (well, at least more code). Take a look at theses slides which cover the code required for a polling v Push scenario along with the resource usage: http://www.leggetter.co.uk/pusher/pusher-presentations/why_use_pusher.html#9 Although it focuses on Pusher it's valid for other realtime web tech solutions. – leggetter Sep 10 '12 at 07:06