indutny / sticky-session

Sticky session balancer based on a `cluster` module
964 stars 99 forks source link

Constantly Connecting/Disconnecting with socket connection. #48

Open shreyaso opened 8 years ago

shreyaso commented 8 years ago

Hi, I am using node 4.2 and need to build server that would be connected to a client via socket. Since the application is not a web, my socket connection is breaking and re-connecting continuously.

Please guide me if i am doing something wrong. NOTE: I am initializing the socket io in worker.

Thanks in advance.

indutny commented 8 years ago

Hello!

Do you have a test case that could be used to reproduce the problem?

Thank you, Fedor.

shreyaso commented 8 years ago

Dear "indutny",

Sorry for the very delayed response.

But please look below for the server code:

var cluster = require('cluster');
var clusterWorkerSize = require('os').cpus().length;
var sticky = require('sticky-session');

var server = require('http').createServer(function(req, res) {
  res.end('worker: ' + cluster.worker.id);
});

if (!sticky.listen(server, 14001)) {
  console.log('master');
  server.once('listening', function() {

    console.log('server started on 14001 port');
  });

} else {

    console.log('worker: ' + cluster.worker.id);
    var io = require('socket.io')(server);

    io.on('connection', function(socket){

        var userID = socket.id;
        console.log("connected "+userID);

        socket.on('disconnect', function() {

            console.log('Got disconnect!'+userID);
        });

    });

}

But please look below for the client code:

var io = require('socket.io-client');
var serverUrl = 'http://localhost:14001';
var conn = io.connect(serverUrl);

conn.on('connect', function () {
    var sessionId = conn.io.engine.id;
    console.log(sessionId);

});
shreyaso commented 8 years ago

Hi "indutny",

Can you please help me, i am glued in this.

Thnaks

indutny commented 8 years ago

Sorry for delay on my side this time. What's expected output of this? This is what I get on the server:

master
server started on 14001 port
worker: 1
worker: 2
worker: 7
worker: 5
worker: 4
worker: 8
worker: 6
worker: 3
connected /#qLNksnI8RLnvwmE1AAAA
shreyaso commented 8 years ago

Hi ,

Your seems to be perfect, but this is what i got from server:

master
server started on 14001 port
worker: 1
worker: 2
worker: 3
worker: 4
connected /#CoOJtqZFJJuT7EqpAAAA
Got disconnect!/#CoOJtqZFJJuT7EqpAAAA
connected /#8L4eJc4MlI52DWziAAAB
Got disconnect!/#8L4eJc4MlI52DWziAAAB
connected /#tYV8z1I9JV86lqoHAAAC
Got disconnect!/#tYV8z1I9JV86lqoHAAAC

and respectively from client is :

CoOJtqZFJJuT7EqpAAAA
8L4eJc4MlI52DWziAAAB
tYV8z1I9JV86lqoHAAAC
indutny commented 8 years ago

That's pretty strange. May I ask you to run server with NODE_DEBUG="net cluster child_process" DEBUG="*" env variables and gist the results?

jqwatson commented 8 years ago

Hi,

I could replicate this behaviour in Node 0.12.4 but found that upgrading to 4.4.7 resolves the issue for me.

Jon.