将 pm2 的集群模块与 socket.io 和 socket.io-redis 一起使用

Posted

技术标签:

【中文标题】将 pm2 的集群模块与 socket.io 和 socket.io-redis 一起使用【英文标题】:Using pm2's cluster module with socket.io and socket.io-redis 【发布时间】:2016-08-24 18:16:15 【问题描述】:

我正在尝试使 pm2 集群模块与 socketio 一起工作,但是当我访问 /hello 时出现错误。 鉴于集群使用了所有 CPU 内核,socketio 会感到困惑,所以我安装了 socket.io-redis 模块来处理这个问题,但看起来它根本没有做任何与 Redis 相关的事情(它是空的)或socket.io,我可能遗漏了一些非常明显的东西。

我使用 nginx 作为 Web 服务器和我的 express 服务器,localhost:8000 指向 localhost:80。

检查:

Nginx 工作,http://localhost/hello 返回 socketio 的响应,我在浏览器控制台上看到了。

Redis 实例已开启

如果我在不使用集群的情况下使用npm start 启动我的服务器,但sudo pm2 start bin/www -i 0 会在问题的底部产生错误。

后台

这是我的 nginx.conf

user  myusernameishere staff;
worker_processes   1;

server 
    listen       80;
    server_name localhost;
    location / 
      proxy_set_header Upgrade $http_upgrade;
      proxy_set_header Connection "upgrade";
      proxy_http_version 1.1;
      proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
      proxy_set_header Host $host;
      proxy_pass http://localhost:8000;
  

app.js

app.get('/hello', function(req,res) 
  res.render('index');
);

io.js

var io = require('socket.io')();
var redis = require('socket.io-redis');

io.adapter(redis(
  host: '127.0.0.1',
  port: 6379
));

io.on('connection', function(socket) 
  socket.emit('news', 
    hello: 'world'
  );
  socket.on('my other event', function(data) 
    console.log(data);
  );
);

io.on('error', function() 
  console.log("errr");
);

// attach stuff to io
module.exports = io;

bin/www(我只加了“ioe”,其他都一样)。

#!/usr/bin/env node

/**
 * Module dependencies.
 */

var app = require('../app');
var ioe = require('../io');
var debug = require('debug')('test5:server');
var http = require('http');

/**
 * Get port from environment and store in Express.
 */

var port = normalizePort(process.env.PORT || '8000');
app.set('port', port);

/**
 * Create HTTP server.
 */

var server = http.createServer(app);

/**
 * Listen on provided port, on all network interfaces.
 */

server.listen(port);
ioe.attach(server);
server.on('error', onError);
server.on('listening', onListening);

/**
 * Normalize a port into a number, string, or false.
 */

function normalizePort(val) 
  var port = parseInt(val, 10);

  if (isNaN(port)) 
    // named pipe
    return val;
  

  if (port >= 0) 
    // port number
    return port;
  

  return false;


/**
 * Event listener for HTTP server "error" event.
 */

function onError(error) 
  if (error.syscall !== 'listen') 
    throw error;
  

  var bind = typeof port === 'string'
    ? 'Pipe ' + port
    : 'Port ' + port;

  // handle specific listen errors with friendly messages
  switch (error.code) 
    case 'EACCES':
      console.error(bind + ' requires elevated privileges');
      process.exit(1);
      break;
    case 'EADDRINUSE':
      console.error(bind + ' is already in use');
      process.exit(1);
      break;
    default:
      throw error;
  


/**
 * Event listener for HTTP server "listening" event.
 */

function onListening() 
  var addr = server.address();
  var bind = typeof addr === 'string'
    ? 'pipe ' + addr
    : 'port ' + addr.port;
  debug('Listening on ' + bind);

前端

index.jade

html
    head
        title Socket.io
        script(src="/socket.io/socket.io.js")
    body
        script(src="javascripts/ol.js")

ol.js

