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

could not look up session by key: connect.sid #9

Closed
githubpendosoft opened this issue Dec 24, 2012 · 27 comments
Closed

could not look up session by key: connect.sid #9

githubpendosoft opened this issue Dec 24, 2012 · 27 comments

Comments

@githubpendosoft
Copy link

I can't make it working.

The client connected but the session always is undefined with error:

could not look up session by key: connect.sid

Could you help please?

app.js

var http = require('http')
, connect = require('connect')
, express = require('express');

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

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

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

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

server.listen(8080);

sessionSockets.on('connection', function (err, socket, session) {
console.log('connection ERR ', err);
console.log('connection session ', session);
});


client

<html>
<head>
<script src='http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js' ></script>
<script src='./node_modules/socket.io/node_modules/socket.io-client/dist/socket.io.js'></script>
<script>
var iosocket = null;
$(function(){
iosocket = io.connect('http://localhost:8080');
iosocket.on('error', function (reason){
console.error('Unable to connect Socket.IO', reason);
});
iosocket.on('connect', function () {
$('#status').html('Connected');
});
iosocket.on('disconnect', function() {
$('#status').html('Disconnected');
iosocket.socket.connect();
});
});

</script>

</head>

<body>

<div id='status'></div>

</body>
</html>

@aliw77
Copy link

aliw77 commented Dec 30, 2012

Same issue - please help.

@eephillip
Copy link

Maybe because your not passing your secret key to the express session, they need to match I think.

edit
socket.io should be connecting because this module doesn't restrict the authorization, but session will be undefined unless the cookie was parsed correctly with your site key

...

var SITE_SECRET = 'key'

...

var cookieParser = express.cookieParser(SITE_SECRET)

...


//example express session
app.use(express.session({
    secret: SITE_SECRET,
    key: 'connect.sid',
    httpOnly: true,
    store: sessionStore
  }));

@wcamarao
Copy link
Owner

wcamarao commented Jan 2, 2013

That's not needed @eephillip, can you guys @githubpendosoft and @aliw77 copy and paste your output of npm ls?

@githubpendosoft
Copy link
Author

@wcamarao
Copy link
Owner

wcamarao commented Jan 9, 2013

That looks ok, however I reviewed your code and am not sure whether this is ok:

<script src='./node_modules/socket.io/node_modules/socket.io-client/dist/socket.io.js'></script>

Can you follow the instructions from socket.io docs on how to load the client?

@githubpendosoft
Copy link
Author

I'm afraid I can't.

After I've installed the socket.io with "npm install socket.io" how it is described on their site I have the socket.io.js in these fields only

./node_modules/socket.io/lib/socket.io.js
./node_modules/socket.io/node_modules/socket.io-client/dist/socket.io.js

And if I use the code from their example
<script src="/socket.io/socket.io.js"></script>
the script can't be loaded since it is absent on this path.

of course I can add some rewrite rules to htaccess but I think it will have the same affect as if I change the path to script.

.........
I've found the way how I can use almost their instructions how to connect the client
I can do it in such way
<script src='http://localhost:8080/socket.io/socket.io.js'>&lt;/script>
in this way the script socket.io.js is loaded well but I have the same session error:
connection ERR { error: 'could not look up session by key: connect.sid' }

May be it will work if I set up it to listen the port 80 but I can't do it since I have to have the Apache run on this port.

PS. sorry for my English.

@wcamarao
Copy link
Owner

Yes, this is preferable

<script src='http://localhost:8080/socket.io/socket.io.js'></script>

This should also be fine without hostname and port, assuming you're browsing within the same context. I just merged a pull request that will give you an error object with better tracing. It's published on npm as 0.1.4. Can you update it, try again and paste the stack trace?

@githubpendosoft
Copy link
Author

I've added the "handshake" data to log:

sessionSockets.on('connection', function (err, socket, session) {
console.log(socket.handshake);
console.log('connection ERR ', err);
console.log('connection session ', session);
});

So the output is:

info - socket.io started
debug - served static content /socket.io.js
debug - client authorized
info - handshake authorized rJxXIvLjXH2hHwwumLxT
debug - setting request GET /socket.io/1/websocket/rJxXIvLjXH2hHwwumLxT
debug - set heartbeat interval for client rJxXIvLjXH2hHwwumLxT
debug - client authorized for
debug - websocket writing 1::
{ headers:
{ host: 'localhost:8080',
'user-agent': 'Mozilla/5.0 (X11; Linux x86_64; rv:18.0) Gecko/20100101 Firefox/18.0',
accept: 'text/html,application/xhtml+xml,application/xml;q=0.9,/;q=0.8',
'accept-language': 'ru-RU,ru;q=0.8,en-US;q=0.5,en;q=0.3',
'accept-encoding': 'gzip, deflate',
referer: 'http://localhost/comet/',
origin: 'http://localhost',
connection: 'keep-alive' },
address: { address: '127.0.0.1', port: 42470 },
time: 'Sat Jan 26 2013 15:23:15 GMT+0400 (MSK)',
query: { t: '1359199395077' },
url: '/socket.io/1/?t=1359199395077',
xdomain: true,
secure: undefined,
secret: 'key',
cookies: {},
signedCookies: {} }
connection ERR [Error: could not look up session by key: connect.sid]
connection session undefined

