WebSockets and Heroku: pushing data to the user - ruby-on-rails

I am build a real time web application using Ruby on Rails and Heroku seems to be the best option for hosting it.
I would prefer pushing new data to the user, when it becomes available, instead of pulling it by sending AJAX requests every few seconds.
Pusher seems to be suggested by Heroku for a such kind of job, but it has some limitations, brings additional costs, and makes you dependent on an external API.
Is there any other option to use WebSockets on Heroku?

If you wanna do websockets you need to use a different server besides rails. And since your on heroku you don't have the flexibility.
Optionally you can host a websockets enabled node server on an ec2 micro instance. Then in your rails app when you want to push -- do a request to the node server and the it will go to the clients.

If you only need one way communication try using Server Side events.

There are two options that you can use with Heroku aside from Pusher:
1) Like Eric Fode mentioned above, you could use SSE given you don't need two-way communication. There is a Heroku add-on which you could leverage for this: https://addons.heroku.com/eshq
2) There is another lesser known library called Faye which you could look into. It supports both Node.js and Ruby servers and it's based on the Bayeux Protocol

Heroku now has support for WebSockets: https://devcenter.heroku.com/articles/heroku-labs-websockets

While WebSockets isn't currently supported, apparently you can use Socket.io on Heroku, configuring it for long-polling.

Related

What should I use to connect a rails app with remote servers?

I am using a ruby on rails app which goal is to handle lockers reservations by users and be able to unlock them using a smartphone.
The rails app is hosted on heroku (free plan), and I use several raspberry pi as remote servers that control opening of lockers.
So far, it works with HTTP requests from the app to the raspberry through a free http ngrok tunnel, but this is limited to a few requests per minute, and the connection is not secure/private at all, so it is not really what I need.
What type of solution would you recommend to ensure a more secure connection between the app and remote servers with no request number limitation ? I have a few concepts in mind, but I don't understand them well enough to know what I should look into. VPN ? SSH ? Web sockets ? Should I still use heroku ?
Thanks a lot guys!
You could achieve what you want with all technologies you listed above but I think using web sockets would simplify the setup process and it also seems to be the most robust.
Have a look at this client for your Raspberries.
To get the server to work you'll need a redis instance, which heroku offers in their free plan so you can keep it there. Your server app will need to use something like ActiveCable or faye.

How to use Rails as DDP server with Meteor.js client

We have a Rails app that acts HTTP API only. On the client side, Ember.js is currently used. We are not overly impressed by Ember and really like the approach Meteor.js takes. So we'd like to exchange the client side with Meteor.js and communicate with the Rails server via websockets that speak the Data Distribution Protocol (DDP), so we can keep using the models, mailers and controllers in Rails. Implementing server side of DDP should be easy.
However, we're unsure how to make Rails talk websockets. We found Reel, which seems to make it easy to accept websocket requests in a standalone environment. Reel seems great as we'd like to implement the DDP on top of the Celluloid stack anyway. But what about running Reel in the Rails environment? Would we need "rails runner" for that? And we'd like to keep using the existing controllers to dispatch incoming requests (like, to add/change/remove resources). Is that even possible without having the request coming through Rack?
Any input is appreciated.
It's a bit late, but I've implemented DDP in Ruby, you can check it out here:
https://github.com/d-snp/ruby-ddp-server
It includes an implementation of EJSON as well. It's built on top of celluloid-websocket, and can be ran simply as a rack app.
I've made an integration with RethinkDB that can be used as a reference to build your own collections implementation.
https://github.com/d-snp/ruby-ddp-server-rethinkdb
I've also made a sample chat application that can be found here:
https://github.com/d-snp/celluloid-rethinkdb-chat
It's something that I have been longing to do as well, to integrate old "legacy" Rails code. Here is the best way I have found:
Since you would not be using any of Rails router/controller/views, but just the ability to read data and push it to the client, I recommend you use Rails to create JSON apis to the database, and deploy the code, then in Meteor you can consume the data via the http package, this would happen on the server at a regular interval and populate the MongoDB with the normalized data you need, then it would serve the browser client.
I am working on such an application that will keep a normalized version of the data in Mongo, and a relational version of the data in mySql (through Rails) this way I can preserve the legacy Rails functionality that I dont want to rewrite in JS, and get the benefit of Meteor for the one page that I need it most.

Websocket and Rails integration

