Recently, I was faced with the need to implement a small chat in one of the Rails projects. There are many ways to solve this problem, ranging from periodic server polls using AJAX and ending with the use of off-the-shelf solutions from third-party services. In order to minimize the traffic and response time of the system, it was decided to use the WebSocket API - one of the parts of the HTML 5 standard that allows the server to initiate data transfer without waiting for a client request. Unfortunately, the WebSocket API is supported by a fairly limited number of browsers, including Google Chrome, Safari 5, Mobile Safari in iOS 4.2 and higher, and perhaps everything (if I am mistaken, correct me in the comments). Of course, this is not enough for a combat application, so you need to offer a backup option for browsers without native support for the WebSocket API. The most convenient way is to use Flash Sockets. Using the WebSocket API and Flash Sockets, the application covers most of the possible cases, including both desktop systems (MS Windows, Linux, Mac OS X) and mobile (Android, iOS). Let's move from theory to practice, let's talk about what tools allow using WebSocket API in a Rails application. There are several possible options, among which I will note the following:
Juggernaut ( http://juggernaut.rubyforge.org/ ) is a solution originally written in Ruby, and then rewritten to node.js.Redis is used to store data, there is a gem for tight integration with a Rails application. Juggernaut offers a fairly large number of ways to implement and emulate the WebSocket API, for example, if neither the native WebSockets support nor the ability to load a Flash object are available in the browser, then the necessary functionality is emulated using periodic AJAX requests, which of course increases the response time of the system and increases the load to the server, but as they say, bezrybe and cancer fish. Juggernaut minuses in my situation were: the need to install redis and node.js on the server, which I could still accept, but the bug due to which FF did not work Flash fallback was just the last straw, after which I went to look for alternatives to Juggernaut.
pusher.com is a service that takes care of all the work done with the WebSockets API and allows you to send messages to clients using the REST interface. The strengths of pusher.com are scalability and the ability to analyze user interaction with an online resource. Unfortunately, in my case, the use of an external service was difficult, so pusher.com had to be abandoned.
Socky-server ( https://github.com/socky/socky-server-ruby ) is a server written in Ruby based on EM-WebSocket and EM-HTTP-Client, which supports WebSockets API and Flash fallback. Rails integration is done using gem socky-client-rails . There is support for rooms, authorization and sending a message to a specific user. What I really liked about this solution (especially after Juggernaut) is the work out of the box: register a couple of dependencies in the Gemfile, run bundle install, generate a config, start the server, and you can start developing. An example of a simple chat using socky-server can be seen here: sockydemo.imanel.org and see the source code here: http://github.com/socky/socky-example .
Summing up, I note that using the WebSockets API improves the interactivity of the web application and makes the resource more convenient, while slightly loading the server, and the Socky-server will make working with the WebSockets API in a Rails application a simple and painless exercise.