我可以通过 socket.emit 发送多少数据? [英] How much data can I send through a socket.emit?

查看:23
本文介绍了我可以通过 socket.emit 发送多少数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我使用 node.js 和 socket.io.我有这个小程序,它接收文本框的内容并将其发送到 node.js 服务器.然后,服务器将其中继回其他连接的客户端.有点像聊天服务,但不完全是.

So I am using node.js and socket.io. I have this little program that takes the contents of a text box and sends it to the node.js server. Then, the server relays it back to other connected clients. Kind of like a chat service but not exactly.

无论如何,如果用户输入价值 2-10k 的文本并尝试发送它会怎样?我知道我可以尝试一下并亲眼看看,但我正在寻找一个实用的最佳实践限制,以限制我可以通过发出的数据量.

Anyway, what if the user were to type 2-10k worth of text and try to send that? I know I could just try it out and see for myself but I'm looking for a practical, best practice limit on how much data I can do through an emit.

推荐答案

Node 和 socket.io 没有任何内置限制.您需要担心的是消息大小、每秒发送的消息数量、连接的客户端数量以及服务器可用带宽之间的关系.换句话说,没有简单的答案.

Node and socket.io don't have any built-in limits. What you do have to worry about is the relationship between the size of the message, number of messages being send per second, number of connected clients, and bandwidth available to your server – in other words, there's no easy answer.

让我们考虑一个 10 kB 的消息.当有 10 个客户端连接时,这相当于您的服务器必须推送 100 kB 的数据,这是完全合理的.添加更多客户端,事情很快就会变得更加苛刻:10 kB * 5,000 个客户端 = 50 MB.

Let's consider a 10 kB message. When there are 10 clients connected, that amounts to 100 kB of data that your server has to push out, which is entirely reasonable. Add in more clients, and things quickly become more demanding: 10 kB * 5,000 clients = 50 MB.

当然,您还必须考虑协议开销的数量:每个数据包,TCP 添加 ~20 个字节,IP 添加 20 个字节,以太网添加 14 个字节,总计 54 个字节.假设 MTU 为 1500 字节,您会看到每个客户端有 8 个数据包(不考虑重新传输).这意味着您将通过网络向每个客户端发送 8*54=432 字节的开销 + 10 kB 有效载荷 = 10,672 字节.

Of course, you'll also have to consider the amount of protocol overhead: per packet, TCP adds ~20 bytes, IP adds 20 bytes, and Ethernet adds 14 bytes, totaling 54 bytes. Assuming a MTU of 1500 bytes, you're looking at 8 packets per client (disregarding retransmits). This means you'll send 8*54=432 bytes of overhead + 10 kB payload = 10,672 bytes per client over the wire.

10.4 kB * 5000 个客户端 = 50.8 MB.

10.4 kB * 5000 clients = 50.8 MB.

在 100 Mbps 链路上,理论上向 5,000 个客户端传送 10 kB 消息所需的最短时间链接.当然,在丢包和损坏数据需要重新传输的现实世界中,需要更长的时间.

On a 100 Mbps link, you're looking at a theoretical minimum of 4.3 seconds to deliver a 10 kB message to 5,000 clients if you're able to saturate the link. Of course, in the real world of dropped packets and corrupted data requiring retransmits, it will take longer.

即使使用非常保守的估计 8 秒向 5,000 个客户端发送 10 kB,这在每 10 到 20 秒发送一条消息的聊天室中也可能没问题.

Even with a very conservative estimate of 8 seconds to send 10 kB to 5,000 clients, that's probably fine in chat room where a message comes in every 10-20 seconds.

实际上,可以归结为几个问题,按重要性排序:

So really, it comes down to a few questions, in order of importance:

  1. 您的服务器有多少可用带宽?
  2. 将同时连接多少用户?
  3. 每分钟将发送多少条消息?

回答完这些问题后,您可以确定您的基础架构将支持的最大消息大小.

With those questions answered, you can determine the maximum size of a message that your infrastructure will support.

这篇关于我可以通过 socket.emit 发送多少数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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