Socket.io Connected User Count

后端 未结 12 826
有刺的猬
有刺的猬 2021-01-30 20:24

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


        
相关标签:
12条回答
  • 2021-01-30 20:57

    I don't see any mention of multi core apps so I'm just gonna add that since I am using multiple cores ( clusters ) I wasn't able to get the right number of sockets consistently on the client side, so I ended up saving them to my mongo instance and it is quite consistent and accurate. With this approach I can view my socket connections in style via the browser :).

    Mongoose schema :

    var socketSchema = mongoose.Schema({
            socket : Number
    });
    

    Usage:

    //reset to 0 when the app starts ( just in case )
    SocketModel.find({ "socket" : 1 } , function(err, deadSockets ) {
        if (err){
            console.log( err );
        }
        else{
            for( var i = 0 ; i < deadSockets.length ; i++ ){
                deadSockets[i].remove();                
            }
        }
    });
    
    io.on('connection', function( socket ) {
        //I found I needed to make sure I had a socket object to get proper counts consistantly
        if( socket ){
            var socketEntry = new SocketModel({ "socket" : 1 });
            socketEntry.save( function(err ){
                if (err){
                    console.log( err );
                }
                else{
            
                }
            });
        }
        //On Disconnect
        socket.on('disconnect', function() {
            SocketModel.findOne({ "socket" : 1} , function(err, deadSocket ) {
                if (err){
                    console.log( err );
                }
                else{
                    deadSocket.remove();
                }
            }); 
        });
    });
    

    How many do I have ?

    SocketModel.count({ "socket" : 1 } , function(err, count ) {
        if (err){
            console.log(err);
        }
        else{
            var term = "sockets";
            if( count == 1 ) term = "socket";
            console.log("Current Load: " , count , term );
        }
    }); 
    

    NOTE I don't like using empty query objects ( {} ) so I just used { "socket" : 1 } as a dummy instead

    0 讨论(0)
  • I am currently using Socket.io v1.3.6 and have found that this works. It gives an accurate number when users connect and when they disconnect:

    io.sockets.sockets.length
    

    Like so:

    var io = require('socket.io').listen(server);
    io.on('connection', function(socket) {
      console.log(io.sockets.sockets.length);
      socket.on('disconnect', function() {
        console.log(io.sockets.sockets.length);
      });
    });
    
    0 讨论(0)
  • 2021-01-30 21:04

    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);
     });
    
    0 讨论(0)
  • 2021-01-30 21:05

    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

    0 讨论(0)
  • 2021-01-30 21: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.

    0 讨论(0)
  • 2021-01-30 21:08

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

    0 讨论(0)
提交回复
热议问题