ruby-on-railswebsocketfayewebsocket-rails

why websocket-rails need to started as a standalone server to support non-eventmachine based web servers?


from websocket-rails wiki, websocket-rails need to started as standalone server when we use Phusion Passenger or Unicorn, but I found faye-rails do not need to do this. what's the difference between them ?


Solution

  • The difference is the API used for "hijacking" or controlling the socket.

    Both solutions take the raw socket away from the original server and handle it independently from the server... However, websocket-rails uses an EventMachine based API, while Faye checks for different supported options, such as the hijack API.

    For this reason, when a non EventMachine based server is used, websocket-rails isn't able to take the socket away from the server and instead runs a separate server that handles only Websocket connection.

    Other approaches include server side support for the websocket protocol in accordance with the proposed Websocket Rack Specification draft, which is implemented by Iodine (disclaimer, this is my project). Other platforms (i.e. Puma) are also working on support for the specification.

    This approach leaves the control of the socket with the original server, allowing the original server to handle all the protocol details. It usually allows for better resource handling, as the socket handling is unified and often resource usage is better optimized. Also, with this approach, code duplication is avoided and the memory footprint is often smaller.

    Another upside to this new specification and approach is that people who write decent servers are usually better aware of networking and server programming issues (such as slow client DoS attack issues etc'), allowing for a more robust and secure Websocket implementation.

    P.S.

    I would avoid websocket-rails if possible, ActionCable would probably have better Rails integration and unexpected socket hijacking might break middleware, causing weird bugs (at one point hijack would cause weird database connection pooling issues, where connections weren't returned to the pool).

    Good Luck!