How do I send a message to a specific room with Socket.io?

Asked

Viewed 827 times

1

How do I send a message to a specific room? guy:

server.js

io.in('roomA').on('msg', function(msg) {
...
});

client js.

io.in('roomA').emit('msg, 'Hi');

1 answer

2

According to the official documentation, to send a message to a specific room you can use the example below:

//enviando para todos na sala (room) 'game', com exceção do remetente
socket.to('game').emit('nice game', "let's play a game");

// enviando para todos na sala (room) 'game', incluindo o remetente
io.in('game').emit('big-announcement', 'the game will start soon');

Browser other questions tagged

You are not signed in. Login or sign up in order to post.