@quiricada
Copy link

same problem here.

so i tried other packages, the same problem.

like githubpendosoft, the cookies were empty/null. then i came across this socketio/socket.io#776, yipingshui's comment on 'use io.connect("http://ip:port")', bingo!

my original: io.connect("http://192.168.10.1:2222")
changed it to: io.connect("http://localhost:2222")

newbie here, my guess:

  1. the connect.sid cookie was under "localhost"
  2. my hosts file didn't have a mapping for 192.168.10.1 localhost

@wcamarao
Copy link
Owner

Thanks quiricada. Can you githubpendosoft follow up on that and let us know if you make any progress? It doesn't look like you're doing something wrong with session.socket.io, it seems to be something with your socket.io config.

@githubpendosoft
Copy link
Author

I've used io.connect('http://localhost:8008'); as you can see in the code I've posted above.
I've opened the same URL in the browser 'http://localhost/comet/'.
I've tried to move the index.php to www root and open in the browser 'http://localhost/index.php'.

I've got the same error:

info - socket.io started
debug - served static content /socket.io.js
debug - client authorized
info - handshake authorized G0eABh_2A2512Pmzleqr
debug - setting request GET /socket.io/1/websocket/G0eABh_2A2512Pmzleqr
debug - set heartbeat interval for client G0eABh_2A2512Pmzleqr
debug - client authorized for
debug - websocket writing 1::
{ headers:
{ host: 'localhost:8080',
'user-agent': 'Mozilla/5.0 (X11; Linux x86_64; rv:18.0) Gecko/20100101 Firefox/18.0',
accept: 'text/html,application/xhtml+xml,application/xml;q=0.9,/;q=0.8',
'accept-language': 'ru-RU,ru;q=0.8,en-US;q=0.5,en;q=0.3',
'accept-encoding': 'gzip, deflate',
referer: 'http://localhost/index.php',
origin: 'http://localhost',
connection: 'keep-alive' },
address: { address: '127.0.0.1', port: 46136 },
time: 'Mon Jan 28 2013 23:15:45 GMT+0400 (MSK)',
query: { t: '1359400545556' },
url: '/socket.io/1/?t=1359400545556',
xdomain: true,
secure: undefined,
secret: 'key',
cookies: {},
signedCookies: {} }
connection ERR [Error: could not look up session by key: connect.sid]
connection session undefined

The code
server: http://myxomop.e-vds.ru/node/app2.js
client: http://myxomop.e-vds.ru/node/index.php.txt

I will try to test it on real domain and let you know in 1-2 days.

@wcamarao
Copy link
Owner

Wait, you're using socket.io with php on apache? This library won't help you. It integrates socket.io with either connect or express which are node modules.

@githubpendosoft
Copy link
Author

I'm use socket.io with nodejs server.
The Apache was used to handle the client code only.

It doesn't working without Apache as well. Please check my code. Is it correct?

server: http://myxomop.e-vds.ru/node/app2.js
client: http://myxomop.e-vds.ru/node/index.html

Could you please provide me with en example that is working?

BR.

@wcamarao
Copy link
Owner

wcamarao commented Feb 1, 2013

Ok, I wrote one quickly, doing the minimum to demo the features: read and write session values within sockets. It's under /example. You will need to do npm install & node server.js, then go to localhost:3000. Hope that helps.

@bjwyse
Copy link

bjwyse commented Feb 3, 2013

Absolutely great module. Thanks for writing it. I am up and running now.

I had some issues with session being undefined and it was because on client side I connected using 'http://127.0.0.1:3000' but had browsed to 'http://localhost:3000'.

Error was '{}'. It would be cool if you could detect this issue and report it in err.

@wcamarao
Copy link
Owner

wcamarao commented Feb 4, 2013

Thanks for your feedback bjwyse. That's a common mistake when getting started with socket.io, and you can avoid that by simply calling io.connect() without an address.

@MaxMarkson
Copy link

Hi, I'm still having this problem.
The client's connection is made in this way:

<script type="text/javascript">socket = io.connect('http://'+location.host);</script>

so it can't be the same problem of bjwyse.
Any suggestion?

@wcamarao
Copy link
Owner

@MaxMarkson can you try the example and see if that works out there?

https://github.com/functioncallback/session.socket.io/tree/master/example

@MaxMarkson
Copy link

