Within namespaces, you can define rooms or channels that a socket can join and leave.
By default, a room is created with a random un-guessable ID for the connected socket:
io.on('connection', (socket) => {
console.log(socket.id) // Outputs socket ID
})
On connection, when emitting an event, for example:
io.on('connection', (socket) => {
socket.emit('say', 'hello')
})
What happens underneath is similar to this:
io.on('connection', (socket) => {
socket.join(socket.id, (err) => {
if (err) {
return socket.emit('error', err)
}
io.to(socket.id).emit('say', 'hello')
})
})
The join method was used to include the socket inside a room. In this case, the socket ID is the joint room, and the only...