Socket.io Connected User Count

2019-03-08 13:42发布

I finally got socket.io to work properly, but I have encountered a strange problem.

I am not sure if this is the best way, but I am using:

io.sockets.clients().length

This returns the number of clients connected to my server. The problem is after a few connects and disconnects of users, the number starts to stay higher than it should be.

For instance, if I connect and ask my friends to, the number goes up which is correct. But when we start to disconnect and reconnect the number does not decrease.

I am running the node.js and sockets.io server on a vmware ubuntu server.

Does anyone know why this is or has a better method for finding out how many people are connected to the server?

10条回答
孤傲高冷的网名
2楼-- · 2019-03-08 14:04

I have found the way to figure it out in version 1.3.7. There are three methods as follows:

  1. io.engine.clientsCount
  2. io.sockets.sockets.length
  3. Object.keys(io.sockets.connected).length

Hope these can help someone with the same issue.:)

查看更多
Animai°情兽
3楼-- · 2019-03-08 14:05

I am currently using socket v1.4.29 with typeScript, you can find the number of clients connected by using this

 io.sockets.on('connection', function(socket) {
 var clients = socket.client.conn.emit.length;
 console.log("clients: " + clients);
 });
查看更多
爱情/是我丢掉的垃圾
4楼-- · 2019-03-08 14:06

Just in case someone gets to this page while using socket.io version 1.0

You can get the connected clients count from

socketIO.engine.clientsCount

Need an answer and the above did not work for new version of socket.io

查看更多
男人必须洒脱
5楼-- · 2019-03-08 14:06

Connected Users count in number with socket.io version - 1.3.7

io.on('connection', (socket) => {
    console.log(io.sockets.server.httpServer._connections);  //output in number
    // or
    console.log(io.sockets.server.engine.clientsCount);  //output in number
});

查看更多
何必那么认真
6楼-- · 2019-03-08 14:07

There is a github issue for this. The problem is that whenever someone disconnects socket.io doesn't delete ( splice ) from the array, but simply sets the value to "null", so in fact you have a lot of null values in your array, which make your clients().length bigger than the connections you have in reality.

You have to manage a different way for counting your clients, e.g. something like

socket.on('connect', function() { connectCounter++; });
socket.on('disconnect', function() { connectCounter--; });

It's a mind buzz, why the people behind socket.io have left the things like that, but it is better explain in the github issue, which I posted as a link!

查看更多
forever°为你锁心
7楼-- · 2019-03-08 14:07

I'm using socket.io 0.9.10 and the following code to determine the number of sockets:

var socketIO =  require('socket.io').listen( .....
var numberOfSockets = Object.keys(socketIO.connected).length;

Not sure how accurate this number reacts to the various edge-cases, but 'til now it seems accurate: every browser connecting increases the number, every browser closed decreases it.

查看更多
登录 后发表回答