var socket = io.connect('http://localhost');
socket.on('news', function (data) 
    console.log(data);
    socket.emit('my other event', 
        my: 'data'
    );
);

错误

WebSocket connection to 'ws://localhost/socket.io/?EIO=3&transport=websocket&sid=r57-btYjx30gu9qIAAAA' failed: Error during WebSocket handshake: Unexpected response code: 502
http://localhost/socket.io/?EIO=3&transport=polling&t=LHYMof_&sid=r57-btYjx30gu9qIAAAA Failed to load resource: the server responded with a status of 400 (Bad Request)
http://localhost/socket.io/?EIO=3&transport=polling&t=LHYMogS&sid=r57-btYjx30gu9qIAAAA Failed to load resource: the server responded with a status of 400 (Bad Request)
socket.io.js:1971 WebSocket connection to 'ws://localhost/socket.io/?EIO=3&transport=websocket&sid=sXOD3N5g0MAcJmvUAAAB' failed: Error during WebSocket handshake: Unexpected response code: 502
http://localhost/socket.io/?EIO=3&transport=polling&t=LHYMot7&sid=sXOD3N5g0MAcJmvUAAAB Failed to load resource: the server responded with a status of 400 (Bad Request)
http://localhost/socket.io/?EIO=3&transport=polling&t=LHYMotO&sid=sXOD3N5g0MAcJmvUAAAB Failed to load resource: the server responded with a status of 400 (Bad Request)
socket.io.js:1971 WebSocket connection to 'ws://localhost/socket.io/?EIO=3&transport=websocket&sid=eL4_3l-I0hvuf2WlAAAC' failed: Error during WebSocket handshake: Unexpected response code: 502
socket.io.js:1456 GET http://localhost/socket.io/?EIO=3&transport=polling&t=LHYMp73&sid=eL4_3l-I0hvuf2WlAAAC 400 (Bad Request)Request.create @ socket.io.js:1456Request @ socket.io.js:1369XHR.request @ socket.io.js:1297XHR.doPoll @ socket.io.js:1327Polling.poll @ socket.io.js:1740Polling.onData @ socket.io.js:1779(anonymous function) @ socket.io.js:1330Emitter.emit @ socket.io.js:2556Request.onData @ socket.io.js:1491Request.onLoad @ socket.io.js:1572xhr.onreadystatechange @ socket.io.js:1444
socket.io.js:1456 POST http://localhost/socket.io/?EIO=3&transport=polling&t=LHYMp7N&sid=eL4_3l-I0hvuf2WlAAAC 400 (Bad Request)Request.create @ socket.io.js:1456Request @ socket.io.js:1369XHR.request @ socket.io.js:1297XHR.doWrite @ socket.io.js:1310(anonymous function) @ socket.io.js:1829(anonymous function) @ socket.io.js:3291proxy @ socket.io.js:2223(anonymous function) @ socket.io.js:3306(anonymous function) @ socket.io.js:3286exports.encodePacket @ socket.io.js:3085encodeOne @ socket.io.js:3285eachWithIndex @ socket.io.js:3304map @ socket.io.js:3311exports.encodePayload @ socket.io.js:3290Polling.write @ socket.io.js:1828close @ socket.io.js:1797Polling.doClose @ socket.io.js:1802Transport.close @ socket.io.js:841Socket.onClose @ socket.io.js:711Socket.onError @ socket.io.js:689(anonymous function) @ socket.io.js:279Emitter.emit @ socket.io.js:2556Transport.onError @ socket.io.js:814(anonymous function) @ socket.io.js:1333Emitter.emit @ socket.io.js:2556Request.onError @ socket.io.js:1502(anonymous function) @ socket.io.js:1449
socket.io.js:1971 WebSocket connection to 'ws://localhost/socket.io/?EIO=3&transport=websocket&sid=9oiUaLB-1JnMchjqAAAD' failed: Error during WebSocket handshake: Unexpected response code: 502
socket.io.js:1456 GET http://localhost/socket.io/?EIO=3&transport=polling&t=LHYMpKB&sid=9oiUaLB-1JnMchjqAAAD 400 (Bad Request)Request.create @ socket.io.js:1456Request @ socket.io.js:1369XHR.request @ socket.io.js:1297XHR.doPoll @ socket.io.js:1327Polling.poll @ socket.io.js:1740Polling.onData @ socket.io.js:1779(anonymous function) @ socket.io.js:1330Emitter.emit @ socket.io.js:2556Request.onData @ socket.io.js:1491Request.onLoad @ socket.io.js:1572xhr.onreadystatechange @ socket.io.js:1444
socket.io.js:1456 POST http://localhost/socket.io/?EIO=3&transport=polling&t=LHYMpKW&sid=9oiUaLB-1JnMchjqAAAD 400 (Bad Request)Request.create @ socket.io.js:1456Request @ socket.io.js:1369XHR.request @ socket.io.js:1297XHR.doWrite @ socket.io.js:1310(anonymous function) @ socket.io.js:1829(anonymous function) @ socket.io.js:3291proxy @ socket.io.js:2223(anonymous function) @ socket.io.js:3306(anonymous function) @ socket.io.js:3286exports.encodePacket @ socket.io.js:3085encodeOne @ socket.io.js:3285eachWithIndex @ socket.io.js:3304map @ socket.io.js:3311exports.encodePayload @ socket.io.js:3290Polling.write @ socket.io.js:1828close @ socket.io.js:1797Polling.doClose @ socket.io.js:1802Transport.close @ socket.io.js:841Socket.onClose @ socket.io.js:711Socket.onError @ socket.io.js:689(anonymous function) @ socket.io.js:279Emitter.emit @ socket.io.js:2556Transport.onError @ socket.io.js:814(anonymous function) @ socket.io.js:1333Emitter.emit @ socket.io.js:2556Request.onError @ socket.io.js:1502(anonymous function) @ socket.io.js:1449
socket.io.js:1971 WebSocket connection to 'ws://localhost/socket.io/?EIO=3&transport=websocket&sid=Rm2pZbHx6RKHMB6SAAAE' failed: Error during WebSocket handshake: Unexpected response code: 502
socket.io.js:1456 GET http://localhost/socket.io/?EIO=3&transport=polling&t=LHYMpTo&sid=Rm2pZbHx6RKHMB6SAAAE 400 (Bad Request)Request.create @ socket.io.js:1456Request @ socket.io.js:1369XHR.request @ socket.io.js:1297XHR.doPoll @ socket.io.js:1327Polling.poll @ socket.io.js:1740Polling.onData @ socket.io.js:1779(anonymous function) @ socket.io.js:1330Emitter.emit @ socket.io.js:2556Request.onData @ socket.io.js:1491Request.onLoad @ socket.io.js:1572xhr.onreadystatechange @ socket.io.js:1444
socket.io.js:1456 POST http://localhost/socket.io/?EIO=3&transport=polling&t=LHYMpU7&sid=Rm2pZbHx6RKHMB6SAAAE 400 (Bad Request)

