question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[typescript] share session data across express and socket.io

See original GitHub issue

Describe 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:

  1. 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 changing socket.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.
  1. 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:closed
  • Created 2 years ago
  • Comments:14 (1 by maintainers)

github_iconTop GitHub Comments

5reactions
barroudjocommented, May 5, 2021

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:

declare module 'node:http' {
  interface IncomingMessage {
    session: string
  }
}

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.

1reaction
SCdFcommented, Aug 13, 2021

@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

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to use with `express-session` | Socket.IO
There are two ways to share the session context between Express and Socket.IO, depending on your use case:
Read more >
How to share sessions with Socket.IO 1.x and Express 4.x?
The solution is surprisingly simple. It's just not very well documented. It is possible to use the express session middleware as a Socket.IO...
Read more >
@types/express-socket.io-session - npm
@types/express-socket.io-session. TypeScript icon, indicating that this package has built-in type declarations.
Read more >
Socket.io - shared session with Express : r/node - Reddit
Each time I refresh the page and emit set-data from the client, a new session doc is created in MongoDB and the sessionID...
Read more >
How To Create a Real-Time App with Socket.IO, Angular, and ...
WebSocket is the internet protocol that allows for full-duplex communication between a server and clients. This protocol goes beyond the typical ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found