Im trying to perform some analytics on the sockets that are coming from the server, i want to know which room has the most connections. cause in one utility event for sockets, im emitting the total number of connected sockets for a particular room (guestRoom) and in some instances this can climb quite, high, but this happens whenever someone makes a connection to the socket through the browser and is not logged in through my service, of these socket events they are further divided into other rooms based on how they are interacting with the service, the room has the _id
of the document as the room name, i want to know which of these rooms has the most connected users.
so my thought process on this is to get all the rooms loop over the data count the total number of connections and send back the id of the room.
here is the code i've crafted so far.
/**
* @description finds the room with the most connections and returns the id of the room to the client
* @param {Object} io - the socket io object
*
*/
export default async (io) => {
try {
const rooms = io.sockets.adapter.rooms;
console.log(rooms);
let max = 0;
let roomWithMostConnections = '';
for (const room in rooms) {
console.log(`Room: ${room}`.yellow);
if (room == 'adminRoom' || room == 'userRoom' || room == 'watcherRoom')
continue;
// we need to turn each room into an array and check the length of the connections
const roomConnections = Array.from(rooms[room].sockets);
console.log(`Connections: ${roomConnections.length}`.green);
if (roomConnections.length > max) {
console.log(`Max: ${max}`.red);
max = roomConnections.length;
roomWithMostConnections = room;
}
}
console.log(
`The room with the most connections is ${roomWithMostConnections}`
);
io.emit('mostConnections', roomWithMostConnections);
} catch (error) {
console.log(error);
}
};
the thing thats causing my hangup is the data thats returned for the variable const rooms
which looks like this:
[0] Map(6) {
[0] 'Sx2LUMA6rhMMJYbXAAAD' => Set(1) { 'Sx2LUMA6rhMMJYbXAAAD' },
[0] '0Cb2x7XgEsRWgpF2AAAH' => Set(1) { '0Cb2x7XgEsRWgpF2AAAH' },
[0] 'Ye33dgBl3UbJt1WGAAAK' => Set(1) { 'Ye33dgBl3UbJt1WGAAAK' },
[0] 'wCpe0OQQCVqF1Z08AAAL' => Set(1) { 'wCpe0OQQCVqF1Z08AAAL' },
[0] '61CJKUhmjGP6kbZpAAAO' => Set(1) { '61CJKUhmjGP6kbZpAAAO' },
[0] 'NPxCHhSzypIE_t8nAAAP' => Set(1) { 'NPxCHhSzypIE_t8nAAAP' }
[0] }
Im not sure how to read this data, i know map
usually refers to an array method, but this clearly looks like an object, but something like Object.entries
returns an []
array, and if i do Array.from
the data gets even messier...
what is this data, and how should i read it so i can turn this into an array to count the total number of connections.
CodePudding user response:
This Map is a data structure, we could say its like an object, that accepts anything as a key and saves its data in order.
Docs I would suggest you also looking at javascripts full reference book.
You can use Map's get method to retrieve its data.
rooms.get('Sx2LUMA6rhMMJYbXAAAD')
Maps are also iterable, so you could use rooms.forEach() for example.
In case you dont know. a Set is another data structure. Hope this article can help you.