-
Notifications
You must be signed in to change notification settings - Fork 112
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
Comments
@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 |
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 |
This is not working for me, TypeError: Cannot read property 'url' of undefined, some idea? |
@albertogasparin it's working! Thank you, mate, i lost a lot of time before found your solution! ❤️ |
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? |
@thanhlq 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
}
// ...
}) |
@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'." |
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);
}); |
Question is in the title, just wondering if there is a function like
session.getSesh(seshid).userid
or somethingThe text was updated successfully, but these errors were encountered: