Skip to content

Socket.io wrong data being sent to socket (possible ID collisions) #2726

@dvideby0

Description

@dvideby0

We are currently experiencing an issue with our web application where the wrong data is being sent to a socket. Based on what we are seeing, it appears there might be socketID collisions because of our multiple server setup but we have not been able to confirm yet.

The behavior

Users that are joined to 1 room will start receiving messages from another room (that they have not been joined to) at random. When they refresh, they are back to getting the messages that they were supposed to be getting.

Our Configuration

var express = require('express')();
var api = require('http').createServer(express);

/*
 * Configure socket.io.
 */
var socketio = require('socket.io')(api, {
  serveClient: true,
  transports: ['websocket']
});
var redis = require('redis').createClient;
var adapter = require('socket.io-redis');
var pub = redis(appModules.config.redis.clients.socketio.port, appModules.config.redis.clients.socketio.host, {
  return_buffers: true,
  auth_pass: appModules.config.redis.password
});
var sub = redis(appModules.config.redis.clients.socketio.port, appModules.config.redis.clients.socketio.host, {
  return_buffers: true,
  auth_pass: appModules.config.redis.password
});
socketio.adapter(adapter({
  host: appModules.config.redis.clients.socketio.host,
  port: appModules.config.redis.clients.socketio.port,
  pubClient: pub,
  subClient: sub
}));
socketio.on('connection', function(socket) {
  socket.emit('connection-event', 'socket connection successful for socket: ' + socket.id);
  socket.on('disconnect', function() {
    require('../../main_socket/v1/disconnect')(this);
  });
  socket.on('reconnect', function() {
    require('../../main_socket/v1/reconnect')(this);
  });
  socket.on('client-event', function(data) {
    data = JSON.parse(data);
    appModules.shared.session.decryptToken(data.token)
      .then(function(jwt) {
        appModules.services.redis.clients.user_sessions.hgetallAsync(jwt.decodedToken.email)
          .then(function(session) {
            data.session = session;
            require('../../main_socket/' + data.apiVersion + '/' + data.context + '/' + data.action)(data, socket);
          })
          .catch(function(err) {
            logger.error(err);
          });
      })
      .catch(function(err) {
        logger.error(err);
      });
  });
});

Assumption

Since refreshing seems to fix the issue for a specific user, we assume it is a socketID collision since they will get a new one on refresh.

Additional Notes

We are hosting our app on Heroku and followed their instructions for session affinity (https://devcenter.heroku.com/articles/session-affinity) but this did not seem to make any impact on the issue.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions