Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NodeJS & Socket.IO

I am facing a strange issue while using NodeJS and Socket.io.

Server which receive data via ZeroMQ. That work perfect. For each message from ZeroMQ, I used sockets.volatile.emit to send that to all connected clients. 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)

Note : Each connected client received each message from ZeroMQ, so basically for more client there is more data sent over the socket.IO.

Via Logs/Debug i know the receive from ZeroMQ has no delay and all works on that part. The emitting seems to have a queue or delay that keeps increasing.

The messages rate is 80 messages/sec for each client.

Note: NodeJS 0.10.20 and Socket.IO 0.9.16.

How can I control that to prevent client received old messages ?

like image 860
shay Avatar asked Oct 23 '13 10:10

shay


People also ask

What is NodeJS used for?

Node. js is primarily used for non-blocking, event-driven servers, due to its single-threaded nature. It's used for traditional web sites and back-end API services, but was designed with real-time, push-based architectures in mind.

Is NodeJS a programming language?

Node. js is not a programming language. Rather, it's a runtime environment that's used to run JavaScript outside the browser.

Is NodeJS frontend or backend?

Node. js is sometimes misunderstood by developers as a backend framework that is exclusively used to construct servers. This is not the case; Node. js can be used on the frontend as well as the backend.

Is NodeJS better than Java?

Java uses the concept of multithreading with ease, whereas Node JS does not use the concept of multi-threading like Java does. For large scale projects that involved concurrency, Java is highly recommended, whereas Node JS does not handle the thread and Java, which is the weakest point of this framework.


1 Answers

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. 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.

Its hard to solve your problem without code examples but here is 3 reasons why your app or you could create bottlenecks (maybe unknowingly):

  • Parsing a big json payload with the JSON.parse function.

  • Trying to do syntax highlighting on a big file on the backend (with something like Ace or highlight.js).

  • Parsing a big output in one go (such as the output of a git log command from a child process).

More info in the first article in section 2 called "Blocking the event loop"

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"

Here is a model of the Node.js Processing model, to see what happens on the event loop and its surroundings

Node.js processing model

like image 101
Simon Dragsbæk Avatar answered Sep 20 '22 16:09

Simon Dragsbæk