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 7033593
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T01:05:04+00:00 2026-05-28T01:05:04+00:00

I use node.js and express, socket.io. I use session in Express. How can I

  • 0

I use node.js and express, socket.io.
I use session in Express.

How can I read the session and work with him in socket.io – in the part with connection?

store.userid is undefined.

var express = require('express')
  , stylus = require('stylus')
  , nib = require('nib')
  , sio = require('socket.io')
  , ejs = require('ejs');

store  = new express.session.MemoryStore;

app.configure(function () {

  app.use(express.bodyParser());
  app.use(express.cookieParser());
  app.use(express.session({ secret: 'secret', store: store }))

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

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

app.get('/', function(req, res) {

req.session.userid = Math.floor(Math.random()*5000);

});


var io = sio.listen(app)
  , nicknames = {};
io.configure(function () {
  io.set('transports', ['websocket','flashsocket','xhr-polling']);
});


io.sockets.on('connection', function (socket) {

  socket.emit('hello', { hello: store.userid }); //store.userid is undefined

  });

In variable store:

store =

{ sessions: { 'DNHIsZqgk53zbK3xg8qescJK.dUbdgfVq0D70UaLTMGTzO4yx5vVJral2zIhVsfFGnBA': '{"lastAccess":1326317111111,"cookie":{"originalMaxAge":14399999,"expires":"2012-01-12T01:28:17.266Z",
"httpOnly":true,"path":"/"},"userid":3528}' },
  hash: [Function],
  generate: [Function] }
  • 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-05-28T01:05:05+00:00Added an answer on May 28, 2026 at 1:05 am

    I could be wrong, but I don’t think you can (or should) rely on the http session to be populated when you run your connection event handler. They are likely running on different protocols, and the http session relies on the browser cookie (which socket.io might send when it’s doing the xhr polling, but probably won’t if it’s using a ‘real’ socket).

    I would recommend having your client JS fetch the cookie (or at least the session id) manually somehow (either by fetching the cookies using JS or if you want to write it onto the page somehow… your choice). Then, when the client makes the connection, they can pass that value up and you can use it to associate it with the session. There’s some docs on doing stuff that lasts all session on the github page; do a search for “session” here: https://github.com/learnboost/socket.io

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

Sidebar

Related Questions

I am using node.js with the express framework. As a session store I am
I'm using Express with connect-redis session store And I tied it with Socket.IO through
Can anyone use node.js (express.js or whatever) for a data-driven website like e-commerce?
How can I use XPath to select an XML-node based on its content? If
Is it safe to do something like this in Node.js/Express.js? // use Object.create(null) so
after install Express.js, I wrote this on node: var express = require('express'), app =express.createServer();
I'm using Node.js together with Express framework and i18n module . So I use
In my app (node / express / redis), I use some code to update
I'm doing my first project in node/express. I'm looking to implement a data store
The most common theme I read about why to use node.js is for high

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.