When I try the example I have this error:
"TypeError: Property 'engine' of object # is not a function
at View.render (C:\Users\Massimo\workspace\Example\node_modules\express\lib\view.js:75:8)
at Function.app.render (C:\Users\Massimo\workspace\Example\node_modules\express\lib\application.js:501:10)
at ServerResponse.res.render (C:\Users\Massimo\workspace\Example\node_modules\express\lib\response.js:755:7)
at sessionSockets.on.socket.on.session.foo (C:\Users\Massimo\workspace\Example\example.js:29:7)
at callbacks (C:\Users\Massimo\workspace\Example\node_modules\express\lib\router\index.js:161:37)
at param (C:\Users\Massimo\workspace\Example\node_modules\express\lib\router\index.js:135:11)
at pass (C:\Users\Massimo\workspace\Example\node_modules\express\lib\router\index.js:142:5)
at Router._dispatch (C:\Users\Massimo\workspace\Example\node_modules\express\lib\router\index.js:170:5)
at Object.router (C:\Users\Massimo\workspace\Example\node_modules\express\lib\router\index.js:33:10)
at next (C:\Users\Massimo\workspace\Example\node_modules\connect\lib\proto.js:190:15)"

But I don't have to use the express module, I need an integration between connect, connect-route, socket.io and passport.
Here my little example which has the "[Error: could not look up session by key: connect.sid]" error: http://pastebin.com/mNDZyjpa

@MaxMarkson
Copy link

@functioncallback have you got any news about the problem?

@wcamarao
Copy link
Owner

@MaxMarkson I don't see any problem really. I have a more complete working example here: https://github.com/functioncallback/upstream

Take a look and see if that helps maybe? At least passport is being used, with express, socket.io and other modules. Also, it's deployed here: http://upstream-beta.herokuapp.com

@alexand7u
Copy link

This error "could not look up session by key: connect.sid" appeared only in chrome(but not every time to fix you have to restart the browser ), firefox works ok.
the only thing i did was to invert server.listen with sessionSockets.on('connection',function(){ ...});
P.S: I tested session.socket.io example from git and still the same issue, is some timeout problem, the session remain saved or something like this

@MaxMarkson
Copy link

Well, I have the problem even with firefox and the server.listen was already after the on connection.
The last solution must be something wrong somewhere else in my code, i don't know what else can be...apparently no one has my problem.

@hugogz
Copy link

hugogz commented Mar 24, 2014

Hello!

first of all thanks for this great code, but i've been with a problem for some hours and i guess maybe you can help me with it..

I'm a newie with nodejs and express, this is my server code (i got it from the sample):

var http = require('http')
, path = require('path')
, connect = require('connect')
, express = require('express')
, app = express();

var cookieParser = express.cookieParser('aaaa')
, sessionStore = new connect.middleware.session.MemoryStore();

app.configure(function () {
console.log("hola configuracion "+sessionStore);
app.set('views', path.resolve('views'));
app.set('view engine', 'jade');

app.use(express.bodyParser());
app.use(express.methodOverride());
app.use(cookieParser);
app.use(express.session({ store: sessionStore }));
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);

app.get('/', function(req, res) {
console.log("hola get "+session);
req.session.foo = req.session.foo || 'bar';
console.log("hola get "+session);
res.render('index');
});

sessionSockets.on('connection', function (err, socket, session) {
console.log("hola emit "+err);
console.log("hola emit "+session);
socket.emit('session', session);

socket.on('foo', function(value) {
// foo = value;
console.log("hola log "+session);
console.log("hola log0 "+value);
session.foo = value;
console.log("hola log2"+session);
session.save();
console.log("hola log3"+session);
socket.emit('session', session);
});
});

server.listen(3000);

and this is the client code:

<script type='text/javascript' src='socket.io.js'> </script> <style> body { padding: 30px 50px; }
  #t {
    display: block;
    margin-bottom: 15px;
    outline: none;
    width: 700px;
    height: 100px;
  }

  #foo {
    width: 200px;
  }
</style>
</head>

SessionSockets usage example

<textarea id="t"></textarea>
<textarea id="foo"></textarea>

<script type='text/javascript'>
  var socket = io.connect("http://localhost:3000");

  socket.on('session', function (session) {
    document.getElementById('t').value = JSON.stringify(session);
  });

  document.getElementById('foo').addEventListener('keyup', function () {
    socket.emit('foo', this.value);
  });
</script>
</body>

The problem is that the session is always null when it reachs the server. The error that comes with it is "Error: could not look up session by key: connect.sid"

I think i had it working one time but after i can't configure it to do it. I'm getting crazy with this error and i can't find a solution. Any help will be much apreciated,

Thanks in advance!

Hugo.

@S0c5
Copy link

S0c5 commented Mar 29, 2014

Hi :) i have the same problem :( :D

@aleks091
Copy link

Hi, I had the same problem as bjwyse, and his solution worked for me

@totty90
Copy link

totty90 commented Nov 21, 2014

Same problem with express4 and all new deps.

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

No branches or pull requests