Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How might one go about accessing a session value inside of a socket.io instance? #53

Open
PanicIsReal opened this issue Jan 28, 2016 · 8 comments

Comments

@PanicIsReal
Copy link

Question is in the title, just wondering if there is a function like session.getSesh(seshid).userid or something

@hedgerh
Copy link

hedgerh commented Jun 30, 2016

@Gacnt - Did you have any luck figuring this out? I'm trying to figure out the best way to use session storage when using the ws websocket library.

@albertogasparin
Copy link

Well, I've made it work in this way:

import http from 'http';
import socketIO from 'socket.io';
import koa from 'koa';
import session from 'koa-session';

// your koa setup
const app = new koa();
app.keys = ['my-secret'];
app.use(session({}, app));

// socket.io binding
app.server = http.createServer(app.callback());
app.io = socketIO(app.server);

// socket.io middleware to set socket.session
app.io.use(function (socket, next) {
  let error = null;
  try {
    // we manually create a koa context, so we can use it to retrieve the session
    let context = app.createContext(socket.request, socket.response);
    socket.session = context.session;
  } catch (err) {
    error = err;
  }
  return next(error);
});

In this implementation, the main difference with cxt.session is that socket.session is "static": it is not a getter but it is just the returned value (something to not worry about with cookies).

@marceloch2
Copy link

This is not working for me, TypeError: Cannot read property 'url' of undefined, some idea?

@epszaw
Copy link

epszaw commented Nov 12, 2017

@albertogasparin it's working! Thank you, mate, i lost a lot of time before found your solution! ❤️

@thanhlq
Copy link

thanhlq commented Jul 24, 2018

it does NOT work (ctx.session is undefined) if I configure a session store e.g.

(Knowing that, the session store is working fine with my Rest API)

sessionConfig.store = new SessionStoreImpl(); // redis
app.use(session(sessionConfig, app));

Thanks very much for any ideas?

@lastHelp
Copy link

@thanhlq
I've found a way to fix this.
here is some code:

app.io.use(async function (socket, next) { // making io middleware async, not sure it's good idea
// ... 
try {
// ...
 const ctx = app.createContext(socket.request, new http.OutgoingMessage())
// calling initFromExternal to fetch session data from external store, if you are using other than koa-session module you have to find how to do this. 
 await ctx.session._sessCtx.initFromExternal()
 // now ctx.session is full of data
}
// ...
})

@ejose19
Copy link

ejose19 commented Jan 23, 2020

@lastHelp Your solution works great, all other "workarounds" involves too much hacking to get the session, but using .initFromExternal reuses everything already configured.

One note regarding types, I had to change new http.OutgoingMessage() to new http.ServerResponse(socket.request) or you will get "Argument of type 'OutgoingMessage' is not assignable to parameter of type 'ServerResponse'."

@ghost
Copy link

ghost commented Sep 9, 2020

Hi everyone!

Just wanted to build on the various solutions presented here to offer something that is almost equivalent to the official documentation for integrating express-session into socket.io as seen here. Idea is to just re-use the existing session middleware to populate the newly created context and to attach the session to "socket.request". Currently unsure about whether using async'ed socket.io middleware is supported or not, the docs are a bit unclear.

import http from 'http';
import socketIO from 'socket.io';
import koa from 'koa';
import session from 'koa-session';

// your koa setup
const app = new koa();
app.keys = ['my-secret'];

const sessionMiddleware = session({}, app);
app.use(sessionMiddleware);

// socket.io binding
app.server = http.createServer(app.callback());
app.io = socketIO(app.server);

// socket.io middleware to set socket.session
app.io.use(function (socket, next) {
  let error = null;
  try {
    // we manually create a koa context, so we can use it to retrieve the session
    let context = app.createContext(socket.request, socket.response);
    await sessionMiddleware(context, next);
    socket.request.session = context.session;
  } catch (err) {
    error = err;
  }
  return next(error);
});

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

8 participants