HTTP 与 Websockets 的开销对比

发布于 2024-10-30 15:47:53 字数 606 浏览 6 评论 0原文

我正在两端使用 node.js 构建一个文件同步程序(与 Dropbox 不同)。我需要有潜在的数千个客户同时请求数据。

这是我当前的系统:

  • 服务器通过 websocket 向客户端推送通知(文件已更新)
  • 客户端对下载进行排队并在空闲时发出 HTTP 请求

我将以压缩块的形式提供数据,例如每个 50 MB,因此 HTTP 请求开销(标头)可以忽略不计。

如果我使用 websockets 来请求推送通知,是否会出现:

  • 整体速度显着提高? (减少延迟、身份验证等)
  • 服务器上保持连接开放的额外开销?
  • 推送二进制数据有问题吗?

我认为我需要通过专用的 websocket 发送通知,因为我不希望它们在下载时在服务器上排队(大量开销)。

注意:只要客户端的系统处于打开状态,这些 Websocket 将长期开放。

编辑:我将在不同端口上的不同 http 服务器上使用 Websockets,以便将它们移动到不同的位置CPU 核心。我可能会打开数千个(如果不是数十万个)并发 Websocket...

I am building a file synchronization program (not unlike Dropbox) using node.js on both ends. I need to have potentially thousands of clients requesting data at the same time.

Here is my current system:

  • Server pushes notifications to client over a websocket (file has been updated)
  • Client queues downloads and makes an HTTP request when idle

I will be serving data in compressed chunks of, say, 50 MB each, so the HTTP request overhead (headers) is negligible.

If I were to use websockets for requests and push notifications, would there be:

  • Noticeable overall speed improvements? (reduced latency, authentication, etc.)
  • Additional overhead on the server to keep connections open?
  • Issues with pushing binary data?

I think I need to have notifications sent over a dedicated websocket because I don't want them to be queued on the server while a download is taking place (lots of overhead).

Note: These websockets will be open long-term, as long as the client's system is on.

EDIT: I will be using the websockets on a different http server on different ports in order to move them to different CPU cores. I could potentially have thousands (if not hundreds of thousands) of concurrent websockets open...

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

╰沐子 2024-11-06 15:47:54

如果您打算在客户端和服务器上使用node.js,那么您应该使用本机 net 模块带有纯套接字而不是 WebSocket。纯套接字对于数据传输(尤其是二进制数据)进行了更好的优化。据我所知,浏览器 WebSocket 甚至还不支持二进制传输。

If you intend to use node.js for both client and server then you should use native net module with pure sockets rather than WebSockets. Pure sockets are much better optimized for data transfer, especially binary. As far as I know browser WebSockets do not even support binary transfer yet.

静水深流 2024-11-06 15:47:54

我正在寻找其他东西,我发现这篇文章很好地解释了 websockets:

http://blog.new-bamboo.co.uk/2009/12/7/real-time-online-activity-monitor -example-with-node-js-and-websocket

以下是本文中一些非常有趣的部分:

与现有解决方案相比,Websocket 使您能够以显着更少的网络开销进行连续通信。

和:

在使用 WebSocket 建立连接期间,客户端和服务器每帧交换 2 个字节的数据,而进行连续轮询时则需要 8 KB 的 http 标头。

对于我的用例(没有浏览器),这似乎是最佳解决方案!现在我只需要决定每个客户端是否需要一个 Websocket 还是多个 Websocket(目前我倾向于使用一个 Websocket)。

我真的希望这对某人有用。我暂时将其保留。我将在本周晚些时候参加 JS 会议,如果我学到更多信息,我将添加到这篇文章中。

对于那些关心浏览器支持的人,请参阅。看起来 WebKit 是唯一支持它的可靠引擎(Chrome 和 Safari)。

I was searching around for something else and I found this post that explains websockets pretty well:

http://blog.new-bamboo.co.uk/2009/12/7/real-time-online-activity-monitor-example-with-node-js-and-websocket

Here are some pretty interesting parts from the article:

Websocket enables you to have continuos communication in significantly less network overhead compared to existing solution.

And:

During making connection with WebSocket, client and server exchange data per frame which is 2 bytes each, compared to 8 kilo bytes of http header when you do continuous polling.

For my use case (no browser), this seems like the optimal solution! Now I just need to decide whether I want to have a single websocket or multiple websockets per client (I'm leaning towards a single one at this point).

I really hope this is useful to someone. I'll leave this open for the time being. I'll be attending a JS conference later this week, and if I learn anything more I'll add to this post.

For those of you who care about browser support, see this. It looks like WebKit is the only reliable engine that supports it (Chrome and Safari).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文