I was wondering what the purpose of rooms is. See: https://socket.io/docs/rooms-and-namespaces/
You can basically mimic join
and leave
with just socket.on
and socket.off
Take for example:
(no room)
client:
socket.on('chat', (data) => {
console.log("chat received", data)
$(this.$refs.chatBox).append(`
<div>
${data}
</div>
`)
})
server:
socket.on('chat', function (id, msg) {
console.log("id:", id, 'message:', msg)
socket.broadcast.emit('chat', msg);
// io.emit('chat', msg)
})
This will now listen to the "chat"
and I can turn it off using socket.off("chat")
Now I do something similar but with a room:
client:
// to join
socket.emit('waiting room', socket.id)
// to message
socket.emit('waiting room chat', { msg: this.$refs.waiting_input.value })
server:
socket.on('waiting room', function (id) {
console.log("socket has joined the waiting room", id);
socket.join("waiting room")
})
socket.on("waiting room chat", function(payload) {
console.log("waiting room chat:", payload.msg)
// io.to("waiting room").emit('waiting room', payload.msg)
socket.broadcast.to("waiting room").emit('waiting room', payload.msg)
})
Now if I send a message to waiting room chat
but a socket hasn't joined the room, it won't receive it. But what advantage does the second method have over the first?