如果我将 io.connect url 从 http://localhost 更改为 http://127.0.0.1:8000,socketio 会多次触发文本,我会收到不同类型的错误。

Object hello: "world"
socket.io.js:1456 GET http://127.0.0.1:8000/socket.io/?EIO=3&transport=polling&t=LHYM_kn&sid=BXmJUdQy1IkEsURcAAAA 400 (Bad Request)Request.create @ socket.io.js:1456Request @ socket.io.js:1369XHR.request @ socket.io.js:1297XHR.doPoll @ socket.io.js:1327Polling.poll @ socket.io.js:1740Polling.onData @ socket.io.js:1779(anonymous function) @ socket.io.js:1330Emitter.emit @ socket.io.js:2556Request.onData @ socket.io.js:1491Request.onLoad @ socket.io.js:1572xhr.onreadystatechange @ socket.io.js:1444
socket.io.js:2108 WebSocket connection to 'ws://127.0.0.1:8000/socket.io/?EIO=3&transport=websocket&sid=BXmJUdQy1IkEsURcAAAA' failed: WebSocket is closed before the connection is established.
socket.io.js:1456 POST http://127.0.0.1:8000/socket.io/?EIO=3&transport=polling&t=LHYM_lc&sid=BXmJUdQy1IkEsURcAAAA 400 (Bad Request)Request.create @ socket.io.js:1456Request @ socket.io.js:1369XHR.request @ socket.io.js:1297XHR.doWrite @ socket.io.js:1310(anonymous function) @ socket.io.js:1829(anonymous function) @ socket.io.js:3291proxy @ socket.io.js:2223(anonymous function) @ socket.io.js:3306(anonymous function) @ socket.io.js:3286exports.encodePacket @ socket.io.js:3085encodeOne @ socket.io.js:3285eachWithIndex @ socket.io.js:3304map @ socket.io.js:3311exports.encodePayload @ socket.io.js:3290Polling.write @ socket.io.js:1828close @ socket.io.js:1797Polling.doClose @ socket.io.js:1802Transport.close @ socket.io.js:841Socket.onClose @ socket.io.js:711Socket.onError @ socket.io.js:689(anonymous function) @ socket.io.js:279Emitter.emit @ socket.io.js:2556Transport.onError @ socket.io.js:814(anonymous function) @ socket.io.js:1333Emitter.emit @ socket.io.js:2556Request.onError @ socket.io.js:1502(anonymous function) @ socket.io.js:1449
ol.js:3 Object hello: "world"
socket.io.js:1456 GET http://127.0.0.1:8000/socket.io/?EIO=3&transport=polling&t=LHYM_vM&sid=acVsGs32BSeXRB6aAAAA 400 (Bad Request)Request.create @ socket.io.js:1456Request @ socket.io.js:1369XHR.request @ socket.io.js:1297XHR.doPoll @ socket.io.js:1327Polling.poll @ socket.io.js:1740Polling.onData @ socket.io.js:1779(anonymous function) @ socket.io.js:1330Emitter.emit @ socket.io.js:2556Request.onData @ socket.io.js:1491Request.onLoad @ socket.io.js:1572xhr.onreadystatechange @ socket.io.js:1444
socket.io.js:2108 WebSocket connection to 'ws://127.0.0.1:8000/socket.io/?EIO=3&transport=websocket&sid=acVsGs32BSeXRB6aAAAA' failed: WebSocket is closed before the connection is established.
ol.js:3 Object hello: "world"

