Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8068957
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T12:51:22+00:00 2026-06-05T12:51:22+00:00

I use Express and socket.io. When you create a session with the express –

  • 0

I use Express and socket.io.

When you create a session with the express – post. Click the button and using Ajax and the POST method (login), I save a session.

 app.post('/login', function(req, res){
         req.session.loginid = req.body.lgnid;
    });

In socket.io it can not read.

My source code:

var express = require('express');
var app = express.createServer();
var io = require('socket.io').listen(app); 
var MemoryStore = express.session.MemoryStore;
var sessionStore = new MemoryStore();
var parseCookie = require('connect').utils.parseCookie;
var Session = require('connect').middleware.session.Session;
var stylus = require('stylus');
  app.use(express.bodyParser());
  app.use(express.cookieParser());
  app.use(express.session({store: sessionStore
        , secret: 'secret'
        , key: 'express.sid'}));

  app.use(stylus.middleware({ src: __dirname + '/public', compile: compile }))
  app.use(express.static(__dirname + '/public'));
  app.set('views', __dirname);

 // disable layout
  app.set("view options", {layout: false});

  // make a custom html template
  app.register('.html', {
    compile: function(str, options){
      return function(locals){
        return str;
      };
    }
  });

  function compile (str, path) {
    return stylus(str)
      .set('filename', path)
      .use(nib());
  };

var Session = require('connect').middleware.session.Session;
sio.set('authorization', function (data, accept) {
    if (data.headers.cookie) {
        data.cookie = parseCookie(data.headers.cookie);
        data.sessionID = data.cookie['express.sid'];
        // save the session store to the data object 
        // (as required by the Session constructor)
        data.sessionStore = sessionStore;
        sessionStore.get(data.sessionID, function (err, session) {
            if (err || !session) {
                accept('Error', false);
            } else {
                // create a session object, passing data as request and our
                // just acquired session data
                data.session = new Session(data, session);
                accept(null, true);
            }
        });
    } else {
       return accept('No cookie transmitted.', false);
    }
});

How to load and save data from sessionStore?

io.sockets.on('connection', function (socket) {
 
    var hs = socket.handshake;
    console.log('Session : ' + hs.session.loginid + '); //Session : undefined

});
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-05T12:51:24+00:00Added an answer on June 5, 2026 at 12:51 pm

    A recommended way to achieve this actually (at least in my experience) is to forgo the sessionStore.get function and go for sessionStore.load instead – the difference is that with load the callback is given a session instance instead of having to create one yourself. The difference is simply taking your snippet:

    sessionStore.get(data.sessionID, function (err, session) {
        if (err || !session) {
            accept('Error', false);
        } else {
            // create a session object, passing data as request and our
            // just acquired session data
            data.session = new Session(data, session);
            accept(null, true);
        }
    });
    

    And changing it to:

    sessionStore.load(data.sessionID, function(err, session) {
      if (err || !session) {
        accept("Error", false);
      } else {
        data.session = session;
        accept(null, true);
      }
    });
    

    And from there, you should be able to access the session as you attempt, such as socket.handshake.session, however when dealing with the session if you make changes in a socket event you have to manually save the session yourself. If you add/remove things you must: session.save([callback]).

    I realize this isn’t much of an answer since I’m telling you to do roughly what you already are but this is working for me right now with Express 2.5.8 and Socket.IO 0.9.6.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm using jquery mobile in my express project. How could I use Ajax for
I'm doing it like this: app.use(express.session({ cookie:{domain:.+settings.c.SITE_DOMAIN}, secret:'abc', store: redis_store, })); When I log
With the following enabled: app.use(express.session({ store: sessionStore, secret: 'secret', key: 'express.sid'})); Whilst testing my
I have this as configuration of my Express server app.use(app.router); app.use(express.cookieParser()); app.use(express.session({ secret: keyboard
I'm using express framework. I want to reach session data from socket.io. I tried
I have here is an example directly got from express document. app.use(express.cookieParser()); app.use(express.session({ secret:
I use node.js and express, socket.io. I use session in Express. How can I
I'm using Express with connect-redis session store And I tied it with Socket.IO through
I know that I can use function(req, res) { req.session } using express. However
I'm using node.js and the less compiler middleware: app.configure(function() { // ... app.use(express.compiler({ src:

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.