io.on("connection", (socket) => {
console.log(`User Connected: ${socket.id}`);
//joining Room
socket.join(selectedOption);
socket.broadcast.emit("userConn", `${username} joined ${selectedOption}`);
socket.on('emitMessage', (data) => {
if(data.room == selectedOption){
socket.broadcast.to(selectedOption).emit('message', {username: data.username , message: data.message});
}
});
});
For some reason after the first user has connected, when I login from the second user, they seem to connect twice. Hence:
console.log(`User Connected: ${socket.id}`);
runs twice, making the server side console looking like:
User Connected: pPNqnsAIJAUgDrpkAAAB
User Connected: yZPm7IuY673r1hkUAAAD
User Connected: yZPm7IuY673r1hkUAAAD
This is also resulting in the first user receiving the second user's messages twice because they seem to be connected twice.
I was making a simple socket.io chat server, but the second user's messages were being received twice by the first user. Upon further observation, I noticed that the second user connects to the server twice.