服务器发送事件和Rails流 [英] Server Sent Events and Rails Streaming

查看:68
本文介绍了服务器发送事件和Rails流的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试Rails 4 ActionController::Live和服务器发送事件.我正在使用MRI 2.0.0和Puma.

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

据我所知,每个连接的客户端都保持与服务器的活动连接.我想知道是否有可能在不保持所有响应流运行的情况下利用SSE.

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使用线程管理多个连接,我想并发连接的数量是有限制的.
如果我想支持一个现实世界的场景,有成千上万的客户注册到我的Rails应用中进行SSE事件,该怎么办?

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?

有什么例子吗?

此外,我通常在Nginx反向代理后面运行Rails应用服务器.是否需要任何特定的设置?

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

推荐答案

构建SSE的方法是,客户端打开与服务器的连接,然后断开连接,直到服务器发送一些数据为止.这是SSE规范的一部分,而不是ActionController :: Live的特定内容.它实际上与长轮询相同,但是在返回第一位数据后,连接没有关闭,并且浏览器内置了该机制.

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.

因此,唯一可以实现的方法是通过无限期地与Web服务器建立多个开放的客户端连接.至于需要什么资源来处理它们,我不确定,因为我还没有尝试对其进行基准测试,但是如果您有那么多用户,它需要足够的服务器供Puma保持开放的数千个连接.打开页面.

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.

puma的默认限制是16个并发连接.几篇关于为Rails设置SSE的博客文章都提到将其提高到更大的价值,但我发现没有一个建议提出更高的价值.他们确实提到数据库连接的数量必须相同,因为每个Rails线程都保持一个正在运行.某种声音听起来像是一种昂贵的运行方式.

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.

我还没有评论过反向代理,因为我没有尝试过,但是由于SSE是通过标准HTTP完成的,所以我不认为它需要任何特殊的设置.

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.

这篇关于服务器发送事件和Rails流的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