Saving variables in socket.io across multiple pages
I'm new to node.js and socket.io so some of these questions might seem really silly. With that said, I was trying to develop an application that can store the username between different pages. For example, I set my username to one page. Then I am redirected to another page, but my username is still a variable available on the socket connection.
Here's my basic server setup:
var app = require('express')();
var http = require('http').Server(app);
var io = require('socket.io')(http);
app.get('/set', function(req, res){
res.sendfile('set.html');
});
app.get('/get', function(req, res){
res.sendfile('get.html');
});
io.on('connection', function(socket){
});
http.listen(3000, function(){
console.log('listening on *:3000');
});
My questions:
1.) Can I find out which page the socket is currently on? If so, how?
2.) How can I interact with this information in io.on (...)?
I've tried using session middleware for socket.io and express, but I can't seem to get it to work. I'm sure using sessions is the best way to do this, but I don't know how to implement them. Any suggestions for setting up sessions available through the different pages would be nice too.
Thanks you!
For this you need to use express sessions and handshake:
sock.on('your_socket_name', function (data) {
sock.handhsake.session.name = data.name;
sock.handshake.session.save();
});
check out this link, it will definitely help you:
https://www.npmjs.com/package/socket.io-handshake