0

Here is an excerpt of the code I am using to make a connection to my chat room...

io.on('connection', function(socket) {
    var col = db.collection('messages');
    sendStatus = function(s){
        socket.emit('status', s);
    };
    //emit all messages
    col.find().limit(100).sort({_id:1}).toArray(function(err, res){
        if(err) throw err;
        socket.emit('output', res);

    });
    socket.on('input', function(data) {
    ...
    ... etc

What code can I implement to check how many users or participants are connected to my chat room.

I heard of a method clients() but I'm not sure if that's what I need to use or how to use it.

buydadip
  • 8,890
  • 22
  • 79
  • 154

1 Answers1

0

In the latest version of socket.io, you can see how many sockets are in a given chat room with this:

io.sockets.adapter.rooms["testRoom"].length

Where testRoomis whatever the name of your chat room is (whatever you used .join() with.


io.sockets.adapter.rooms["testRoom"]

is an Object where each property name except .length is the socket.id of the socket that is in the chat room.


FYI, the code you show in your question doesn't appear to have anything to do with socket.io chat rooms so that has me a bit perplexed.

jfriend00
  • 683,504
  • 96
  • 985
  • 979