Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WebSocket connection to 'wss://localhost:8085/socket.io/?EIO=3&transport=websocket&sid=JTBdJ2zQXyURAijeAAAj' failed: WebSocket is closed before the connection is established. #1796

Closed
ScorpioKing opened this issue Sep 22, 2014 · 6 comments

Comments

@ScorpioKing
Copy link

socket is not getting connected giving above error in cluster mode of node js

@wstrm
Copy link

wstrm commented Sep 22, 2014

+1 Having the same problem with my Express app, using a cluster.

Tried to migrate to v1.1.0, used sticky-session (because the handshakes/connections ends up at different workers), also the docs tells me to use that.
But it seems it does not support the current node version, or am I wrong? (indutny/sticky-session#10 (comment))

Here's what I tried, but I was not able to get it working (master and all workers started, but was unable to view anything from the browser):

if (cluster.isMaster) {

  // initiate socket.io & https server to listen on
  var server = http.createServer(app);

  var io = require('socket.io')(server);

  // setup store for master
  io.adapter(redis({ host: 'localhost', port: 6379 }));
  // call socket config
  require('./config/socket')(io, app, passport);

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

};

if (cluster.isWorker) {

  // sticky-session does not work with node v0.10?
  var sticky = require('sticky-session');

  sticky(function() {

    // listen for connections
    var server = http.createServer(app);

    // initiate socket.io
    var io = require('socket.io')(server);

    // setup store for each worker (so they can communicate)
    io.adapter(redis({ host: 'localhost', port: 6379 }));

    // call socket config
    require('./config/socket')(io, app, passport);

    return server;
  }).listen(app.get('port'), function() {
    utils.log('Listening on port ' + app.get('port'));
  });
};

I also tried to remove sticky-session, and that's when I got the same problem as OP, was able to view the site, but it tried to establish several connections, and then each handshake failed.

And this is how it looked before migrating:

if (cluster.isMaster) {

  // initiate socket.io & https server to listen on
  var server = http.createServer(app);
  var io = require('socket.io').listen(server);

  // setup store for master
  io.set('store', new RedisStore({
    redisPub: redis.createClient(),
    redisSub: redis.createClient(),
    redisClient: redis.createClient()
  }));

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

if (cluster.isWorker) {

  // listen for connections
  var server = http.createServer(app).listen(app.get('port'), function () {
    utils.log('Listening on port ' + app.get('port'));
  });

  // initiate socket.io
  var io = require('socket.io').listen(server);

  // setup store for each worker (so they can communicate)
  io.set('store', new RedisStore({
    redisPub: redis.createClient(),
    redisSub: redis.createClient(),
    redisClient: redis.createClient()
  }));

  // call socket config
  require('./config/socket')(io, app, passport);

};

@jpouillard
Copy link

sticky-session don't works too for me, just use nginx sticky session or aws load balancer (http://docs.aws.amazon.com/ElasticLoadBalancing/latest/DeveloperGuide/US_StickySessions.html)

@wstrm
Copy link

wstrm commented Sep 25, 2014

OK I'll try that, thanks!

@peteruithoven
Copy link

@willeponken, not that I'm an expert but I don't believe that's how you use sticky-session. The sticky-session instance already creates clusters.
HAProxy is also something people advice, it can do the sticky sessions, load balancing and it can do ssl termination.

@wstrm
Copy link

wstrm commented Sep 27, 2014

@peteruithoven You're right, removed my own clustering and it works. Going the HAProxy or nginx way instead though.

@objectiveSee
Copy link

Here is an example of the chat-example from Socket.IO modified to work with cluster and the sticky-session module. The sticky-session module will create slaves for you and then route all requests through master to the slave which the client was originally connected to (via IP address hashing).

https://github.com/evilstudios/chat-example-cluster

This issue can be closed.

@rauchg rauchg closed this as completed Nov 25, 2014
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants