Reputation: 22170
I need to add realtime notifications to my Rails app. Here are the different possible architectures I have found.
Questions:
Upvotes: 26
Views: 3701
Reputation: 19221
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!
Upvotes: 0
Reputation: 41
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.
Upvotes: 2
Reputation: 976
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.
Upvotes: 4