Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Struggling to write a session #20

Closed
alexcroox opened this issue Apr 15, 2013 · 14 comments
Closed

Struggling to write a session #20

alexcroox opened this issue Apr 15, 2013 · 14 comments
Labels

Comments

@alexcroox
Copy link

I am trying to write a session value but I am getting "TypeError: Cannot set property 'foo' of undefined"

var express = require('express'),
    app = express(),
    http = require('http'),
    connect = require('connect'),
    mysql = require('mysql'),
    queue = [],
    db = mysql.createConnection({
          host     : 'xxxxx',
          user     : 'xxxx,
          password : 'xxxx',
          database : 'xxxx'
    }),
    cookieParser = express.cookieParser('testingthis'),
    sessionStore = new connect.middleware.session.MemoryStore();

app.configure(function () {
    app.use(express.bodyParser());
    app.use(express.methodOverride());
    app.use(cookieParser);
    app.use(express.session({ store: sessionStore }));
});

var server = http.createServer(app),
    io = require('socket.io').listen(server),
    sessionSockets = require('session.socket.io'),
    sessionSockets = new sessionSockets(io, sessionStore, cookieParser);

server.listen(3000);
db.connect();

sessionSockets.on('connection', function (err, socket, session) {

    session.foo = 'bar'; // errors here: TypeError: Cannot set property 'foo' of undefined

    session.save();

    console.log(session.foo);
@tanema
Copy link

tanema commented Apr 16, 2013

one problem I see is

sessionSockets = require('session.socket.io'),
sessionSockets = new sessionSockets(io, sessionStore, cookieParser);

and have you checked if there is an error on connection?

Also I may be wrong but it is also good practice to do the server.listen() very last after your socket setup

@wcamarao
Copy link
Owner

@alexcroox any progress based on @tanema's suggestions?

@alexcroox
Copy link
Author

I don't understand what @tanema is pointing out, this is my current code:

(I moved server.listen to the bottom of the app)

var express = require('express'),
    app = express(),
    http = require('http'),
    cookieParser = express.cookieParser('testingthis'),
    sessionStore = new connect.middleware.session.MemoryStore();

app.configure(function () {
    app.use(cookieParser);
    app.use(express.session({ store: sessionStore }));
});

var server = http.createServer(app),
    io = require('socket.io').listen(server),
    sessionSockets = require('session.socket.io'),
    sessions = new sessionSockets(io, sessionStore, cookieParser);

sessions.on('connection', function (err, socket, session) {

    session.foo = 'bar';
    session.save();
    // ERROR HERE - TypeError: Cannot set property 'foo' of undefined

Basically session is undefined.

@wcamarao
Copy link
Owner

I can't catch any problem by looking at the code, would you please try running the example from the project source?

@alexcroox
Copy link
Author

debug - websocket writing 5:::{"name":"session","args":[null]}

(is the output of me trying the example)

@wcamarao
Copy link
Owner

wcamarao commented May 2, 2013

I'm not sure what do you mean by that log output. Have you made progress on trying the example?

@coronelrc
Copy link

having the same problem, there are times that session comes undefined inside the

sessionSockets.on('connection', function (err, socket, session) {
}

isn't session supposed to be defined all the time while inside that clause? I can't understand what the replies are really pointing out to, doesn't seem to solve any problem. Anyone there have ideas?

@coronelrc
Copy link

should there be a callback if session is ready? i find it strange for session to be undefined inside that block

@alexcroox
Copy link
Author

It's undefined because something isn't setup correctly, I'm yet to find out what.

@tanema
Copy link

tanema commented May 9, 2013

Maybe this will help, this is my working setup with redis, but you could change the session store to anything else.

var express = require('express'),
     routes = require('./routes'),
     http = require('http'),
     path = require('path'),
     sessionSecret = "secret",
     cookieParser = express.cookieParser(sessionSecret),
     RedisStore = require("connect-redis")(express),
     sessionStore = new RedisStore(),
     SessionSockets = require('session.socket.io'),
     app = express();

app.configure(function(){
  app.set('port', process.env.PORT || 3001);
  app.set('views', __dirname + '/views');
  app.set('view engine', 'hjs');
  app.use(express.favicon());
  app.use(express.logger('dev'));
  app.use(express.bodyParser());
  app.use(express.methodOverride());
  app.use(cookieParser);
  app.use(express.session({ store: sessionStore }));
  app.use(express.static(path.join(__dirname, 'public')));
  app.use(app.router);
});

var server = http.createServer(app),
    io = require('socket.io').listen(server);

var SessionSockets = require('session.socket.io'),
     sessionSockets = new SessionSockets(io, sessionStore, cookieParser);

sessionSockets.on('connection', function (err, socket, session) {

@coronelrc
Copy link

5th line throws an error since session is not defined (Cannot read property 'room' of undefined) you guys have ideas?

sessionSockets.on('connection', function (err, socket, session) {
console.log('>>>>>>>>>>>>>>>>>>>>>>>>>');
room = socket.room = 'Lobby';
//socket.leave(socket.room)
if(session.room!==undefined){
socket.room = session.room;
}
}

@wcamarao
Copy link
Owner

wcamarao commented Jun 8, 2013

I'm closing this for inactivity. The module seems to be working fine, as feedback from other people and my local tests. If it's still not working for you, I recommend starting with the example, and making changes in baby steps, until you have it customized the way you need, so you may move forward with your app.

@wcamarao wcamarao closed this as completed Jun 8, 2013
@nil1511
Copy link

nil1511 commented Oct 25, 2013

I had the same problem i.e. session is undefined for me the problem was that in on client javascript
let it be io.connect(); rather than io.connect('localhost:3000')

@ArthurGerbelot
Copy link

I have the same problem, and no solution..

It try to throw err on session.Sockets.on('connection') bracket and it's return :
"Error: could not look up session by key: connect.sid ...."

Someone have ideas ?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

6 participants