📎 Webclip
WebSockets in Rails 4
The post says Rails 4’s concurrent request handling makes it possible to keep WebSocket connections open in a single process with a concurrent server like Puma. It also says this approach avoids EventMachine and any reactor model.
Reading notes#
- Rails 4 is described as fully concurrent, so there is no full-stack lock on a request.
- With a concurrent server like Puma, one process can handle many requests at the same time.
- Ruby threads inside a Rails app can hold WebSocket connections open without bogging down the server.
- Rack Hijack, available since Rack 1.5.0, gives access to the underlying socket for bidirectional communication with the client.
- Because Rails is built on Rack, the client socket can be grabbed directly from a Rails controller.
- Tubesock is presented as a small, new, untested gem that packages this functionality.
- Tubesock provides a Rails controller module and a wrapper method to hijack the Rack connection.
- It also wraps the
websocketgem to handle WebSocket handshakes and frames. - The example controller shows a
chataction that hijacks the connection, sends a greeting on open, and echoes received data back to the client. - The post links to the Tubesock GitHub repository and to an example chat application.
