Share a cookie-based express-session middleware with socket.io. Works with Express >=4.0.0 and socket.io >=1.0.0. The current stable version is 1.3.5, released 2018-07-07, with no further updates expected (last release over 6 years ago). It provides access to the Express session object via socket.handshake.session. Key differentiator: simple drop-in middleware that reuses the same express-session instance, avoiding the need for a separate session store. Alternative approaches include using JWT or custom middleware with socket.io's own session management.
npm install express-socket.io-sessionVerified import paths — ran on the pinned version, not inferred.
Sets up Express with express-session and socket.io, sharing the session via express-socket.io-session. Shows session access in both HTTP and socket.io handlers.
Pass the result of calling express-session() (the middleware function), not the express-session module.
Set autoSave: true in options or call socket.handshake.session.save() explicitly after modifications.
Always access session via socket.handshake.session.
If using a custom session store, ensure it is compatible with express-session's save/load mechanisms.
Use socket.io <3 or find another solution for sharing sessions (e.g., socket.io v3 emits socket instead of next function).
const session = require('express-session')({ secret: 'keyboard cat', resave: true, saveUninitialized: true });Ensure io.use(sharedsession(session)); is called before io.on('connection', ...).Access session inside socket event handlers after connection; if using namespaces, ensure sharedsession is applied to the namespace as well.
npm install express-socket.io-session (server-side only; not a client-side package).
Enable autoSave: true or use socket.handshake.session.save() after modifications (only available after session middleware runs).