skipMiddlewares:true is safe/good in socket.io?

15 views Asked by At

I'm reading the official documentation of socket.io about the Connection state recovery, which says:

const io = new Server(httpServer, {
  connectionStateRecovery: {
    // the backup duration of the sessions and the packets
    maxDisconnectionDuration: 2 * 60 * 1000,
    // whether to skip middlewares upon successful recovery
    skipMiddlewares: true,
  }
});

If I'm right, here middlewares are functions that I set that will be invoked between other socket.io core functions, like after a new client connection, is it true?

And here:

If the skipMiddlewares option is set to true, then the middlewares will be skipped when the connection is successfully recovered:

function computeUserIdFromHeaders(headers) {
  // to be implemented
}

// this middleware will be skipped if the connection is successfully recovered
io.use(async (socket, next) => {
  socket.data.userId = await computeUserIdFromHeaders(socket.handshake.headers);

  next();
});

io.on("connection", (socket) => {
  // the userId attribute will either come:
  // - from the middleware above (first connection or failed recovery)
  // - from the recevery mechanism
  console.log("userId", socket.data.userId);
});

So, while recovering the state, skipMiddlewares:true prevents middlewares from running but uses the backup packets data as-is, correct?

If so, is it a safe/good practice?

0

There are 0 answers