[typescript] share session data across express and socket.io
See original GitHub issueDescribe the bug I recently moved from JavaScript to TypeScript and I’m having a difficult time trying to share session data across express and socket.io.
To Reproduce
Socket.IO server version: 4.0.1
Server main.ts:
import http from 'http';
import express from 'express';
import expressSession from 'express-session';
import { Server } from 'socket.io';
const session = expressSession({
// session config...
});
const app = express();
app.use(session);
const httpServer = http.createServer(app);
const io = new Server(httpServer);
// *need to achieve session sharing here*
// rest socket.io configuration...
httpServer.listen(80, () => console.log('http listening'));
types.ts, to expand (I doubt this is the appropriate word) the session object by augmenting the module:
import 'expresss-session';
declare module 'express-session' {
interface SessionData {
username: string
}
};
The above allows me to use req.session.username
in app
callbacks.
Expected behavior
I want to be able to access express session data from a Socket
object.
Platform: Windows 10, Node v14.16.0, TypeScript v4.2.4
Additional context In JS, this is what I used:
io.use((socket, next) => session(socket.request, socket.request.res || {}, next));
I tried two options to achieve the same in TS:
- I changed the above line by little but I encountered two errors:
io.use((socket: Socket, next) => session(socket.request, socket.request.res || {}, next));
Property 'res' does not exist on type 'IncomingMessage'
, which I could fix by changingsocket.request.res || {}
to{}
. Not sure if it was mandatory or even needed as I can’t test it.Argument of type 'IncomingMessage' is not assignable to parameter of type 'Request<ParamsDictionary, any, any, ParsedQs, Record<string, any>>'
, which I could not fix at all.
- I tried using a library from NPM like so:
import sharedSession from 'express-socket.io-session';
// same code...
io.use(sharedSession(session, { autoSave: true }));
// same code...
No errors, until I try getting the value of socket.handshake.session.username
(see docs for usage), which, of course, raises an error: Property 'session' does not exist on type 'Handshake'
.
Issue Analytics
- State:
- Created 2 years ago
- Comments:14 (1 by maintainers)
Top GitHub Comments
OK just to say that there is a workaround in your case, but know that it is a little bit filthy…
What you can do is augment the IncomingMessage interface from the http module:
Since socket.io uses that definition, you will have session on it. But also every type definition that uses IncomingMessage from the http module will have it, which is maybe not something you want, or maybe it is.
@lxwang42 your link is to this ticket, the one you’re commenting on.
While I’m here, FWIW I just @ ts-ignore’d the lines where I pull things out of the session, which so far is only in 2-3 places. It’s a bit annoying, I’m not sure @Rc85 wasn’t able to actually add stuff to their session