Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
/**
* Returns the room names that a socket is connected to.
*
* @param {string} socketId - a socket id - `sails.sockets.getId(this.req)`
*
* @returns {string[]} rooms
*/
function getSocketRooms(socketId) {
return new Promise((resolve, reject) => {
sails.io.of('/').adapter.clientRooms(socketId, (err, rooms) => {
if (err) {
reject(err);
} else {
// the "||" because if socket is not connected to any rooms, ids is null
resolve(rooms || []);
}
});
});
}
/**
* Gets the sockets currently connected in room.
*
* @param {string} room
*
* @returns {string[]} socketIds
*/
function getSocketIdsInRoom(room) {
return new Promise((resolve, reject) => {
sails.io.in(room).clients((err, socketIds) => {
if (err) {
reject(err);
} else {
// the "||" because if room has no sockets in it,
resolve(socketIds || []);
}
});
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.