socket.ionamespaceschatroom

How do i allow socket.io users to join a room with multiple namespaces?


I have a socket.js file on the server side, and I want my user communication to be separated into different namespaces, namely

this.mainRoom = io.of('/main_room')
this.privateRoom = io.of('/private_room')

When users join the room specific to '/main_room' namespace, it works fine. But I want the user to be able to join the '/private_room' namespace. Here's the code

const socks = function (io) {
let self = this
self.privateRoom = null

// where all the messaging and the events that are not part of the
// user-to-user communication
this.mainRoom = io.of('/main_room')
this.mainRoom.on('connection', (socket) => {
    socket.on('set_name', (data) => {
        const nickname = data.name
        socket.nickname = nickname
        socket.emit('name_set', data)
        socket.send(JSON.stringify({
                type: 'serverMessage',
                nickname: socket.nickname,
                message: "Welcome! "
            })
        )
        socket.broadcast.emit('user_entered', {
            nickname: socket.nickname,
        })
    })
    socket.on('join_room', (room) => {
        //  concept roomname !== namespace
        //  concept Rooms are subchannels of the namespaces.

        //create another socket,  since socketio doesnt allow a single socket
        //to be connected to multiple namespaces
        socket.join(room.name, () => {
        ---------->> how do i allow user to join the room in the private_room namespace? <<---------
        ---------->> how should i create socket2 to join the same room.name? <<----------
            // let socket2 = self.privateRoom.sockets[socket.id]
            // socket2.join(room.name)
        })
    })
})

// split the socket code into 2 separate parts for namespaces
// this is so that the messaging and events that are related to users
// are in a namespace
// while messaging and events that are not part of user-to-user
// communication is separated 
this.privateRoom = io.of('/private_room')
this.privateRoom.on('connection', (socket) => {
    socket.on('message', (message) => {
        message = JSON.parse(message)
        if (message.type === "userMessage") {
            socket.in(socket.room).broadcast.send(JSON.stringify(message))
            message.type = "myMessage"
            socket.send(JSON.stringify(message))
        }
    })
})

}


Solution

  • ah! silly error

    Just had to rename this.privateRoom's socket as socket2 and make it a global variable

    self.socket2 = null
    
    
    this.privateRoom = io.of('/private_room')
    this.privateRoom.on('connection', (socket2) => {
        self.socket2 = socket2
        self.socket2.on('message', (message) => {
            message = JSON.parse(message)
            if (message.type === "userMessage") {
                self.socket2.in(self.socket2.room).send(JSON.stringify(message))
                message.type = "myMessage"
                self.socket2.send(JSON.stringify(message))
            }
        })
    })
    

    And give it back to mainRoom

    let socket2 = self.socket2.join(room.name)
    socket2.room = room.name
    socket.in(room.name).emit('user_entered', {'name': socket.nickname})