Cesar Sanchez
Cesar Sanchez

Reputation: 13

Socket.IO 1.X , Express 4, Cluster

I'm using the socket it 1.X and express 4 and cluster for a server app only advantage to you sometimes in the client 's session is lost SOCKET IO does not understand why

app.js

var app = require('express')();
var http = require('http').Server(app);
var redis = require('socket.io-redis');
var cluster = require('cluster');
// var sessionStore = new (require('connect-mongo')(express))({ db: "test" });
if(cluster.isMaster ){
    // Count the machine's CPUs
    var cpuCount = require('os').cpus().length;
    console.log( "CORES : " + cpuCount );

    // Create a worker for each CPU
    for (var i = 0; i < cpuCount; i += 1) {
        cluster.fork();
    }

    // Listen for dying workers
    cluster.on('exit', function (worker) {

        // Replace the dead worker,
        // we're not sentimental
        console.log('Worker %d died :(', worker.id);
        cluster.fork();

    });
} else {


    var io = require('socket.io').listen(http);
    io.adapter(redis({ host: 'localhost', port: 6379 }));


    app.get('/', function(req, res){
      res.sendFile(__dirname + '/index.html');
    });

    io.on('connection', function(socket){
      socket.on('chat message', function(msg){
        io.emit('chat message', msg);
      });
    });

    http.listen(3000, function(){
      console.log('listening on *:3000');
    });
}

That's my code and do not understand that sometimes I am wrong and the client returns this error

{"code":1,"message":"Session ID unknown"}

Which all my events are not issued and my app does not continue correctly

Thanks

Upvotes: 1

Views: 109

Answers (0)

Related Questions