Server Installation
Prerequisites#
Please make sure that Node.js is installed on your system. The current Long Term Support (LTS) release is an ideal starting point, see here.
info
At least Node.js 10 is needed, older versions are not supported anymore.
Installation#
To install the latest release:
- NPM
- Yarn
- pnpm
npm install socket.ioyarn add socket.iopnpm add socket.ioTo install a specific version:
- NPM
- Yarn
- pnpm
npm install socket.io@versionyarn add socket.io@versionpnpm add socket.io@versionLatest releases#
- 4.4.1 (2022-01-06): GitHub release / diff / npm
- 4.4.0 (2021-11-18): GitHub release / diff / npm
- 4.3.2 (2021-11-08): GitHub release / diff / npm
- 4.3.1 (2021-10-17): GitHub release / diff / npm
- 4.3.0 (2021-10-15): GitHub release / diff / npm
- 4.2.0 (2021-08-30): GitHub release / diff / npm
Additional packages#
By default, Socket.IO use the WebSocket server provided by the ws package.
There are 2 optional packages that can be installed alongside this package. These packages are binary add-ons which improve certain operations. Prebuilt binaries are available for the most popular platforms so you don't necessarily need to have a C++ compiler installed on your machine.
- bufferutil: Allows to efficiently perform operations such as masking and unmasking the data payload of the WebSocket frames.
- utf-8-validate: Allows to efficiently check if a message contains valid UTF-8 as required by the spec.
To install those packages:
- NPM
- Yarn
- pnpm
npm install --save-optional bufferutil utf-8-validateyarn add --optional bufferutil utf-8-validatepnpm add -O bufferutil utf-8-validatePlease note that these packages are optional, the WebSocket server will fallback to the Javascript implementation if they are not available. More information can be found here.
Other WebSocket server implementations#
Any Websocket server implementation which exposes the same API as ws (notably the handleUpgrade method) can be used.
For example, you can use the eiows package, which is a fork of the (now deprecated) uws package:
- NPM
- Yarn
- pnpm
npm install eiowsyarn add eiowspnpm add eiowsAnd then use the wsEngine option:
const { Server } = require("socket.io");const eiows = require("eiows");
const io = new Server(3000, { wsEngine: eiows.Server});This implementation "allows, but doesn't guarantee" significant performance and memory-usage improvements over the default implementation. As usual, please benchmark it against your own usage.
Usage with uWebSocket.js#
Starting with version 4.4.0, a Socket.IO server can now bind to a uWebSocket.js server.
Installation:
- NPM
- Yarn
- pnpm
npm install uWebSockets.js@uNetworking/uWebSockets.js#v20.4.0yarn add uWebSockets.js@uNetworking/uWebSockets.js#v20.4.0pnpm add uWebSockets.js@uNetworking/uWebSockets.js#v20.4.0Usage:
const { App } = require("uWebSockets.js");const { Server } = require("socket.io");
const app = new App();const io = new Server();
io.attachApp(app);
io.on("connection", (socket) => { // ...});
app.listen(3000, (token) => { if (!token) { console.warn("port already in use"); }});Miscellaneous#
Dependency tree#
A basic installation of the server includes 23 packages:
โโโฌ socket.io@4.4.1 โโโฌ accepts@1.3.7 โ โโโฌ mime-types@2.1.34 โ โ โโโ mime-db@1.51.0 โ โโโ negotiator@0.6.2 โโโ base64id@2.0.0 โโโฌ debug@4.3.3 โ โโโ ms@2.1.2 โโโฌ engine.io@6.1.1 โ โโโ @types/cookie@0.4.1 โ โโโ @types/cors@2.8.12 โ โโโ @types/node@17.0.8 โ โโโ accepts@1.3.7 deduped โ โโโ base64id@2.0.0 deduped โ โโโ cookie@0.4.1 โ โโโฌ cors@2.8.5 โ โ โโโ object-assign@4.1.1 โ โ โโโ vary@1.1.2 โ โโโ debug@4.3.3 deduped โ โโโฌ engine.io-parser@5.0.2 โ โ โโโ base64-arraybuffer@1.0.1 โ โโโฌ ws@8.2.3 โ โโโ UNMET OPTIONAL DEPENDENCY bufferutil@^4.0.1 โ โโโ UNMET OPTIONAL DEPENDENCY utf-8-validate@^5.0.2 โโโ socket.io-adapter@2.3.3 โโโฌ socket.io-parser@4.0.4 โโโ @types/component-emitter@1.2.11 โโโ component-emitter@1.3.0 โโโ debug@4.3.3 dedupedinfo
The type declarations for 3rd party packages are included, in order to ease the use of the library for TypeScript users (but at the cost of a slightly-larger package).
See also: https://github.com/microsoft/types-publisher/issues/81#issuecomment-234051345
Transitive versions#
The engine.io package brings the engine that is responsible for managing the low-level connections (HTTP long-polling or WebSocket). See also: How it works
socket.io version | engine.io version | ws version |
|---|---|---|
4.4.x | 6.1.x | 8.2.x |
4.3.x | 6.0.x | 8.2.x |
4.2.x | 5.2.x | 7.4.x |
4.1.x | 5.1.x | 7.4.x |
4.0.x | 5.0.x | 7.4.x |
3.1.x | 4.1.x | 7.4.x |
3.0.x | 4.0.x | 7.4.x |
2.4.x | 3.5.x | 7.4.x |