I need some help here. I need to set user username and do not change it. But the problem is everytime I join with new user it;s username is added to session and then all users are typing as the latest user who joined. How to solve this problem ? This is the code:
let users = [];
let username;
app.post('/join-server', async(req, res) => {
username = await req.body.username;
users.push(username);
req.session.username = username;
res.redirect('/chatroom');
});
socket.on('message-sent', (data) => {
io.emit('message-broadcast', ({
message: data.message,
username: username
}));
});