title: Why won't socket.io and express-session use next function
body: <p>I'm trying to use the express-session library in my MERN stack app along with socket.io for the real time messaging part of the app.</p>
  <p>For authentication I am using express-session and would like to use that same middleware for socket connections, however when I followed the answer for the same problem <a href="https://stackoverflow.com/questions/25532692/how-to-share-sessions-with-socket-io-1-x-and-express-4-x">here</a>, the middlware just simply didn't proceed to the next function and so when trying to connect, the middlware never went to the onConnect handler and the handshake never completed.</p>
  <pre><code>const day = 1000 * 60 * 60 * 24; // ms
  
  const sessionStore = MongoStore.create({
      mongoUrl: &quot;mongodb://localhost:27017/zooit&quot;,
      collection: &quot;sessions&quot;
  });
  
  // defining the session middlware from express-session
  const session = () =&gt; expressSession({
      secret: process.env.SECRET_KEY,
      resave: false,
      saveUninitialized: true,
      store: sessionStore,
      cookie: {
          maxAge: day * 30
      }
  });
  
  // session middlware fine with express
  app.use(session());
  
  io.use((socket, next) =&gt; {
      // won't use next function
      session(socket.request, {}, next);
  
      // tried doing this but then in the handler session.request.session was undefined
      // next();
  });
  
  io.on(&quot;connection&quot;, require(&quot;./socket-controllers/onConnect&quot;));
  io.on(&quot;disconnect&quot;, require(&quot;./socket-controllers/onDisconnect&quot;));
  </code></pre>
  <pre><code>// onConnect.js
  function onConnect(socket) {
      // this function isn't reached when not calling next but when explicitly calling next() socket.request.session is undefined
      console.log(`User connected: ${socket.request.session}`);
  }
  </code></pre>
  <p>If anyone has any suggestions on how I should proceed that would be great</p>