sails.sockets.id()
Gets the ID of a request socket object.
sails.sockets.id(socket);
Usage
Argument | Type | Details | |
---|---|---|---|
1 | socket | ((Socket)) | A request socket (WebSocket/Socket.io) object e.g. req.socket . |
Once acquired, the socket object's ID can be used to send direct messages to that socket (see sails.sockets.emit) or get information about the rooms that the socket is subscribed to (see sails.sockets.socketRooms).
Example
// Controller action
getSocketID: function(req, res) {
if (!req.isSocket) return res.badRequest();
var socketId = sails.sockets.id(req.socket);
// => "BetX2G-2889Bg22xi-jy"
return res.ok('My socket ID is: ' + socketId);
}
Notes
- The phrase "request socket" here refers to an application-layer WebSocket/Socket.io connection.
req.socket
also exists for HTTP requests, but it refers to the underlying TCP socket at the transport layer, which is different. Be sure and ensurereq.isSocket == true
before usingreq.socket
with this method.