Edit Page

.getId()

Parse the socket ID from an incoming socket request (req).

sails.sockets.getId(req);

Usage

#
Argument Type Details
1 req req A socket request (req).

Once acquired, the socket object's ID can be used to send direct messages to that socket (see sails.sockets.broadcast).

Example

#
// Controller action
getSocketID: function(req, res) {
  if (!req.isSocket) {
    return res.badRequest();
  }

  var socketId = sails.sockets.getId(req);
  // => "BetX2G-2889Bg22xi-jy"

  sails.log('My socket ID is: ' + socketId);

  return res.json(socketId);
}

Notes

#
  • Be sure to check that req.isSocket === true before passing in req. This method does not work for HTTP requests!

Is something missing?

If you notice something we've missed or could be improved on, please follow this link and submit a pull request to the sails repo. Once we merge it, the changes will be reflected on the website the next time it is deployed.

Reference

Reference