【问题讨论】:

你解决了吗? 【参考方案1】:

socket.io 握手需要粘性会话。将 socket.io 传输限制为仅 websockets(禁用轮询)或实施粘性会话。

【讨论】:

【参考方案2】:

我遇到了完全相同的问题。

现在 socket.io 中有一个完整的部分专门用于在使用多个 worker 时使 socket.io 工作。 https://socket.io/docs/v4/using-multiple-nodes/

最简单最快的解决方案是像这样禁用 HTTP 长轮询传输:

// client side
const socket = io("https://example.com",  transports: ["websocket"] );

另一种解决方案是实现粘性会话并为 socket.io 使用 redis 适配器。您可以在上面的链接中阅读更多相关信息。

希望这对任何人都有帮助:)

【讨论】:

以上是关于将 pm2 的集群模块与 socket.io 和 socket.io-redis 一起使用的主要内容,如果未能解决你的问题,请参考以下文章

如何在插槽IO中使用PM2集群?

在nodeJs中将pm2与socket io集成[关闭]

如何使用 Socket.io 和 Express 进行集群

使用集群将 Socket.IO 扩展到多个 Node.js 进程时的一些问题

带有 socket.io 和 expressjs 的节点集群

在多核服务器中使用带有集群的 socket.io 的好方法?