I am new to the web socket framework with Rails and generally I feel I should get my hands to start working with it. I have used Faye for my private message publishing and it works nicely. So I want to ask if there is a nice tutorial out there that can show me how to use web sockets with rails. Thank you.
Node.js + socket.io is probably your easiest option right now. Faye is set up to use Node.js, so you may have already have Node.js running alongside your rails app (depending on how you're using Faye). You'll just want to start using socket.io. The socket.io repo includes some very useful examples - I sugget looking at chat.
You can communicate between your rails app and Node.js server via http. Node.js lets you easily make an http server. You may also want to take a look at request.

Push data from rails app to clients

I'm working on a rails app that will primarily be exposed by an api to various mobile clients (iOS, android etc). The application involves users submitting data to the server (via api calls), but what I want to include is the ability to push this data down to other clients. The general concept is similar to a messaging app, where I submit a message to the server from me client and the receiver is pushed the message from the server.
The only method I know of at the moment is to constantly poll the server, but there must be better tech solutions than this. Any ideas?
I would look at using a websocket within the page to push the updates.
You could implement this using Faye, which falls back to long polling and other work-arounds for browsers without websocket support. Faye has a pure-ruby implementation, so you could probably work out access to your model layer.
Edit:
Also, this is a project that combines Faye with Rails. It is fairly new, but might do what you want. Faye-Rails
You should check out http://www.pusher.com
Pusher is a hosted API for quickly, easily and securely adding scalable realtime functionality to web and mobile apps.
If you need self-hosted solution, then you should check out slanger gem https://github.com/stevegraham/slanger which is server implementation for pusher client libraries. When you feel you need hosted solution, you just change URL's.
Slanger is an open source server implementation of the Pusher protocol written in Ruby. It is designed to scale horizontally across N nodes and to be agnostic as to which Slanger node a subscriber is connected to, i.e subscribers to the same channel are NOT required to be connected to the same Slanger node. Multiple Slanger nodes can sit behind a load balancer with no special configuration. In essence it was designed to be very easy to scale.
Ruby has it's own event-processing library, implemented like a gem:
https://github.com/eventmachine/eventmachine
Maybe it helps you
I prefer event machine over any other solution. It is somewhat more complicated that faye but you can write way more sophisticated code using event machine.
You might wanna check this peepcode screencast on event machine

How can I retrieve updated records in real-time? (push notifications?)

I'm trying to create a ruby on rails ecommerce application, where potential customers will be able to place an order and the store owner will be able to receive the order in real-time.
The finalized order will be recorded into the database (at the moment SQLite), and the storeowner will have a browser window open, where the new orders will appear just after the order is finalized.
(Application info: I'm using the HOBO rails framework, and planning to host the app in Heroku)
I'm now considering the best technology to implement this, as the application is expected to have a lot of users sending in a lot of orders:
1) Each browser window refreshes the page every X minutes, polling the server continuously for new records (new orders). Of course, this puts a heavy load on the server.
2) As above, but poll the server with some kind of AJAX framework.
3) Use some kind of server push technology, like 'comet' asynchronous messaging. Found Juggernaut, only problem is that it is using Flash and custom ports, and this could be a problem as my app should be accessible behind corporate firewalls and NAT.
4) I'm also checking node.js framework, seems to be efficient for this kind of asynchronous messaging, though it is not supported in Heroku.
Which is the most efficient way to implement this kind of functionality? Is there perhaps another method that I have not thought of?
Thank you for your time and help!
Node.js would probably be a nice fit - it's fast, loves realtime and has great comet support. Only downside is that you are introducing another technology into your solution. It's pretty fun to program in tho and a lot of the libraries have been inspired by rails and sinatra.
I know heroku has been running a node.js beta for a while and people were using it as part of the recent nodeknockout competition. See this blog post. If that's not an option, you could definitely host it elsewhere. If you host it at heroku, you might be able to proxy requests. Otherwise, you could happily run it off a sub domain so you can share cookies.
Also checkout socket.io. It does a great job of choosing the best way to do comet based on the browser's capabilities.
To share data between node and rails, you could share cookies and then store the session data in your database where both applications can get to it. A more involved architecture might involve using Redis to publish messages between them. Or you might be able to get away with passing everything you need in the http requests.
In HTTP, requests can only come from the client. Thus the best options are what you already mentioned (polling and HTTP streaming).
Polling is the easier to implement option; it will use quite a bit of bandwidth though. That's why you should keep the requests and responses as small as possible, so you should definitely use XHR (Ajax) for this.
Your other option is HTTP streaming (Comet); it will require more work on the set up, but you might find it worth the effort. You can give Realtime on Rails a shot. For more information and tips on how to reduce bandwidth usage, see:
http://ajaxpatterns.org/Periodic_Refresh
http://ajaxpatterns.org/HTTP_Streaming
Actually, if you have your storeowner run Chrome (other browsers will follow soon), you can use WebSockets (just for the storeowner's notification though), which allows you to have a constant connection open, and you can send data to the browser without the browser requesting anything.
There are a few websocket libraries for node.js, but i believe you can do it easily yourself using just a regular tcp connection.

Resources