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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T06:47:59+00:00 2026-05-31T06:47:59+00:00

Using connect, express, and socket.io, I’m trying to allow my application to grab the

  • 0

Using connect, express, and socket.io, I’m trying to allow my application to grab the session details when reconnecting. My sessions obviously work while the client is connected, but if I refresh the page on my browser, it forgets everything.
My session cookie is definitely the same, so it’s not that.

My code’s a big mish-mash of snippets I’ve taken from a number of different sources, since there doesn’t seem to be one complete example application out there.

What am I missing..?

var qs      = require('querystring'),
    express = require('express'),
    app     = express.createServer(),
    io      = require('socket.io').listen(app.listen(8000)),
    routes  = require('./routes'),
    pCookie = require('connect').utils.parseCookie,
    Session = require('connect').middleware.session.Session,
    RedStore= require('connect-redis')(express),
    sStore  = new RedStore();
   
// Configuration

app.configure(function(){
  app.set('views', __dirname + '/views');
  app.set('view engine', 'ejs');
  app.use(express.bodyParser());
  app.use(express.methodOverride());
  app.use(express.cookieParser());
  app.use(express.session({ store: sStore, secret: 'tehsecretz' }));
  app.use(app.router);
  app.use(express.static(__dirname + '/public'));
});

app.configure('development', function(){
  app.use(express.errorHandler({ dumpExceptions: true, showStack: true }));
});

app.configure('production', function(){
  app.use(express.errorHandler());
});

// Routes

app.get('/', routes.index);

io.sockets.on('connection', function (client) {
    var hs = client.handshake,
        session = hs.session;

    console.log('A socket connected called: ' + hs.sessionId);

    var intervalId = setInterval(function() {
        hs.session.reload(function() {
            hs.session.touch().save();
        });
    }, 60 * 1000);

    if (!session.userName) {
        // Prompts the user for a name on the frontend
        client.emit('need-to-register');
    }

    client.on('message', function(msg, c) {
        console.log(session);
        console.log(msg);
    });

    client.on('register-user', function(data, fn) {
        // This retrieves the user's name
        // and - hopefully - saves it to the session.

        data = qs.parse(data);
        session.userName = data.username;

        hs.session.save();
        console.log('Saving: ', session);

        fn('ok');
    });

    client.on('disconnect', function() {
        clearInterval(intervalId);
    });
});

io.set('authorization', function (data, accept) {
    if (data.headers.cookie) {
        data.cookie = pCookie(data.headers.cookie);
        data.sessionId = data.cookie['connect.sid'];

        data.sessionStore = sStore;
        sStore.get(data.sessionId, function (err, session) {
            if (err || !session) {
                accept('Error', false);
            } else {
                console.log(data.sessionId, session);
                data.session = new Session(data, session);
                accept(null, true);
            }
        });
    } else {
        return accept('No cookie transmitted', false);
    }
});
  • 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-31T06:48:00+00:00Added an answer on May 31, 2026 at 6:48 am

    Ugh. So in Daniel Baulig’s post on the subject, he referenced the identifier sessionID. I figured that was just poor convention (as I’m used to camelCase) and promptly changed it to sessionId in my code.

    As I was debugging, I turned on MONITORing on my redis instance and noticed the session was being written to sess:undefined.

    Turns out sessionID is special and is referenced internally as the actual identifier. Changing all instances of sessionId to sessionID allows me to have a better clue as to who is connecting!

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

Sidebar

Related Questions

I'm trying to connect a C# application (using Visual C# 2008 Express Edition) to
I'm using Express with connect-redis session store And I tied it with Socket.IO through
I'm using express and adding in basic authentication via the connect middleware. I'm trying
I'm using express.session and connect-mongo be my DataStore. What I'm facing is... in my
i'm using node 0.4.11 express 2.4.6 mongodb 1.8.3 mongoose 2.1.2 connect-mongodb 1.0.0 and trying
I'm trying to get a VB.net (express 2010) app to connect to a socket
I am trying to connect to SQL Server Express locally using VB.NET 2005. I
I just submitted my iPhone application using iTunes Connect, but I don't see anywhere
I'm trying to get a list of user's friends using fb connect for the
I uploading the images using connect-form and express as in https://github.com/visionmedia/express/blob/cbdd907393e6fc9cf4eb092237241d1626a9a98d/examples/multipart/app.js . The images

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.