集群适配器
工作原理
¥How it works
集群适配器允许在 Node.js 集群 内使用 Socket.IO。
¥The Cluster adapter allows to use Socket.IO within a Node.js cluster.
发送到多个客户端(例如 io.to("room1").emit()
或 socket.broadcast.emit()
)的每个数据包也会通过 IPC 通道发送到其他工作进程。
¥Every packet that is sent to multiple clients (e.g. io.to("room1").emit()
or socket.broadcast.emit()
) is also sent to other workers via the IPC channel.
该适配器的源代码可以找到 此处。
¥The source code of this adapter can be found here.
支持的功能
¥Supported features
特性 | socket.io 版本 | 支持 |
---|---|---|
套接字管理 | 4.0.0 | :白色复选标记:是(自版本 0.1.0 起) |
服务器间通信 | 4.1.0 | :白色复选标记:是(自版本 0.1.0 起) |
广播并致谢 | 4.5.0 | :白色复选标记:是(自版本 0.2.0 起) |
连接状态恢复 | 4.6.0 | :X:不 |
安装
¥Installation
npm install @socket.io/cluster-adapter
用法
¥Usage
使用 Node.js 集群
¥With Node.js cluster
const cluster = require("cluster");
const http = require("http");
const { Server } = require("socket.io");
const numCPUs = require("os").cpus().length;
const { setupMaster, setupWorker } = require("@socket.io/sticky");
const { createAdapter, setupPrimary } = require("@socket.io/cluster-adapter");
if (cluster.isMaster) {
console.log(`Master ${process.pid} is running`);
const httpServer = http.createServer();
// setup sticky sessions
setupMaster(httpServer, {
loadBalancingMethod: "least-connection",
});
// setup connections between the workers
setupPrimary();
// needed for packets containing buffers (you can ignore it if you only send plaintext objects)
// Node.js < 16.0.0
cluster.setupMaster({
serialization: "advanced",
});
// Node.js > 16.0.0
// cluster.setupPrimary({
// serialization: "advanced",
// });
httpServer.listen(3000);
for (let i = 0; i < numCPUs; i++) {
cluster.fork();
}
cluster.on("exit", (worker) => {
console.log(`Worker ${worker.process.pid} died`);
cluster.fork();
});
} else {
console.log(`Worker ${process.pid} started`);
const httpServer = http.createServer();
const io = new Server(httpServer);
// use the cluster adapter
io.adapter(createAdapter());
// setup connection with the primary process
setupWorker(io);
io.on("connection", (socket) => {
/* ... */
});
}
使用 PM2.5
¥With PM2
参见 相关文档。
¥See the associated documentation.
使用 recluster
¥With recluster
cluster.js
const cluster = require("cluster");
const http = require("http");
const { setupMaster } = require("@socket.io/sticky");
const { setupPrimary } = require("@socket.io/cluster-adapter");
const recluster = require("recluster");
const path = require("path");
const httpServer = http.createServer();
// setup sticky sessions
setupMaster(httpServer, {
loadBalancingMethod: "least-connection",
});
// setup connections between the workers
setupPrimary();
// needed for packets containing buffers (you can ignore it if you only send plaintext objects)
// Node.js < 16.0.0
cluster.setupMaster({
serialization: "advanced",
});
// Node.js > 16.0.0
// cluster.setupPrimary({
// serialization: "advanced",
// });
httpServer.listen(3000);
const balancer = recluster(path.join(__dirname, "worker.js"));
balancer.run();
worker.js
const http = require("http");
const { Server } = require("socket.io");
const { setupWorker } = require("@socket.io/sticky");
const { createAdapter } = require("@socket.io/cluster-adapter");
const httpServer = http.createServer();
const io = new Server(httpServer);
// use the cluster adapter
io.adapter(createAdapter());
// setup connection with the primary process
setupWorker(io);
io.on("connection", (socket) => {
/* ... */
});
选项
¥Options
名称 | 描述 | 默认值 |
---|---|---|
requestsTimeout | 服务器间请求(例如带有 ack 的 fetchSockets() 或 serverSideEmit() )的超时 | 5000 |
最新版本
¥Latest releases
版本 | 发布日期 | 发行说明 | 差异 |
---|---|---|---|
0.2.2 | 2022 年 3 月 | link | 0.2.1...0.2.2 |
0.2.1 | 2022 年 10 月 | link | 0.2.0...0.2.1 |
0.2.0 | 2022 年 4 月 | link | 0.1.0...0.2.0 |
0.1.0 | 2021 年 6 月 | link |