简体   繁体   English

NodeJS和Socket.IO

[英]NodeJS & Socket.IO

I am facing a strange issue while using NodeJS and Socket.io . 我在使用面临着一个奇怪的问题NodeJSSocket.io

Server which receive data via ZeroMQ . 通过ZeroMQ接收数据的服务器。 That work perfect. 那工作完美。 For each message from ZeroMQ , I used sockets.volatile.emit to send that to all connected clients. 对于来自ZeroMQ每条消息,我使用sockets.volatile.emit将其发送到所有连接的客户端。 The issue arise only for large number of connected accounts (more than 100), it seems there is a queue on the sending to clients (client receive message in delay that keep increasing) 问题仅出现在大量连接帐户(超过100个)上,似乎有一个队列发送给客户端(客户端接收消息延迟不断增加)

Note : Each connected client received each message from ZeroMQ, so basically for more client there is more data sent over the socket.IO. 注意:每个连接的客户端都收到来自ZeroMQ的每条消息,因此基本上对于更多客户端,通过socket.IO发送的数据更多。

Via Logs/Debug i know the receive from ZeroMQ has no delay and all works on that part. 通过Logs / Debug我知道ZeroMQ的接收没有延迟,所有工作都在那部分。 The emitting seems to have a queue or delay that keeps increasing. 发射似乎有一个不断增加的队列或延迟。

The messages rate is 80 messages/sec for each client. 每个客户端的消息速率为80消息/秒。

Note: NodeJS 0.10.20 and Socket.IO 0.9.16. Note: NodeJS 0.10.20和Socket.IO 0.9.16。

How can I control that to prevent client received old messages ? 如何控制它以防止客户端收到旧邮件?

Checkout this article it will tell you a lot of the basic mistakes and it's, about blocking the event loop which seems pretty similar to what your doing. 查看这篇文章它将告诉你许多基本错误,它是关于阻止事件循环,这似乎与你的行为非常相似。

Maybe use tools such as: Debug and Blocked i think it would help solve you'r issue. 也许使用如下的工具: DebugBlocked我认为这有助于解决你的问题。 Both to debug where you creating a bottleneck on performance & other basic issues. 两者都可以调试在性能和其他基本问题上创建瓶颈的位置。

Alternatively hook your node project up on PM2 and bind it to Keymetrics.IO this will give you a good view into your server and why it's running slow and why you make a performance bottleneck. 或者在PM2上挂起您的节点项目并将其绑定到Keymetrics.IO,这将为您提供良好的服务器视图,以及为什么它运行缓慢以及为什么会造成性能瓶颈。

Its hard to solve your problem without code examples but here is 3 reasons why your app or you could create bottlenecks (maybe unknowingly): 没有代码示例很难解决您的问题,但这里有3个原因可以解释您的应用程序或者您可能会产生瓶颈(可能是在不知不觉中):

  • Parsing a big json payload with the JSON.parse function. 使用JSON.parse函数解析大型json有效负载。

  • Trying to do syntax highlighting on a big file on the backend (with something like Ace or highlight.js). 尝试在后端的大文件上进行语法突出显示(使用Ace或highlight.js之类的东西)。

  • Parsing a big output in one go (such as the output of a git log command from a child process). 一次性解析大输出(例如从子进程输出git log命令)。

More info in the first article in section 2 called "Blocking the event loop" 更多信息在第2节的第一篇文章中称为“阻止事件循环”

A question related to yours, this one . 这是一个与你有关的问题。

Wanna know more about the Event loop i can warmly direct you to a tread " How the single threaded non blocking IO model works in Node.js " 想要了解更多有关Event循环的信息,我可以热情地指导您如何在Node.js中使用单线程非阻塞IO模型

Here is a model of the Node.js Processing model, to see what happens on the event loop and its surroundings 这是Node.js Processing模型的模型,用于查看事件循环及其周围环境发生的情况

Node.js处理模型

If it turns out that you're not blocking the event loop in any terrible way, then you might be hitting the limits on what socket.io can handle for your specific application. 如果事实证明你没有以任何可怕的方式阻止事件循环,那么你可能会遇到socket.io可以为你的特定应用程序处理的限制。 If thats the case then you might consider scaling up your instances. 如果是这种情况,那么您可以考虑扩展您的实例。

Check out this article for more information: http://drewww.github.io/socket.io-benchmarking/ 查看此文章以获取更多信息: http//drewww.github.io/socket.io-benchmarking/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM