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.

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

See original GitHub issue

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

Issue Analytics

  • State:open
  • Created 8 years ago
  • Comments:8

github_iconTop GitHub Comments

6reactions
albertogasparincommented, Jun 28, 2017

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).

1reaction
lastHelpcommented, Jan 22, 2020

@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
}
// ...
})
Read more comments on GitHub >

github_iconTop Results From Across the Web

The Socket instance (server-side)
A Socket is the fundamental class for interacting with the client. It inherits all the methods of the Node.js EventEmitter, like emit, on, ......
Read more >
socket.io and session? - node.js - Stack Overflow
To make it work, in the express/connect side, I explicitly define the session store so I can use it inside socket: MemoryStore =...
Read more >
The Importance of Documentation or “How i discovered how to ...
socket.request.session // Now it's available from Socket.IO sockets too! Win! }); The preceding 2 steps are the elements for the ...
Read more >
Flask-SocketIO and the User Session - miguelgrinberg.com
But any changes that are made to the Flask session through HTTP routes after the Socket.IO connection took place will not be accessible...
Read more >
The Socket.IO Server — python-socketio documentation
This instance can be transformed into a standard WSGI application by wrapping it with the ... You can define a catch-all handler using...
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