This method is deprecated.
If you absolutely need to retrieve the list of socket IDs in a room, you may use the lower-level
sails.io.sockets.in(roomName).clients(cb)
method (see https://github.com/socketio/socket.io/#namespaceclientsfnfunction).However, be aware that in multi-server scenarios, that method will not return IDs of sockets on other servers (at the time of writing, the current documentation on Socket.io's GitHub is out of date on that point).
For the most common use-case of "taking all members of room A and subscribing/unsubscribing them to room B", you can use the
sails.sockets.addRoomMembersToRooms
andsails.sockets.removeRoomMembersFromRooms
methods, which do work cross-server.
Get the IDs of all sockets subscribed to a room.
sails.sockets.subscribers(roomName, cb);
Argument | Type | Details | |
---|---|---|---|
1 | roomName | The name of the room whose socket IDs should be retrieved. e.g. 'supportchat' . |
|
2 | cb | Function to be called when the socket IDs have been retrieved. The function should have two arguments: err and socketIds . |
sails.sockets.subscribers('supportchat', function(err, socketIds) {
console.log(socketIds);
});
// => ['BetX2G-2889Bg22xi-jy', 'BTA4G-8126Kr32bi-za']
This method currently only operates on a single server. If your Sails app is distributed over multiple servers (for example by using the
socket.io-redis
adapter), calling.subscribers(roomName, cb)
will only retrieve the socket IDs of subscribers toroomName
that are connected to the server making the call.
// Controller action
getRoomSubscribers: function(req, res) {
if (!req.isSocket) return res.badRequest();
if (!req.param('room')) return res.badRequest('No room
specified- please specify the name of the room whose subscribers you want to look up.');
var subscribers = sails.sockets.subscribers(room); return res.ok(require('util').format( 'The "%s" room currently has %d subscribers: ', req.param('room'), subscribers.length, subscribers )); } ``` -->