I am using node.js and socket.io for some two way data handling. What I want is persistent connections when the client navigates through the site. I was using standard
var socket;
socket = io.connect("http://localhost:port");
socket.on('connect', function(data){
console.log("called connect");
console.log(user_id);
socket.emit('init', user_id);
});
This way I assign the user_id to that socket and can use that later to send data selectively to certain users.
I was first inserting this in every web page.
I notice that socket connect and disconnect is called when we move to new page and even on reloads. Is there a way to avoid this and do the init
call only once?
I tried looking at express.js
but couldn't figure out any obvious solutions.