S
S
Sveratum2015-04-20 14:03:02
Node.js
Sveratum, 2015-04-20 14:03:02

How to count number of node.js connections?

Good afternoon!
There is a small project on node.js + express, I can't figure out how to count the number of active connections at the moment.
Those. a function that, when called, yields a specified number of connections.
I will be glad to help.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
I
Ilya Shatokhin, 2015-04-20
@srsd

var count = 0;
var app = express();

http.createServer(app).listen(8080)
  .on('connection', function(socket) {
    count++;

    socket.on('close', function () {
      count--;
    })
  });


function getConnectionsCount () {
  return count;
}

UPD . There is also an asynchronous standard method:
var app = express();
var server = http.createServer(app).listen(8080);

server.getConnections(function (err, count) {
  
});

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question