This method is deprecated.
If you absolutely need to retrieve the list of socket IDs subscribed to a specific record, you may use the lower-level
app.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.
Something.subscribers(`record`,[`contexts`])
Returns an array of sockets that are subscribed to record
. This can be used in conjunction with lower-level methods like [sails.sockets.emit
]https://sailsjs.com/documentation/reference/web-sockets/sails-sockets/sails-sockets-emit) to send custom messages to a collection of sockets, or with .subscribe
to subscribe one group of sockets to a new instance.
Description | Accepted Data Types | Required ? | |
---|---|---|---|
1 | Record | Yes | |
2 | Contexts to subscribe to | No |
Note: record
can be either an instance of a model, or a model’s primary key.
context
If you specify a specific context (or array of contexts), you will only get sockets that are subscribed to the specified contexts for the record.
Controller Code
// Find user #1
User.findOne(1).exec(function(e,userOne){
// Get all of the sockets that are subscribed to user #1
var subscribers = User.subscribers(userOne);
// Subscribe them all to userOne's best friend, too
_.each(subscribers, function(subscriber) {
User.subscribe(subscriber, userOne.bestFriendId);
});
});