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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T19:41:28+00:00 2026-06-14T19:41:28+00:00

I am attempting to combine Express (3.x) with NowJS (which is largely a wrapper

  • 0

I am attempting to combine Express (3.x) with NowJS (which is largely a wrapper around Socket.io) to create a real-time application that first requires that you log in. I’m admittedly new to Node, but I have a pretty good handle on how each package works separately. However, accessing Express Session information from NowJS is giving me fits.

The problem seems to be that since I am forced to use XHR-Polling (using Heroku as a hosting platform), I am not able to easily access cookie information within the Socket.io authentication method. In the code below for authFunction, “data.headers.cookie” is undefined. In a roundabout way (within Now.js code), this is leaving “this.user.cookie” empty (but not undefined) in the nowjs.on(“connect”,…) callback. That in turn leaves me unable to grab the session information.

From reading around, it seems like the cookie might only be available if using websockets. Without the cookie, I have no idea which user is making calls to the server other than a random identifier which gets generated for them.

Can anyone suggest how to approach this? Do I need to manually send the cookie from the client side upon establishing connection to the Now.js server?

var app = express();

app.configure(function(){
  app.set('port', process.env.PORT || 3000);
  app.set('views', __dirname + '/views');
  app.engine('html', consolidate.swig);
  app.set('view engine', 'html');

  app.use(express.favicon());
  app.use(express.logger('dev'));
  app.use(express.bodyParser());
  app.use(express.methodOverride());
  app.use(express.cookieParser());
  app.use(express.session({ secret: "secrethere" , store: sessionStore}));

  app.use(express.static(path.join(__dirname, 'public')));
  app.use(app.router);
});

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

//Map the routes
var routes = require('./routes/routes')(db);
app.get[...] //routes defined here

var server = http.createServer(app, {cookieKey: 'connect.sid'});
server.listen(app.get('port'), function(){
  console.log("Express server listening on port " + app.get('port'));
});

var parseCookie = require('express/node_modules/connect').utils.parseCookie;
var authFunction = function (data, accept) {
// check if there's a cookie header
if (data.headers.cookie) {
    // if there is, parse the cookie
    data.cookie = parseCookie(data.headers.cookie);

    // 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['connect.sid'];
} 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);
};

var everyone = nowjs.initialize(server, {socketio: {transports: ['xhr-polling', 'jsonp-polling'], authorization: authFunction}});

nowjs.on('connect', function (socket) {
//TODO - This will need to be based on the URL that is being visited, or
//similar to establish that users are viewing the same thing
var sid = unescape(this.user.cookie["connect.sid"]); //# you DO have to unescape the session id!

sessionStore.get(sid, function(err, sess){
    console.log("That group who joined? his userId is " + sess.userId)
});

var newRoom = "group";//this.user.session;//.groupName;
var newGroup = nowjs.getGroup(newRoom);
this.now.serverRoom = newRoom;
newGroup.addUser(this.user.clientId);
});

edit: I would of course prefer simple solutions like the top answer here: Session support in Now.js, but that does not work for me for (likely) the same reason – my “cookie” is empty and does not contain the “connect.sid” key.

  • 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-14T19:41:29+00:00Added an answer on June 14, 2026 at 7:41 pm

    After a significant amount of trying different things, I realized that this actually has to do with the environment I was using (Cloud9 IDE).

    When debugging with Cloud9, they give you a nice url to walk through your site at “http://[projectname].[username].c9.io”. However, NowJS sends its requests to “http://project-[unique ID].rhcloud.com/socket.io/1/xhr-polling/…”. This was not prompting the cookie to be sent along with the request, since it was not the same host as the cookie is associated with (okay, terminology may be a little off here).

    To resolve this issue, I am using “http://project-[unique ID].rhcloud.com/…” as my base for debugging, and it seems to be working 100% properly. Heroku seems to not have this problem, as all requests (regular page requests and socketio) are going through the same host.

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

Sidebar

Related Questions

I have a date and a time that I'm attempting to combine in Python.
I am attempting to create a script that wraps a Groovy class that will
I'm attempting to craft a function that takes a time object and converts it
I'm attempting to combine 11 different data types into a single list that I
I am attempting to create a shortcut that will load the vcvarsall.bat (Visual Studio
Attempting to build a C# NPAPI plugin I have found a tutorial which describes
Attempting to make a NSObject called 'Person' that will hold the login details for
Attempting to build a C# project which has numerous references to assemblies in NuGet
I'm attempting to create an order number for customers to use. I will have
Attempting to create a filter select to find schedules with coursedates within a given

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.