Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

"could not look up session by key: connect.sid" - session.socket.io

I am pretty new to the NodeJS development. I am using the session.socket.io plugin in my express app, but I am getting this error when I debug the app "could not look up session by key: connect.sid", inside sessionSockets.on('connection', ...

Here is the entire app.js:

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

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

app.configure(function() {
    app.set('port', process.env.PORT || 3000);
    app.use(express.logger('dev'));
    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.configure('development', function() {
    app.use(express.errorHandler());
});

app.get('/', function(req, res) {
    res.end("Welcome to the API server");
});

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

    console.log('-----------------------');
    console.log(err.error);
    console.log('-----------------------');

    socket.emit('welcome', {
        msg: 'You\'re now listening to the api.'

    });

});

server.listen(app.get('port'), function() {
    console.log("Express server listening on port " + app.get('port'));
});

Client side:

<!DOCTYPE html>

<head>   
    <meta charset="utf-8">    
    <title>Express+SocketIO testing</title> 
</head>

<body>

    <div id="responses">   
        <h2>API responses:</h2>   
        <ul></ul>
    </div>

    <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>    
    <script src="http://localhost:3000/socket.io/socket.io.js"></script>

    <script>

        $(document).ready(function() {

            var socket = io.connect('//localhost:3000');

            // Welcome message    
            socket.on('welcome', function(data) {    
                var li = '<li>Server said: ' + data.msg + '</li>';    
                $('#responses ul').append(li);    
            });
        });

    </script> 
</body>
</html>

package.json

{
  "name": "application-name",
  "version": "0.0.1",
  "private": true,
  "scripts": {
    "start": "node app"
  },
  "dependencies": {
    "express": "3.0.3",
    "socket.io":"*",
    "session.socket.io":"*"
  }
}

Any help or hints are welcome. Thank you.

like image 765
jithujose Avatar asked Oct 21 '22 22:10

jithujose


2 Answers

This happened to me when I used a Java socket.io client, since Java did not have access to the node.js session.

So, I decided it's best not to use the session.socket.io plugin at all. A more general solution, which can work with all kinds of clients, is: send the session data (or a session ID) to the client, possibly by inserting a Javascript snippet to the client code; then have the client send the session ID back to the server.

like image 89
Erel Segal-Halevi Avatar answered Oct 27 '22 09:10

Erel Segal-Halevi


Many people were having the same error and there was a very simple solution as per this comment on Github. It was simply a matter of browsing to 127.0.0.1 versus localhost.

I hope that this solves your problem too.

like image 26
Tim S. Avatar answered Oct 27 '22 07:10

Tim S.