Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Server Sent Events and Rails Streaming

I'm experimenting with Rails 4 ActionController::Live and Server Sent Events. I'm using MRI 2.0.0 and Puma.

For what I can see, each connected client keeps an active connection to the server. I was wondering if it is possible to leverage SSEs without keeping all response streams running.

Puma manages multiple connections using threads, and I imagine there is a limit to the number of cuncurrent connections.
What if I want to support a real-world scenario with thousands of clients registering to my Rails app for SSE events?

Is there any example?

Also, I usually run Rails app servers behind an nginx reverse proxy. Would it require any particular setup?

like image 542
tompave Avatar asked Sep 03 '13 12:09

tompave


People also ask

What are server-sent events used for?

Server-Sent Events (SSE) is a server push technology enabling a client to receive automatic updates from a server via an HTTP connection, and describes how servers can initiate data transmission towards clients once an initial client connection has been established.

What is HTTP event stream?

Event stream format. The event stream is a simple stream of text data which must be encoded using UTF-8. Messages in the event stream are separated by a pair of newline characters.

What is the difference between server-sent events SSEs and WebSockets in html5?

WebSockets and SSEs: Differences The significant difference between both technologies is that WebSockets are bidirectional while SSEs are mono-directional. So, if you're eager to add server push functionality to your app, they're both viable options to explore.

How do I send events from server to client?

The server-sent events streaming can be started by the client's GET request to Server. Accept: text/event-stream indicates the client waiting for event stream from the server, Cache-Control: no-cache indicates that disabling the caching and Connection: keep-alive indicates the persistent connection.


1 Answers

The way that SSEs are built is by the client opening a connection to the server, which is then left open until the server has some data to send. This is part of the SSE spec, and not a thing specific to ActionController::Live. It's effectively the same as long-polling, but with the connection not being closed after the first bit of data is returned, and with the mechanism built into the browser.

As such, the only way it can be implemented is by having multiple open client connections to the webserver which sit there indefinitely. As to what resources are required to deal with them, I'm not sure, as I've not yet tried to benchmark this, but it'll need enough servers for Puma to keep open thousands of connections if you have that many users with a page open.

The default limit for puma is 16 concurrent connections. Several blogs posts about setting up SSEs for Rails mention upping this to a larger value, but none that I've found suggest what this higher value should be. They do mention that the number of DB connections will need to be the same, as each Rails thread keeps one running. Sort of sounds like an expensive way to run things.

"Run a benchmark" is the only answer really.

I can't comment as to reverse proxying as I've not tried it, but as SSEs are done over standard HTTP, I shouldn't think it'll need any special setup.

like image 98
Matt Gibson Avatar answered Sep 30 '22 03:09

Matt Gibson