Server options
Socket.IO server optionsβ
path
β
Default value: /socket.io/
It is the name of the path that is captured on the server side.
caution
The server and the client values must match (unless you are using a path-rewriting proxy in between).
Server
import { createServer } from "http";
import { Server } from "socket.io";
const httpServer = createServer();
const io = new Server(httpServer, {
path: "/my-custom-path/"
});
Client
import { io } from "socket.io-client";
const socket = io("https://example.com", {
path: "/my-custom-path/"
});
serveClient
β
Default value: true
Whether to serve the client files. If true
, the different bundles will be served at the following location:
<url>/socket.io/socket.io.js
<url>/socket.io/socket.io.min.js
<url>/socket.io/socket.io.msgpack.min.js
(including their associated source maps)
See also here.
adapter
β
Default value: require("socket.io-adapter")
(in-memory adapter, whose source code can be found here)
The "Adapter" to use.
Example with the Redis adapter:
- CommonJS
- ES modules
- TypeScript
const { Server } = require("socket.io");
const { createAdapter } = require("@socket.io/redis-adapter");
const { createClient } = require("redis");
const pubClient = createClient({ host: "localhost", port: 6379 });
const subClient = pubClient.duplicate();
const io = new Server({
adapter: createAdapter(pubClient, subClient)
});
io.listen(3000);
import { Server } from "socket.io";
import { createAdapter } from "@socket.io/redis-adapter";
import { createClient } from "redis";
const pubClient = createClient({ host: "localhost", port: 6379 });
const subClient = pubClient.duplicate();
const io = new Server({
adapter: createAdapter(pubClient, subClient)
});
io.listen(3000);
import { Server } from "socket.io";
import { createAdapter } from "@socket.io/redis-adapter";
import { createClient } from "redis";
const pubClient = createClient({ host: "localhost", port: 6379 });
const subClient = pubClient.duplicate();
const io = new Server({
adapter: createAdapter(pubClient, subClient)
});
io.listen(3000);
parser
β
Default value: socket.io-parser
The parser to use. Please see the documentation here.
connectTimeout
β
Default value: 45000
The number of ms before disconnecting a client that has not successfully joined a namespace.
Low-level engine optionsβ
pingTimeout
β
Default value: 20000
This value is used in the heartbeat mechanism, which periodically checks if the connection is still alive between the server and the client.
The server sends a ping, and if the client does not answer with a pong within pingTimeout
ms, the server considers that the connection is closed.
Similarly, if the client does not receive a ping from the server within pingInterval + pingTimeout
ms, the client also considers that the connection is closed.
In both cases, the disconnection reason will be: ping timeout
socket.on("disconnect", (reason) => {
console.log(reason); // "ping timeout"
});
Note: the default value might be a bit low if you need to send big files in your application. Please increase it if that's the case:
const io = new Server(httpServer, {
pingTimeout: 30000
});
pingInterval
β
Default value: 25000
See above.
upgradeTimeout
β
Default value: 10000
This is the delay in milliseconds before an uncompleted transport upgrade is cancelled.
maxHttpBufferSize
β
Default value: 1e6
(1 MB)
This defines how many bytes a single message can be, before closing the socket. You may increase or decrease this value depending on your needs.
const io = new Server(httpServer, {
maxHttpBufferSize: 1e8
});
It matches the maxPayload option of the ws package.
allowRequest
β
Default: -
A function that receives a given handshake or upgrade request as its first parameter, and can decide whether to continue or not.
Example:
const io = new Server(httpServer, {
allowRequest: (req, callback) => {
const isOriginValid = check(req);
callback(null, isOriginValid);
}
});
transports
β
Default value: ["polling", "websocket"]
The low-level transports that are allowed on the server-side.
See also: client-side transports
allowUpgrades
β
Default value: true
Whether to allow transport upgrades.
perMessageDeflate
β
History
Version | Changes |
---|---|
v3.0.0 | The permessage-deflate extension is now disabled by default. |
v1.4.0 | First implementation. |
Default value: false
Whether to enable the permessage-deflate extension for the WebSocket transport. This extension is known to add a significant overhead in terms of performance and memory consumption, so we suggest to only enable it if it is really needed.
Please note that if perMessageDeflate
is set to false
(which is the default), the compress flag used when emitting (socket.compress(true).emit(...)
) will be ignored when the connection is established with WebSockets, as the permessage-deflate extension cannot be enabled on a per-message basis.
All options from the ws
module are supported:
const io = new Server(httpServer, {
perMessageDeflate: {
threshold: 2048, // defaults to 1024
zlibDeflateOptions: {
chunkSize: 8 * 1024, // defaults to 16 * 1024
},
zlibInflateOptions: {
windowBits: 14, // defaults to 15
memLevel: 7, // defaults to 8
},
clientNoContextTakeover: true, // defaults to negotiated value.
serverNoContextTakeover: true, // defaults to negotiated value.
serverMaxWindowBits: 10, // defaults to negotiated value.
concurrencyLimit: 20, // defaults to 10
}
});
httpCompression
β
Added in v1.4.0
Default value: true
Whether to enable the compression for the HTTP long-polling transport.
Please note that if httpCompression
is set to false
, the compress flag used when emitting (socket.compress(true).emit(...)
) will be ignored when the connection is established with HTTP long-polling requests.
All options from the Node.js zlib
module are supported.
Example:
const io = new Server(httpServer, {
httpCompression: {
// Engine.IO options
threshold: 2048, // defaults to 1024
// Node.js zlib options
chunkSize: 8 * 1024, // defaults to 16 * 1024
windowBits: 14, // defaults to 15
memLevel: 7, // defaults to 8
}
});
wsEngine
β
Default value: require("ws").Server
(source code can be found here)
The WebSocket server implementation to use. Please see the documentation here.
Example:
const io = new Server(httpServer, {
wsEngine: require("eiows").Server
});
cors
β
Default value: -
The list of options that will be forwarded to the cors
module. More information can be found here.
Example:
const io = new Server(httpServer, {
cors: {
origin: ["https://example.com", "https://dev.example.com"],
allowedHeaders: ["my-custom-header"],
credentials: true
}
});
cookie
β
Default value: -
The list of options that will be forwarded to the cookie
module. Available options:
- domain
- encode
- expires
- httpOnly
- maxAge
- path
- sameSite
- secure
Example:
import { Server } from "socket.io";
const io = new Server(httpServer, {
cookie: {
name: "my-cookie",
httpOnly: true,
sameSite: "strict",
maxAge: 86400
}
});
info
Since Socket.IO v3, there is no cookie sent by default anymore (reference).
allowEIO3
β
Default value: false
Whether to enable compatibility with Socket.IO v2 clients.
See also: Migrating from 2.x to 3.0
Example:
const io = new Server(httpServer, {
allowEIO3: true // false by default
});