ruby-on-rails-4websocketreal-timefaye

Confusion over choosing Faye or Rails 4 Actioncontroller::Live


I have already used Faye with Ruby On Rails, it's almost at 0 cost for me, because I'm running Faye over another server connected to my Rails App.

However I have faced some problems like when a query takes too long on the Rails server, after a while the Faye Connection would fail and raise an exception.

Now what I'm looking into the Actioncontroller::Live , most of the implementations are using Redis, which will be a bit costy for my startup, however I realized I can't do subscribe/publish style things with the Actioncontroller::Live.

My question: should I move over to Actioncontroller::Live or stick to Faye ? While these are the things that I want to accomplish:

  1. Updates after commenting/feed
  2. Notification system, based on pub/sub, similar to Faye.
  3. Exception handling
  4. Scalability > More users more connections

I know that Faye uses Bayeux vs ActionController::live uses SSE/ HTTP. Should I consider anything related to Socket.IO ? SockJS ?

I have already read through some of the question about this topic on here like: Replace Faye with rails 4 server side events? Faye VS rails 4 streaming? But I need more info:


Solution

  • Here's some notes on why I would stick with Faye, which might bring you closer to an answer on this question:

    Browser compatibility

    As you read in the related stackoverflow question, Faye has better browser compatibility.

    Stability

    Rails::Live functionality doesn't seem to be very stable yet. There's currently active development on Rails SSE. As an example, it's quite unlikely that you won't be affected by this issue.

    Threading & blocking vs asynchronous non-blocking

    Do you use multi-threading in your application? If you don't, I definitely wouldn't introduce it just for Rails::Live as it opens up the possibility of non-threadsafe gem issues & limitations of server choices.

    If you do have multi-threading, each client will keep a thread open to your application. If you run out of threads your application will be unresponsive/dead. Consider how many threads you need to cater for peak times with users having multiple browser tabs open, or even DOS attacks where someone opens up a huge amount of idle SSE/websocket connections to reach your max and take your app down. If you set a high amount of max threads to support many idle connections, you open up the possibility of having that many non-idle threads which could have it's own problems. No SSE/websockets and no comet/long polling is much safer for blocking apps. From what I understand, your setup runs Faye separately. The Faye server runs Ruby EventMachine or Node.js which are both asynchronous non-blocking and do not use a thread for each open connection. It can handle a huge amount of concurrent connections without problems.

    My opinion is that a normal blocking Rails web application with a separate asynchronous non-blocking server for connections that stay open (to pass messages & make the app live) is the best of both setup. This is what you have with Rails + Faye.

    Update: Actioncable was announced at Railsconf 2015. It runs non-blocking as described above, but it's an integrated official Rails solution. Having a single framework with a massive community, an integrated non-blocking connection handler for websockets that you can run and configure separately while everything works "out of the box" is a big advantage of Rails.

    From Action Cable readme: Action Cable is powered by a combination of EventMachine and threads. The framework plumbing needed for connection handling is handled in the EventMachine loop, but the actual channel, user-specified, work is handled in a normal Ruby thread. This means you can use all your regular Rails models with no problem, as long as you haven't committed any thread-safety sins.

    To learn more you can read up on ActionCable & Underlying architecture.