Comments (8)
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).
from session.
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?
from session.
@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
}
// ...
})
from session.
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);
});
from session.
@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.
from session.
This is not working for me, TypeError: Cannot read property 'url' of undefined, some idea?
from session.
@albertogasparin it's working! Thank you, mate, i lost a lot of time before found your solution! ❤️
from session.
@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'."
from session.
Related Issues (20)
- Secure cookie issue HOT 1
- Firebase Only: New session after each change to session
- what should I do to only store the signed cookie
- Mechanism to destroy all sessions?
- Issue when setting maxAge = 'session'
- Compatibility with koa-redis? HOT 1
- The cookie doesn't allow response on production environment HOT 8
- bug/question: session not being initialized from external when retrieving
- How to set cookies that are only available today?
- koa-session has been upgraded from 6.1.0 to 6.2.0 HOT 1
- externalKey参数只有在设置了custom store时才有效 HOT 1
- Cannot overwrite session in Koa/next.js
- Why is there set-cookie in every response? HOT 2
- readme link error
- error: Package subpath './v4' is not defined by "exports" in ...node_modules/uuid/package.json HOT 1
- Getting ECONNRESET in production HOT 1
- [fix] uuid version is deprecated HOT 3
- [feat] Expose the 'reload()' function to reload a session from an external store.
- [fix] Session Data Not Persisting in POST Requests in Koa Application
- Issue with "=" at the end of a koa-session encoded property HOT 5
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from session.