Cluster 适配器

这个怎么运作

集群适配器允许在Node.js cluster中使用 Socket.IO 。

每个发送给多个客户的数据包 (例如 io.to("room1").emit()socket.broadcast.emit()) 也通过IPC渠道发送给其他客户端。

这个适配器的源代码可以在这里找到。

安装

  1. npm install @socket.io/cluster-adapter

用法

使用 Node.js cluster

  1. const cluster = require("cluster");
  2. const http = require("http");
  3. const { Server } = require("socket.io");
  4. const numCPUs = require("os").cpus().length;
  5. const { setupMaster, setupWorker } = require("@socket.io/sticky");
  6. const { createAdapter, setupPrimary } = require("@socket.io/cluster-adapter");
  7. if (cluster.isMaster) {
  8. console.log(`Master ${process.pid} is running`);
  9. const httpServer = http.createServer();
  10. // setup sticky sessions
  11. setupMaster(httpServer, {
  12. loadBalancingMethod: "least-connection",
  13. });
  14. // setup connections between the workers
  15. setupPrimary();
  16. // needed for packets containing buffers (you can ignore it if you only send plaintext objects)
  17. // Node.js < 16.0.0
  18. cluster.setupMaster({
  19. serialization: "advanced",
  20. });
  21. // Node.js > 16.0.0
  22. // cluster.setupPrimary({
  23. // serialization: "advanced",
  24. // });
  25. httpServer.listen(3000);
  26. for (let i = 0; i < numCPUs; i++) {
  27. cluster.fork();
  28. }
  29. cluster.on("exit", (worker) => {
  30. console.log(`Worker ${worker.process.pid} died`);
  31. cluster.fork();
  32. });
  33. } else {
  34. console.log(`Worker ${process.pid} started`);
  35. const httpServer = http.createServer();
  36. const io = new Server(httpServer);
  37. // use the cluster adapter
  38. io.adapter(createAdapter());
  39. // setup connection with the primary process
  40. setupWorker(io);
  41. io.on("connection", (socket) => {
  42. /* ... */
  43. });
  44. }

使用 PM2

请参阅相关文档.

使用 recluster

cluster.js

  1. const cluster = require("cluster");
  2. const http = require("http");
  3. const { setupMaster } = require("@socket.io/sticky");
  4. const { setupPrimary } = require("@socket.io/cluster-adapter");
  5. const recluster = require("recluster");
  6. const path = require("path");
  7. const httpServer = http.createServer();
  8. // setup sticky sessions
  9. setupMaster(httpServer, {
  10. loadBalancingMethod: "least-connection",
  11. });
  12. // setup connections between the workers
  13. setupPrimary();
  14. // needed for packets containing buffers (you can ignore it if you only send plaintext objects)
  15. // Node.js < 16.0.0
  16. cluster.setupMaster({
  17. serialization: "advanced",
  18. });
  19. // Node.js > 16.0.0
  20. // cluster.setupPrimary({
  21. // serialization: "advanced",
  22. // });
  23. httpServer.listen(3000);
  24. const balancer = recluster(path.join(__dirname, "worker.js"));
  25. balancer.run();

worker.js

  1. const http = require("http");
  2. const { Server } = require("socket.io");
  3. const { setupWorker } = require("@socket.io/sticky");
  4. const { createAdapter } = require("@socket.io/cluster-adapter");
  5. const httpServer = http.createServer();
  6. const io = new Server(httpServer);
  7. // use the cluster adapter
  8. io.adapter(createAdapter());
  9. // setup connection with the primary process
  10. setupWorker(io);
  11. io.on("connection", (socket) => {
  12. /* ... */
  13. });

配置

配置项描述默认值
requestsTimeout服务器间请求的超时时间,例如fetchSockets()serverSideEmit()5000