1

I am running a nodejs cluster with socket.io library. I am proxing the request via nginx server. Websocket connections are working fine in most of the cases but it is not working with polling as transport protocol in socket.io.

Here's the pseudo code:

if (cluster.isMaster) {
  // Fork workers.
 for (var i = 0; i < numCPUs-1; i++) {
    cluster.fork();
  }
 else{
 var server = http.createServer(function(request, response){
    console.log('Connecting server');
    response.end();
});

server.listen(port);
var socket_io = require('socket.io')(server);
var redis_adapter = require('socket.io-redis');
//config.redis.ip,config.redis.port
socket_io.adapter(redis_adapter({ host: config.redis.ip, port: config.redis.port }));

socket_io.use(function(socket, next){
});


socket_io.on('connection', function (socket) {
};

Would like to know is there any issue in the code. Do we need to use sticky-session .If yes what is correct way to use it in my case.

1 Answer 1

4

websockets are sticky by default.

The problem with polling as transport is that your connection request is going to worker1 whereas handshake or subsequents requests might land to some other worker..lets say worker2. And this worker2 will have no information about this socket connection which was established with worker1.

if your socket.io server is a direct deployment, you can use sticky-session(http://socket.io/docs/using-multiple-nodes/).

But if you socket.io server is behind a proxy server, sticky session might not work in an efficient manner as sticky-server balances request as per remote ip address. And in case of proxy server, all the request will eventually fall on same worker and you rest of the workers will be ideal.

In this case, one possible solution is to start you socket.io workers on multiple ports and do the cluster balancing in nginx/apache. Contact for any further help.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.