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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With