PageRenderTime 26ms CodeModel.GetById 21ms app.highlight 3ms RepoModel.GetById 1ms app.codeStats 0ms

/reference/req/req.socket.md

https://github.com/balderdashy/sails-docs
Markdown | 46 lines | 28 code | 18 blank | 0 comment | 0 complexity | 185c6c12279ac2505949003582d06cb0 MD5 | raw file
 1# req.socket
 2
 3If the current Request (`req`) originated from a connected Socket.io client, `req.socket` refers to the raw Socket.io socket instance.
 4
 5### Usage
 6
 7```js
 8req.socket;
 9```
10
11
12### Details
13
14> **Warning:**
15> 
16> `req.socket` may be deprecated in a future release of Sails.  You should use the [`sails.sockets.*`](./#!documentation/reference/Sockets) methods instead.
17
18If 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.
19
20`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.)
21
22Sails 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](https://github.com/LearnBoost/socket.io/wiki/Rooms) for more information.
23
24
25### Example
26
27```js
28if (req.isSocket) {
29  // Low-level Socket.io methods and properties accessible on req.socket.
30  // ...
31}
32else {
33  // This is not a request from a Socket.io client, so req.socket
34  // may or may not exist.  If this is an HTTP request, req.socket is actually
35  // the underlying TCP socket.
36  // ...
37}
38```
39
40
41
42
43
44<docmeta name="uniqueID" value="reqsocket572002">
45<docmeta name="displayName" value="req.socket">
46