Is Action Cable the same thing as Faye? - ruby-on-rails

Implementing Web Sockets in my app I've become confused which gem is better. I found plenty of different opportunities, however, some are hard to distinct.
Finally, I've chosen Action Cable (a Rails 5 native part) and Faye (appeared earlier and became very popular).
But now I'm stuck - is Action Cable the same thing as Faye? Which are differences (if there's any)?

From the Faye website: Faye is a publish-subscribe messaging system based on the Bayeux protocol. It provides message servers for Node.js and Ruby, and clients for use on the server and in all major web browsers.
From the ActionCable readme: Action Cable seamlessly integrates WebSockets with the rest of your Rails application. It allows for real-time features to be written in Ruby in the same style and form as the rest of your Rails application, while still being performant and scalable. It's a full-stack offering that provides both a client-side JavaScript framework and a server-side Ruby framework. You have access to your full domain model written with Active Record or your ORM of choice.
Short answer is YES, both are pub/sub messaging system.
Long answer is NO, because faye is a low-level tool and ActionCable uses faye (look here), al least some components.
But you always can get the same results using (maybe) different efforts building an application using faye or ActionCable. The big difference is Faye works as a rack-based component instead of rails-based component.

Related

Websockets and Rails

I want to develop a web file manager based on Rails 4.2 (Ruby 2.1.0) with websockets.
Websocket-rails seems nice, but is dead.
em-websocket lacks documentation (or if you have a link it will be great) and is not fully open source compliant.
What is the best way to use websocket with rails?
Take a look at faye-websocket. Here is a nice railscasts tutorial.
Take a look at the Plezi framework.
The advantage over Faye is that Faye requires you to handle your Redis broadcasting logic yourself, whereas Plezi is a framework, which handles the Redis logic for you and lets you run both your Plezi websockets app and your Rails app on the same port on the same server.
Plezi is also easily scalable when using Redis, as it can run all it's broadcasting and unicasting API through Redis and you don't need to do anything except point it to your Redis server.
As stated in the documentation, You just include your Plezi code in your Rails app as middleware. Easy.

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.

What is the Ruby equivalent of Node.js' socket.io?

If I wanted to translate my node application that uses socket.io into a Ruby on Rails application what are the options for replacing socket.io? (Essentially looking for a socket server for Ruby)
http://socket.io/
Plan to translate the application below:
http://www.tokbox.com/blog/creating-chat-roulette-with-node-js-socket-io-and-opentok/
I'd recommend the Faye Ruby implementation as a solid server-side realtime component. It's not a direct port of socket.io but provides you with the realtime infrastructure and some well define messaging concepts that will help you port most realtime applications.
You can find more options via the realtime web tech guide.
Did you look at the Plezi framework?
You can use it either as a separate framework or to augment Rails/Sinatra by adding websocket functionality.
It runs using the Iodine server and supports native websockets, RESTful routes and HTTP streaming, so it's easy to have a fallback position such as long-pulling, much like socket.io does when web sockets don't work.
It's interesting and easy to develop with and has native support for Redis, so it allows websocket broadcasts between processes and machines... although it's still under development, it's full of potential.
A broadcast/echo WebSocket app can look like this:
require 'plezi'
class Echo
def index
"this is an echo server - use websockets to connect. test with: https://www.websocket.org/echo.html"
end
def on_message data
_echo data
broadcast :_echo, data
end
def _echo data
response << data
end
end
Plezi.route '/', Echo
You can actually put the code in the irb console and the server will start the moment you exit irb using the exit command.
I highly recommend Pubnub, it has many wrappers including ruby.
Documentation is really easy to follow and they have many tutorials.
I have used Pubnub on many rails projects, including raspberry pie projects.
Rails 5 now has ActionCable built in, which means websockets are now standard with Rails!

What's the preferred way of using Rails and Flex?

I'd like to create a service, similar to a chat application (realtime app).
From what I can see from my research, BlazeDS is the preferred way, but it involves Java and Java EE. Also, the latest Ruby results seem to be from 2009-2010, so they are likely outdated.
Are there any actively maintained Ruby/Rails solutions for integrating push notifications with Flex?
The current situation
Do you have to use Flex? HTML5 websockets is a nascent but growing technology and there are implementations now. Flex's days are numbered. Yet, websockets doesn't yet have native support in IE.
em-websocket is a ruby websocket server based on eventmachine. The service Pusher is based on it.
Faye is a websocket server and client. (Railscast)
There are other Ruby implementations such as Cramp and Socky.
If you're open to servers in other technologies such as Node, there are many with Ruby or Javascript clients ready to go.
Update: I might mention that I looked into doing something similar with Flex a while back, and got a copy of Flex on Rails. The book's server push example uses Juggernaut, which unfortunately has stopped further development. The author states that Server-Sent Events (SSEs) make Juggernaut redundant. All major browsers except IE support them natively, similar to the situation with websockets.
There are shims ("polyfills") that use javascript to bring these missing capabilities to the browsers. For example, the jQuery Graceful WebSocket is a jQuery plugin that implements a websocket client but falls back to AJAX polling so the functionality will still work in IE, just won't be quite as instant. Because it detects websocket support, as soon as a browser supports websockets they will be used.
Bridging the Gap
We seem to be caught in a transition period, where we are at the sunset era of Flash but not yet at broad support for its replacement technologies. There is one library that may bridge the gap: Socket.IO. This library selects the most capable technology transport at runtime. It will use Flash if present, but can also use websockets, AJAX long polling, AJAX multipart streaming, a "forever iframe" if necessary. This gives it broad browser support:
IE 5.5+
Safari 3+
Google Chrome 4+
Firefox 3+
Opera 10.61+
iPhone Safari
iPad Safari
Android Webkit
WebOS Webkit
This is actually broader compatibility than either Flash/Flex or WebSockets alone. Socket.IO is implemented in Javascript for both server and client, so you need a server-side Javascript runtime such as Node.
Possible solutions
While there don't seem to be many current references to a Rails 3 -> Flex solution (as you have found), it appears there is some traction with the combination of Ruby/Rails and Socket.IO.
If you want to add chat to a Rails app using Socket.IO, there's a nice reference blog post by Liam Kaufman who creates a chat app in Rails 3 using Socket.IO: http://liamkaufman.com/blog/2012/02/25/adding_real-time_to_rails_with_socket.IO_nodejs_and_backbonejs_with_demo/
There's also a socket.io gem which adds support to the Cramp server mentioned above.
There also seem to be other stackoverflow questions with others working on the Rails 3 and Socket.IO combination.
TL;DR summary
While there isn't much indication that folks are doing direct-to-Flex from Rails anymore, there are other solutions with the most promising being a combination of Rails and Socket.IO.
If you want to live within the Ruby world, you can use regular WebSockets to talk to a Flex application. It won't be pretty, but it would work, and you could avoid the Java back-end. This would be a lot more raw than telling BlazeDS to fling structures around, but it should be doable.
On the Flex client side, there is a library written by Kaazing, that is bundled with their WebSocket servers. Download one of their WebSocket servers, and in the client-libs folder, there should be a swc (with docs) that you can use to talk to em-websocket (or really, any websocket tech).
Now, all this being said, you won't have nearly the scaleability of BlazeDS or GraniteDS, but it should work for smaller implementations and demos.
You can use https://github.com/rubyamf/rubyamf or https://github.com/victorcoder/rubyamf_plugin
But you will be have problem with realtime messaging because rubyamf and rubyamf_plugin don't support RTMP.
You can use the RestfulX gem & Flex framework. That's what I use for Rails/Flex.

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

Resources