1

I need to cluster socket.IO server and want to use same port for all connections
In order to do it I create express server, configure it and then tell socket.IO to listen to this server

if(cluster.isMaster){
  var cpuCount = require('os').cpus().length;
  for (let i = 0; i < cpuCount; i += 1) {
    cluster.fork();
} else {
  const credentials = {key: privateKey, cert: certificate};
  const app = express();
  const httpsServer = https.createServer(credentials, app);
  httpsServer.listen(8003);
  const io = SocketIO.listen(httpsServer);
}

But this doesn't work for multiple servers. When I'm trying to connect to socket server I get following message:
websocket.js?13d9:112 WebSocket connection to 'wss://...' failed: Error during WebSocket handshake: Unexpected response code: 400

But in server side event listener on("connection", () => {}) works.
I have same code for creating server clusters when I render app and it works just fine.
Socket server works when there's only one socket.IO server listening to this port.

If there's a way to create multiple socket.IO listeners for one port in cluster?

1 Answer 1

3

Finally found the answer

Socket.io is doing multiple requests to perform handshake and establish connection with a client. With a cluster those requests may arrive to different workers, which will break handshake protocol.

So I decided to use sticky session lib for socket.IO and it works exactly as I wanted

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.