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.
At least Node.js 10 is needed, older versions are not supported anymore.
To install the latest release:
npm install socket.io
yarn add socket.io
pnpm add socket.io
To install a specific version:
npm install socket.io@version
yarn add socket.io@version
pnpm add socket.io@version
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.
To install those packages:
npm install --save-optional bufferutil utf-8-validate
yarn add --optional bufferutil utf-8-validate
pnpm add -O bufferutil utf-8-validate
Please 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.
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 install eiows
yarn add eiows
pnpm add eiows
And 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.
µWebSockets.js
Starting with version 4.4.0, a Socket.IO server can now bind to a µWebSockets.js server.
Installation:
npm install uWebSockets.js@uNetworking/uWebSockets.js#v20.4.0
yarn add uWebSockets.js@uNetworking/uWebSockets.js#v20.4.0
pnpm add uWebSockets.js@uNetworking/uWebSockets.js#v20.4.0
Usage:
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");
}
});
A basic installation of the server includes 23 packages:
└─┬ [email protected] ├─┬ [email protected] │ ├─┬ [email protected] │ │ └── [email protected] │ └── [email protected] ├── [email protected] ├─┬ [email protected] │ └── [email protected] ├─┬ [email protected] │ ├── @types/[email protected] │ ├── @types/[email protected] │ ├── @types/[email protected] │ ├── [email protected] deduped │ ├── [email protected] deduped │ ├── [email protected] │ ├─┬ [email protected] │ │ ├── [email protected] │ │ └── [email protected] │ ├── [email protected] deduped │ ├─┬ [email protected] │ │ └── @socket.io/[email protected] │ └─┬ [email protected] │ ├── UNMET OPTIONAL DEPENDENCY bufferutil@^4.0.1 │ └── UNMET OPTIONAL DEPENDENCY utf-8-validate@^5.0.2 ├── [email protected] └─┬ [email protected] ├── @types/[email protected] ├── [email protected] └── [email protected] deduped
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
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.5.x |
6.2.x |
8.2.x |
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 |
© 2014–2021 Automattic
Licensed under the MIT License.
https://socket.io/docs/v4/server-installation