Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using pm2's cluster module with socket.io and socket.io-redis

I'm trying to make pm2 cluster module work with socketio, but I get errors when I visit /hello. Given that cluster utilizes all CPU cores, socketio gets confused, so I installed socket.io-redis module to take care of this, but it looks like it doesn't do anything at all related to Redis (it's empty) or socket.io, I'm probably missing something very obvious.

I use Nginx as a web server and my express server, localhost:8000 points to localhost:80.

Checks :

  • Nginx works, http://localhost/hello returns socketio's response, I see it on browser console.

  • Redis instance is on

It works if I start my server with npm start without using cluster, but sudo pm2 start bin/www -i 0 produces the error at the bottom of the question.

BACKEND

Here's my 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 (I only added "ioe", everything else is the same).

#!/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);
}

FRONTEND

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'
    });
});

Errors

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)

If I change io.connect url from http://localhost to http://127.0.0.1:8000, socketio fires the text multiple times and I get different kind of errors.

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"}
like image 577
salep Avatar asked Oct 31 '22 04:10

salep


2 Answers

The socket.io handshake requires sticky sessions. Limit the socket.io transports to websockets only (disable polling) or implement sticky sessions.

like image 52
Andreas Avatar answered Nov 12 '22 18:11

Andreas


I had exactly the same problem.

There is actually an entire section now in socket.io devoted to making socket.io work when using multiple workers. https://socket.io/docs/v4/using-multiple-nodes/

The simplest and fastest solution is to disable the HTTP long-polling transport like this:

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

The other solution is to implement sticky sessions and use the redis adapter for socket.io. You can read more about this in the link above.

Hope this helps anyone :)

like image 25
Anatol Avatar answered Nov 12 '22 18:11

Anatol