Skip to content
Advertisement

socket.io emitting to all clients works, but emitting to room doesn’t work

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);
    })
});

Advertisement

Answer

I was able to fix it using socket.in(gameID).emit("new user", user); instead

Advertisement