How scalable are the events sent by the server in Rails?

I plan to write a Rails application in which several users will have updated information that they will use using server events with ActionController :: Live and Puma. I already wrote a test application and it seems to work very well. For what I am doing, SSEs make more sense than WebSockets, as most users just listen in, and SSEs are much simpler than creating websockets-rails that also depend on Faye (in this case, I would just write my own code above Faye).

What I want to know is how scalable are the events sent by the server in Rails? This is due to the fact that I will use Puma, which creates a new stream for each user connected to the EventSource. This application is potentially aimed at the ability to simultaneously connect to hundreds of thousands of users, but the default Puma default limit is 16. Is there a reason why I canโ€™t change the flow limit to 200,000?

+5
source share
3 answers

If Puma creates a new thread for each connection, do not use it. You not only plan hundreds of thousands of users at the same time, but provided that it will be a web application, users can have several instances open on several browser tabs. Even the SSE specification warns of the "multiple tabs" error , because browsers can have their own limits for the number of simultaneous connections to one host:

Clients that support the restriction on connecting to a single HTTP server may run into a problem when opening multiple pages from a site if each page has an EventSource in the same domain. Authors can avoid this by using the relatively sophisticated mechanism for using unique domain names in connections, or by allowing the user to enable or disable the EventSource functionality for each page or share a single EventSource object using a common user.

Use a dedicated server where connections are not blocked. The aforementioned gevent, something built on Node JS or something else in Ruby (which I don't know and therefore cannot recommend anything).

For other readers who get to this page and might get confused, Rich Peck's answer is incorrect. Events sent by the server do not rely on long polling, and they do not send requests every couple of seconds. These are long-term HTTP connections without having to re-open the connection after each event. There are no "persistent requests" on the server.

+6
source

About the Puma approach, which uses one thread for each client, given that you have a limit of 16 threads per server, maybe you can think about scaling the servers horizontally? For example, if you deploy to amazon and configure Elastic Load Balancing + Auto Scaler, your infrastructure will be able to receive as many clients as necessary. Or I'm wrong? And I think that the problem with several tabs could be overcome by prohibiting several connections for each client, displaying the corresponding error message if the user opens a new tab.

0
source

I am not sure about the threading problem, but from experience I can give you some idea about SSE


SSE's

Most importantly, the SSE depends on Javascript for a long poll , that is, it will continue to send requests to your server every few seconds, in an attempt to "listen" for updates

Check this when setting up the SSE eventlistener - it will send requests every couple of seconds to your server. The connection will not last forever

There is a great discussion here: What are Long-Polling, Websockets, Server-Sent Events (SSE) and Comet?

-

The problem here, at least, is that if you send persistent requests to your server, it will not only be inefficient, but also very compressed in terms of functionality

As in pub / sub pattern , if you want to deliver live updates to your users, first you want to have some authentication (which, in my opinion, SSE does not provide), as well as ensuring that you have certain โ€œchannelsโ€ for these users for receive updates for them

Having said that, there is a great post about creating a chat application with SSE here :

enter image description here

I only ever used SSE to send updates for the whole site, simply because I would prefer to have one connection (websocket) that I can authenticate and populate with specific data


Pusher

We use websockets wherever we can

However, the trick is that you can use a third-party website provider, such as Pusher . I am in no way connected with Pusher; we used them for a number of projects - EPIC service

Using Pusher, you can receive and deliver specific messages to users on your site without having to set up your own websocket server. You simply connect to the Pusher service with the same JS (eventlistener) setting, which will connect only once to the Pusher service: enter image description here

You can then send updates to your users simply by clicking on the Pusher API. We pre-installed this using the analytics application, which is under construction here


To answer your question, I do not know how scalable SSE

I tend to lead โ€œbigโ€ Internet companies (as a demonstration of how to do this โ€œrightโ€), and I have not yet seen any of them endorse SSE over websockets. Maybe I'm wrong, but they all prefer websockets

-2
source

Source: https://habr.com/ru/post/1201001/


All Articles