Note that there are some explanatory texts on larger screens.

plurals
  1. POexpress.session.MemoryStore not returning session?
    primarykey
    data
    text
    <p>It was really easy setting up sessions and using them in php. But my website need to deal with websockets. I am facing problem to set up sessions in node.js. I can easily push data without using sessions and it would work fine but when more than one tab is opened the new socket.id is created and previously opened tabs wont function properly. So i have been working on sessions and had problem accessing sessionstore, its logging session not grabbed. I have tried with session.load as well but no luck</p> <p>How do i get session object and use it in a way that opening other tabs wouldnt effect functionality and push data from server to client on all tabs?</p> <pre><code>var express=require('express'); var http = require('http'); var io = require('socket.io'); var cookie = require("cookie"); var connect = require("connect"), MemoryStore = express.session.MemoryStore, sessionStore = new MemoryStore(); var app = express(); app.configure(function () { app.use(express.cookieParser()); app.use(express.session({store: sessionStore , secret: 'secret' , key: 'express.sid'})); app.use(function (req, res) { res.end('&lt;h2&gt;Hello, your session id is ' + req.sessionID + '&lt;/h2&gt;'); }); }); server = http.createServer(app); server.listen(3000); sio = io.listen(server); var Session = require('connect').middleware.session.Session; sio.set('authorization', function (data, accept) { // check if there's a cookie header if (data.headers.cookie) { // if there is, parse the cookie data.cookie = connect.utils.parseSignedCookies(cookie.parse(data.headers.cookie),'secret'); // note that you will need to use the same key to grad the // session id, as you specified in the Express setup. data.sessionID = data.cookie['express.sid']; sessionStore.get(data.sessionID, function (err, session) { if (err || !session) { // if we cannot grab a session, turn down the connection console.log("session not grabbed"); accept('Error', false); } else { // save the session data and accept the connection console.log("session grabbed"); data.session = session; accept(null, true); } }); } else { // if there isn't, turn down the connection with a message // and leave the function. return accept('No cookie transmitted.', false); } // accept the incoming connection accept(null, true); }); sio.sockets.on('connection', function (socket) { console.log('A socket with sessionID ' + socket.handshake.sessionID + ' connected!'); }); </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload