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

查看:358
本文介绍了我可以通过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链路上,您正在查看理论上的最低要求,为4.3秒,以便向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.

即使非常保守地估计,要向5,000个客户发送10 kB的时间为8秒,在聊天室中每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天全站免登陆