Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to check the number of open connections in node.js?

I have a machine running node.js (v0.1.32) with a tcp server (tcp.createServer) and a http server (http.createServer). The http server is hit by long polling requests (lasting 50 sec each) from a comet based application on port 80. And there are tcp socket connections on port 8080 from an iphone application for the same purpose.

It was found that the server was not able to handle more connections (especially the tcp connections while the http connections appeared fine!!??) for a while and was normal only after a restart.

For load testing the connections I have created a tcp server and spawned 2000 requests and figured that the connections starting to fail after the max file descriptor limit on machine is reached (default 1024). Which is a really very small number.

So, a novice question here: How do I scale my application to handle more number of connections on node.js and how I handle this issue.

Is there a way to find out how many active connections are there at the moment?

Thanks Sharief

like image 681
Sharief Shaik Avatar asked Jul 09 '10 16:07

Sharief Shaik


People also ask

How many connections are in a node?

No matter what size instance we use Node always appears to max out at 1000 concurrent connections (this is NOT 1000 per second, but 1000 it can handle at 1 time).

How many connections can a node js handle?

JS uses a single thread with an event-loop. In this way, Node can handle 1000s of concurrent connections without any of the traditional detriments associated with threads.

How many concurrent connections can a node server handle?

As is, node. js can process upwards of 1000 requests per second and speed limited only to the speed of your network card. Note that it's 1000 requests per second not clients connected simultaneously. It can handle the 10000 simultaneous clients without issue.

How many requests can node handle per second?

However, considering that a “Hello World” Node. js server is easily capable of thirty thousand requests per second on that machine that produced these results, 23 requests per second with an average latency exceeding 3 seconds is dismal.


1 Answers

You can get the count of connections by using below:

var server = http.createServer(app);     
server.getConnections(function(error, count) {
    console.log(count); 
});

Using this you keep check on connection and when it cross a threshold then close the previous connections. Hope it helps.

like image 152
dineshydv Avatar answered Sep 21 '22 15:09

dineshydv