So now I'm storing info when user submits login form.
io.on('connection', (socket) => {
let userInfo;
socket.on('login', (data) => {
userInfo = data;
console.log(userInfo); // {username: 'testusername'}
});
socket.on('lobby-left', (userInfo) => {
console.log(userInfo); // { }
});
The problem is that in lobby-left I don't get any info from the variable anymore. I get empty brackets { }. How can I reuse the variable so I can use the login info in all functions ?
socket.on('lobby-left', () => {and let your closure work.undefinedloginin fired beforelobby-left, then you shouldn't be getting undefined. This is basic Javascript closures, and is how I use them with socket.io without issues. There is maybe something else were missing here.