View our GitHub

Please visit sails-docs on GitHub to view documentation on your mobile device.

Edit Page

req.socket

If the current Request (req) originated from a connected Socket.io client, req.socket refers to the raw Socket.io socket instance.

Usage

req.socket;

Details

Warning:

req.socket may be deprecated in a future release of Sails. You should use the sails.sockets.* methods instead.

If the current request (req) did NOT originate from a Socket.io client, req.socket does not have the same meaning. In the most common scenario, HTTP requests, req.socket actually does exist, but it refers instead to the underlying TCP socket. Before using req.socket, you should check the req.isSocket flag to ensure the request arrived via a connected Socket.io client.

req.socket.id is a unique identifier representing the current socket. This is generated by the Socket.io server when a client first connects, and is a valid unique identifier until the socket is disconnected (e.g. if the client is a web browser, until the user closes her browser tab.)

Sails also provides direct, low-level access to all of the other methods and properties from a Socket.io Socket, including req.socket, including req.socket.join, req.socket.leave, req.socket.broadcast, and more. See the relevant docs in the Socket.io wiki for more information.

Example

if (req.isSocket) {
  // Low-level Socket.io methods and properties accessible on req.socket.
  // ...
}
else {
  // This is not a request from a Socket.io client, so req.socket
  // may or may not exist.  If this is an HTTP request, req.socket is actually
  // the underlying TCP socket.
  // ...
}

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-docs repo. Once we merge it, the changes will be reflected on the website the next time it is deployed.

Reference

We wrote a book!

Get early access to the book
with promotion code: mcneildoc

Get the Book