Socket.IO transmitting a array per emit(…)

Socket.IO transmitting a array per emit(…)


I am trying to build a array in my socket io server and sending it to the client.

 var roomList = io.sockets.manager.rooms;

    // new Array to save the clients per room
    var clientsPerRoom = new Array();

    //for (var i = 0; i < roomList.length; i++) {
    for (var room in roomList) {
        room = room.replace("/", "");

        // get the clients per room
        var clients = io.sockets.clients(room).length;

        // saving it in the array at the name of the room
        clientsPerRoom[room] = clients;
    // transmit it to the client
    io.sockets.emit('roomList', roomList, clientsPerRoom);

On the client side

var clients = 1;

        for (room in roomList) {
            if (room.length > 0) {
                room = room.replace("/", "");

                clients = clientsPerRoom[room];

                console.log("ROOM: '" + room + "' has '" + clients + "' CLIENTS");


At the client, “clientsPerRoom” is “[]” (empty?), and so “clients” is “undeined”. What am I doing wrong?

At the console log from the server are the values 1 if a user is connectet. If more users are online then its still 1 but at least it should send this value to the client.


Problem courtesy of: Mexx


Even solved yet.

Problem was: io.sockets.clients(room) only counts users who accepted webcam access, otherwise the are not “really” in the room yet.


Solution courtesy of: Mexx


Leave a Reply

Your email address will not be published. Required fields are marked *