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

scaling socket.io cookie problem #325

Closed
mkuklis opened this issue Nov 2, 2012 · 1 comment
Closed

scaling socket.io cookie problem #325

mkuklis opened this issue Nov 2, 2012 · 1 comment

Comments

@mkuklis
Copy link

mkuklis commented Nov 2, 2012

Hi guys I have a very similar scenario to #202 but based on cookies. For some reason the cookie in on('upgrade', function (req, socket, head) is always undefined. I was wondering if somebody here could point me into the right direction. Here is my current setup:

Balancer:

var http = require('http');
var httpProxy = require('http-proxy');
var uuid = require('node-uuid');
var Cookies = require('cookies');

var addresses = [
  { host: '127.0.0.1', port: 8090 },
  { host: '127.0.0.1', port: 8091 }];

var proxies = addresses.map(function(address) {
  return new httpProxy.HttpProxy({ xforward: true, target: address });
});

var proxiesHash = {};

var balancer = http.createServer(function (req, res) {
  var cookies = new Cookies( req, res);
  var cookie = cookies.get('ws');

  if (!cookie) {
    cookie = uuid.v1();
    cookies.set('ws', cookie);
  }

  if (!proxiesHash[cookie]) {
    var proxy = proxies.shift();
    proxiesHash[cookie] = proxy;
    proxies.push(proxy);
  }
  proxiesHash[cookie].proxyRequest(req, res);
});

balancer.on('upgrade', function (req, socket, head) {
  var cookies = new Cookies(req);
  var cookie = cookies.get('ws'); // always undefined
  if (!proxiesHash[cookie]) return;
  proxiesHash[cookie].proxyWebSocketRequest(req, socket, head);
});

balancer.listen(8000);

Server:

var redis = require('redis');
var sio = require('socket.io');
var RedisStore = sio.RedisStore;
var port = parseInt(process.argv[2]) || 8090;
var io = sio.listen(port);

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

io.sockets.on('connection', function (socket) {
  socket.on('ping', function (data) {
    socket.broadcast.emit('pong', data);
  });
});

Client:

var port = 8000;
var io = require('socket.io-client');
var socket = io.connect('http://127.0.0.1:' + port);

socket.on('connect', function () {
  socket.emit('ping', 'hi from client: ' + port);
});

socket.on('pong', function( data) {
  console.log(data);
});
@indexzero
Copy link
Contributor

Check the headers. if the cookie header isn't set then this is a core issue.

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

2 participants