Registry / http-networking / express-socket.io-session

express-socket.io-session

JSON →
library1.3.5jsnpmunverified

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-session
INSTALL
IMPORT
SIG · EXPRESS-SOCKET.IO-
E
express-socket.io-session
http-networkingjavascriptv1.3.5
Install
Import
Disk
Pass rate
0/ 6
Env Coverage0 / 6
glibc
1822
musl
1822
Install & Compatibility
Where this runs
tested against v? · npm install
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
node 18226 runs
build_error
glibc
node 18226 runs
build_error
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

default
import sharedsession from 'express-socket.io-session'
const sharedsession = require('express-socket.io-session')
Package provides a CommonJS default export; using ESM import works due to Node.js interop.
sharedsession
const sharedsession = require('express-socket.io-session')
const { sharedsession } = require('express-socket.io-session')
Default export, not named. Named import will be undefined.
express-session
import session from 'express-session'
const session = require('express-session').default
express-session is a CommonJS module; both require and import work. Ensure session is configured before passing to express-socket.io-session.

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.

const express = require('express'); const http = require('http'); const socketIO = require('socket.io'); const session = require('express-session'); const sharedsession = require('express-socket.io-session'); const app = express(); const server = http.createServer(app); const io = socketIO(server); const sessionMiddleware = session({ secret: 'my-secret', resave: true, saveUninitialized: true }); app.use(sessionMiddleware); io.use(sharedsession(sessionMiddleware, { autoSave: true })); app.get('/', (req, res) => { req.session.visits = (req.session.visits || 0) + 1; res.send('Session visit count: ' + req.session.visits); }); io.on('connection', (socket) => { console.log('Socket connected. Session ID:', socket.handshake.session.id); socket.on('set-data', (data) => { socket.handshake.session.data = data; socket.handshake.session.save(); }); socket.on('get-data', () => { socket.emit('data', socket.handshake.session.data || null); }); }); server.listen(3000, () => console.log('Server listening on port 3000'));
Debug
Known issues
breakingRequires express-session instance, not the module itself
fix
Pass the result of calling express-session() (the middleware function), not the express-session module.
affects: >=1.0.0
deprecatedautoSave option is false by default; session data not saved automatically
fix
Set autoSave: true in options or call socket.handshake.session.save() explicitly after modifications.
affects: >=1.0.0
gotchaSession object is on socket.handshake.session, not socket.session
fix
Always access session via socket.handshake.session.
affects: >=1.0.0
gotchaOnly works with cookie-based sessions (express-session default store); custom stores may require extra configuration
fix
If using a custom session store, ensure it is compatible with express-session's save/load mechanisms.
affects: >=1.0.0
breakingNot compatible with socket.io v3+ because socket.io v3 changed middleware signature
fix
Use socket.io <3 or find another solution for sharing sessions (e.g., socket.io v3 emits socket instead of next function).
affects: >=1.3.5
Errors
Common errors & fixes
TypeError: session is not a function
Passed the express-session module directly instead of calling it as a function
fix
const session = require('express-session')({ secret: 'keyboard cat', resave: true, saveUninitialized: true });
socket.handshake.session is undefined
Session middleware not applied to socket.io or applied before socket.io connection
fix
Ensure io.use(sharedsession(session)); is called before io.on('connection', ...).
Cannot read property 'session' of undefined
Accessing socket.handshake.session on a socket that doesn't have handshake (e.g., before middleware runs)
fix
Access session inside socket event handlers after connection; if using namespaces, ensure sharedsession is applied to the namespace as well.
Module not found: Can't resolve 'express-socket.io-session'
Package not installed or incorrectly imported in a browser environment
fix
npm install express-socket.io-session (server-side only; not a client-side package).
TypeError: socket.handshake.session.save is not a function
autoSave not enabled and session.save() not available unless using express-session's save method (require manual save)
fix
Enable autoSave: true or use socket.handshake.session.save() after modifications (only available after session middleware runs).
Upgrade
Version history
1.3.5latest on npm
Audit
Dependencies
express-sessionrequiredPeer dependency; the shared session middleware wraps an express-session instance
Agent activity
15 hits · last 30 days
node
14
OpenAI (training)
1
Resources
express-socket.io-session — npm install express-socket.io-session · libregistry