2
votes

I'm using express with socket.io and express-session with express-socket.io-session that helps me connect the session to my socket instance.

Here's the code I used for clustering

var cluster = require('cluster')

if(cluster.isMaster) {
    // imports
    const http = require('http'),
        socketIO = require('socket.io')

    var server = http.createServer(),
        io = socketIO(server)

    for (var i = 0; i < 3; i++) {
        cluster.fork()
    }

    cluster.on('exit', function(worker, code, signal) {
        console.log('worker ' + worker.process.pid + ' died code: ' + code + 'signal: '+ signal)
    })

}

if(cluster.isWorker) {
    var express = require('express'),
        app = express(),
        deploy = require('./main.js'),
        server = require('http').createServer(app),
        io = require('socket.io').listen(server)

        // add store here.

        // deploy and listen
    deploy(app, io)

    console.log(cluster.worker.id)
    app.listen(8080)
}

The Deploy Function runs the stuff like session, routes, etc.

POST http://localhost:8080/socket.io/?EIO=3&transport=polling&t=Lz9Ey8p 404 (Not Found)

1

1 Answers

1
votes

I found my answer, But I'll make it public so that in future if somebody has this problem then they can immediately set this up quickly.

You need to use sticky cluster, refer to this config for Express.