These configuration options provide transparent access to Socket.io, the WebSocket/pubsub server encapsulated by Sails.
Property | Type | Default | Details |
---|---|---|---|
adapter |
'memory' |
The queue socket.io will use to deliver messages. Can be set to either 'memory' or 'socket.io-redis' . If 'socket.io-redis' is specified, you should run npm install socket.io-redis@~1.0.0 --save --save-exact . |
|
transports |
['polling', 'websocket'] |
An array of allowed transport strategies. This should always match your configuration in your socket client (i.e. sails.io.js ). For help, see Configuring the sails.io.js Library. |
If you are configuring your Sails app for production and plan to scale to more than one server, then you should set sails.config.sockets.adapter
to 'socket.io-redis'
, set up your redis instance, and then use the following config to point at it from your app:
Property | Type | Default | Details |
---|---|---|---|
db |
'sails' |
The name of the database to use within your redis instance. | |
host |
'127.0.0.1' |
Hostname of your redis instance. | |
pass |
undefined |
The password for your redis instance. | |
port |
6379 |
Port of your redis instance. |
These configuration options provide lower-level access to the underlying Socket.io server settings for complete customizability.
Property | Type | Default | Details |
---|---|---|---|
afterDisconnect |
undefined |
A function to run when a client-side socket disconnects from the server. To define your own custom logic, specify a function like afterDisconnect: function (session, socket, cb) {} . |
|
allowUpgrades |
true |
This is a raw configuration option exposed from Engine.io. It indicates whether to allow Socket.io clients to upgrade the transport that they are using (e.g. start with polling, then upgrade to a true WebSocket connection). | |
beforeConnect |
undefined |
A function to run every time a new client-side socket attempts to connect to the server which can be used to reject or allow the incoming connection. Useful for tweaking your production environment to prevent DoS attacks, or reject socket.io connections based on business-specific heuristics (e.g. if stooges from a competing business create bots to post spam links about their commercial product in your chat room). To define your own custom logic, specify a function like: beforeConnect: function (handshake, cb) { /* pass back true to allow, false to deny */ return cb(null, true); } As of Sails v0.11, Sails no longer blocks incoming socket connections without cookies-- instead, cookies (and by corollary- sessions) are granted automatically. If a requesting socket.io client cannot receive a cookie (i.e. making a cross-origin socket.io connection) the sails.io.js socket client will automatically send a CORS+JSONP request to try and obtain one BEFORE CONNECTING (refer to the grant3rdPartyCookie option for details). In the antagonistic scenario where even this fails, Sails will still grant a new cookie upon connection, which allows for a one-time session. |
|
cookie |
false |
This is a raw configuration option exposed from Engine.io. It indicates the name of the HTTP cookie that contains the connecting socket.io client's socket id. The cookie will be set when responding to the initial Socket.io "handshake". Alternatively, may be set to false to disable the cookie altogether. Note that the sails.io.js client does not rely on this cookie, so it is disabled (set to false ) by default for enhanced security. If you are using socket.io directly and need to re-enable this cookie, keep in mind that the conventional setting is "io" . |
|
grant3rdPartyCookie |
false |
Whether to expose a GET /__getcookie route that sets an HTTP-only session cookie. By default, if it detects that it is about to connect to a cross-origin server, the Sails socket client (sails.io.js ) sends a JSONP request to this endpoint before it begins connecting. For user agents where 3rd party cookies are possible, this allows sails.io.js to connect the socket to the cross-origin Sails server using a user's existing session cookie, if they have one (for example, if they were already logged in.) |
|
maxHttpBufferSize |
10E7 |
This is a raw configuration option exposed from Engine.io. It reflects the maximum number of bytes or characters in a message when polling before automatically closing the socket (to avoid DoS). | |
path |
/socket.io |
Path that client-side sockets should connect to on the server. See http://socket.io/docs/server-api/#server(opts:object). | |
pingInterval |
25000 |
This is a raw configuration option exposed from Engine.io. It reflects the number of miliseconds to wait between "ping packets" (i.e. this is what "heartbeats" has become, more or less) | |
pingTimeout |
60000 |
This is a raw configuration option exposed from Engine.io. It reflects how many ms without a pong packet to wait before considering a socket.io connection closed | |
pubClient |
undefined |
When using the socket.io-redis adapter, this option allows you to specify a custom Redis client (typically created with Redis.createClient ) used for publishing on channels used by Socket.io. If unspecified, Sails will create a client for you. |
|
sendResponseHeaders |
true |
Whether to include response headers in the JWR (JSON WebSocket Response) originated for each socket request (e.g. io.socket.get() in the browser) This doesn't affect direct socket.io usage-- only if you're communicating with Sails via the request interpreter (e.g. making normal calls with the sails.io.js browser SDK). This can be useful for squeezing out more performance when tuning high-traffic apps, since it reduces total bandwidth usage. However, since Sails v0.10, response headers are trimmed whenever possible, so this option should almost never need to be used, even in extremely high-scale applications. |
|
serveClient |
false |
Whether to serve the default Socket.io client at /socket.io/socket.io.js . Occasionally useful for advanced debugging. |
|
subClient |
undefined |
When using the socket.io-redis adapter, this option allows you to specify a custom Redis client (typically created with Redis.createClient ) used for subscribing to channels used by Socket.io. If unspecified, Sails will create a client for you. |
- In older versions of Sails (<v0.11) and Socket.io (<v1.0), the
beforeConnect
setting was calledauthorization
.