26

I need to add realtime notifications to my Rails app. Here are the different possible architectures I have found.

1. Rails + Socket.io + Redis: As suggested in this post, having the following architecture:

Rails + Socket.io + Redis

  • Pro: Clean, no data is lost if Socket.io is down
  • Con: Introduces several technologies (hardens maintainability)

2. Rails + Socket.io: Making Rails a Socket.io client, as this repo seems to do: https://github.com/lyondhill/socket.io-ruby-client

Rails + Socket.io

  • Pro: Straightforward
  • Con: Relying on a unpopular library

3. Ruby Faye: http://faye.jcoglan.com/ruby.html

  • Pro: 100% Ruby
  • Con: Since an external app is required, Socket.io is much more an industry standard than Faye right now.

4. ActionController::Live: http://edgeapi.rubyonrails.org/classes/ActionController/Live.html

  • Pro: The Rails in app way
  • Con: Too immature

Questions:

  • Is there a standard way (I'd have missed) to do that nowadays?
  • Any thoughts on my comparison (hope this doesn't get closed)?
Alex Antonov
  • 14,134
  • 7
  • 65
  • 142
Augustin Riedinger
  • 20,909
  • 29
  • 133
  • 206

3 Answers3

4

Take a look at eventmachine and websockets. There are also third party services such as Pusher and PubNub that will handle the websocket part for you via HTTP API.

https://github.com/igrigorik/em-websocket

Rails 5 will also be adding ActionCable which will do this in rails, but it's not out yet.

I would say the advantage to these approaches is that you don't need a separate node.js app. The services are very easy to use but not free.

dinomix
  • 956
  • 4
  • 5
2

You should definitely take a look on https://github.com/rails/actioncable

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 ActiveRecord or your ORM of choice.

0

Take a look at Plezi (It's my own pet project, so I might be biased).

Although it can also be used as an independent framework, it's easy to set it up either as a Rails add-on or as a socket.io replacement with Redis.

You can write a Plezi websocket controller and have it run together with your your Rails app on the same process (same ip:port) if you switch your server to Plezi's preferred server (just remove references to other servers from your gemfile).

Another alternative is to run Plezi as an independent process (on a different port) and sync them using Redis (using Plezi's Placebo API).

There's a section in Plezi's documentation titled: "Using Plezi with our existing Rack application"

Pro: Uses native websocket implementations (server side websockets) and a fast C extension server (Iodine). Total integration with your Rails app is possible (serve both websockets and HTTP on the same domain/process/ip/port); Easy scaling (auto-redis support).

Con: Young; Requires C extension support (Ruby MRI) and Linux / BSD / OS X machine (no windows); Shared code needs to be thread-safe (websockets might be running in parallel with HTTP or other connections).

Good Luck!

Myst
  • 18,516
  • 2
  • 45
  • 67