Server Sent Events and Rails Streaming

前端 未结 1 743
执笔经年
执笔经年 2021-02-14 01:28

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 ke

1条回答
  •  名媛妹妹
    2021-02-14 01:59

    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.

    0 讨论(0)
提交回复
热议问题