Thomas Shelby
Thomas Shelby

Reputation: 1370

socket.io doesn't store session

I have application which use node.js and socket.io.

I would like store some information in session. I made example. But my code doesn't work. When I run my script and refresh page I see:

NaN
2

Next, when I refresh page again I can see

NaN
2
NaN
2

So session is not stored. How can I fix my code ?

var Session = require('express-session'),
    SessionStore = require('session-file-store')(Session);
var session = Session({ secret: 'pass', resave: true, saveUninitialized: true });

var config = require("./config.json");
var express = require('express');
var app = express();
var socketio  = require('socket.io');

var server = app.listen(3000, function(){
    console.log('Start')
});
var io = require('socket.io').listen(server);

app.use(session); // session support
app.get('/', function (req, res) {
    req.session.ziom = 1;
    req.session.save();
    console.log('dec');
});

var http = require('http');


var ios = require('socket.io-express-session');
io.use(ios(session));

io.sockets.on('connection', function(socket){
    console.log(parseInt(socket.handshake.session.test));

    socket.handshake.session.test =2;
    socket.handshake.session.save();

    console.log(parseInt(socket.handshake.session.test));
});

Upvotes: 4

Views: 1072

Answers (2)

Mindsers
Mindsers

Reputation: 662

I dont't understand this error because I copy-past your code and everything* work fine !

*I can't see all your code/project, I don't know if you have an index.html file who start connexion event with the node server. I had this file with minimum code for test.

Change in my app.js is res.sendfile('./index.html');

See all my code below for comparison.

app.js

var Session = require('express-session'),
    SessionStore = require('session-file-store')(Session);
var session = Session({ secret: 'pass', resave: true, saveUninitialized: true });

var express = require('express');
var app = express();
var socketio  = require('socket.io');

var server = app.listen(3000, function(){
    console.log('Start')
});
var io = require('socket.io').listen(server);

app.use(session); // session support
app.get('/', function (req, res) {
    req.session.ziom = 1;
    req.session.save();
    console.log('dec');
    res.sendfile('./index.html');
});

var http = require('http');


var ios = require('socket.io-express-session');
io.use(ios(session));

io.sockets.on('connection', function(socket){
    console.log(parseInt(socket.handshake.session.test));

    socket.handshake.session.test =2;
    socket.handshake.session.save();

    console.log(parseInt(socket.handshake.session.test));
});

index.html

<!DOCTYPE html>
<html>
<head>
</head>
<body>
    <script src="https://cdn.socket.io/socket.io-1.3.5.js"></script>
    <script>
      var socket = io();
    </script>
</body>
</html>

Sincerely hope that helps.

Upvotes: 2

Benjamin
Benjamin

Reputation: 1231

I assume you're trying to open the socket.io connection using an HTML file similar to the bootstrap file in the tutorial (second html snippet after this link)?

With a small change, your code worked for me, where working means you get

NaN
2
2
2

on the console with two page refreshes.

In order to get this, I created the boostrap file relative to the index.js file (a copy of your code above) at views/socket.html, and ensured this was served by the express app by adding these lines to the handler for the '/' route:

require('fs').readFile('views/socket.html', function (err, data) {
    res.writeHead(200, {'Content-Type': 'text/html'});
    res.write(data, "utf8");
    res.end();
});

Then visited http://localhost:3000 twice.

It is required for this file to be served by the express app in order for the session cookie to be stored; so that the socket.io connection knows to use the same cookie to be linked to the same session at the next refresh.

Upvotes: 1

Related Questions