When I try io.emit(), the message gets emitted to everyone but when I try io.to(room).emit(), the message doesn't get emitted (In the 3rd last line of the code).
I manually checked the sockets in the room, and all are present in the room correctly. But the message never gets transmitted.
io.on('connection', (socket) => {
socket.on("create game", async ( gameID, host ) => {
socket.join(gameID);
// some logic
})
socket.on("join game", async ( gameID, user ) => {
socket.join(gameID);
// some logic
io.to(gameID).emit("new user", user);
})
});
Is your socket connected to a particular namespace? In my project I had on the client side the following:
const socket = io.connect("/play-online").
So in order to emit to the room I created for that, I had to do:
io.of("/play-online").to(room.roomName).emit("move", move);
I was able to fix it using socket.in(gameID).emit("new user", user); instead