Get all rooms (Sockets) schema question

Hello!
Just a quick question about Get all rooms action:

Just testing, I have this on Socket/Connect:

On this API:

And the output is:

{
"rooms": [
"3T92Y_Xww2slBH3qAAAB",
1,
"h-5lYDYh_-xIU2bmAAAD",
"D6QpBSwRHdEbE_1XAAAG",
"gVeRcrk9Y6hGiQyIAAAH",
"xQmfqx8doen3rNEZAAAJ"
]
}

After a while

{
"rooms": [
1,
"h-5lYDYh_-xIU2bmAAAD",
"D6QpBSwRHdEbE_1XAAAG",
"gVeRcrk9Y6hGiQyIAAAH",
"xQmfqx8doen3rNEZAAAJ",
"WuoHmCTfpSk7k_tRAAAL"
]
}

And so on... It's okay to have it this way?

Got this working by changing line 140 of lib/modules/sockets.js to:

exports.allRooms = async function (options) {
    if (!this.io) return {};

    const nsp = this.io.of(options.namespace || '/');
    const adapter = nsp.adapter;
    const rooms = {};

    for (const [roomId, sockets] of adapter.rooms) {
        if (adapter.sids?.has(roomId)) continue;
        rooms[roomId] = Array.from(sockets);
    }
    return rooms;
};

Now:

image


image

Your solution only works for single server but not multi server with redis as adapter.

1 Like

I've updated the code to filter the result to only contain the public rooms. It is returns as an array of strings. I don't return it as an object with clients as the redis adapter doesn't support this.

Your solution is nicer since you know how many clients are in the room, but this doesn't work on cluster/redis so didn't implement it.

I've also updated the rooms action which returns the rooms joined for the current client which also showed the private room.

sockets.zip (1.3 KB)

1 Like

Yes, that is totally true, thanks

PS:
Have you considerate implementing some of these methods like server side emit?

I guess redis keys are the best option here..

Thanks again :slight_smile:

Fixed n Wappler 7.7.9