Shared users using Socket.io with http polling

141 views Asked by At

I'm trying to use Aws Lambda to my application using Socket.io with Node.js.

Because of websocket limitations on lambda, I decided to use http polling, but when the client do the first request receiving the SID, in a second request, the server throws Session id unknown, because another instance was triggered and dont have this user in memory.

I tried to use socket.io-redis as redis adapter for socket.io, I tried to share the session on redis using the express-session and express-socket.io-session but nothing works.

On my server.js I have:

io.adapter(
  redisAdapter({
    host: REDIS_URL,
    port: REDIS_PORT,
    requestsTimeout: 5000
  })
);

var redisStoreInstance = new RedisStore({
  host: REDIS_URL,
  port: REDIS_PORT,
  client: redis
});

var sessionMiddleware = session({
  store: redisStoreInstance,
  secret: "secret",
  resave: true,
  saveUninitialized: true
});

io.use(sharedsession(sessionMiddleware, {
  autoSave:true
}));

io.sockets.on('connection', (socket) => {
  socket.handshake.session.save();
});

Analysing the engine.io (lib used by socket.io) code, I saw where the exception is throw:

if (!this.clients.hasOwnProperty(sid)) {
    debug('unknown sid "%s"', sid);
    return fn(Server.errors.UNKNOWN_SID, false);
}

And this variable clients dont have a complex implementation, is only a local variable.

So, my question is:

Is possible to create a shared user store with socket.io for multiple instances? (Save on redis for example and make socket.io find the session id for this redis instance instead of memory variable).

Thanks in advance.

0

There are 0 answers