I am building a chat where i want to store the session.id of a user for 10 days. So that if the user comes back after 8 days, his/her messages will appear again. For this to work i have to somehow store the session.id that is generated.
How would i accomplish this?
My current code that runs when the users connections is this:
// user_id is a unique ID per domain so that each chat has its own room
socket.on('adduser', function(user_id){
var sid = socket.id;
console.log('Socket.id ====>', sid);
socket.join('Room_' + user_id);
socket.broadcast.to('Room_' + user_id).emit('updatechat', 'SERVER','Hello');
});
Use express session store for storing and retrieving cookies/ sessionID information. Use socket.io with authorization to extract sessionID's on user connect as per sample code below:
var parseCookie = require('connect').utils.parseCookie;
sio.set('authorization', function (data, accept) {
if (data.headers.cookie) {
data.cookie = parseCookie(data.headers.cookie);
data.sessionID = data.cookie['express.sid'];
} else {
return accept('No cookie transmitted.', false);
}
accept(null, true);
});
The cookie will contain the sessionID of our Express session.
All the attributes, that are assigned to the data object are now accessible through the handshake attribute of the socket.io connection object.
sio.sockets.on('connection', function (socket) {
console.log('A socket with sessionID ' + socket.handshake.sessionID
+ ' connected!');
});
Hope this helps.