Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Socket.io get the namespace a socket belongs to

Is there a way to get a sockets namespace? I've tried various things but cannot get it to work. Given a socket object I would like to be able to know what namespace it belongs to. Thank you very much.

for example

nsp = io.of('/' + venue_code).on('connection', function(socket) {
    socket.namespace = nsp
    if (typeof servers[nsp.name] == 'undefined') {
        servers[nsp.name] = socket.id
        winston.debug("Server " + socket.id + " connected to " + nsp.name)
        socket.room = "servers"
        socket.join(socket.room)
    } else {
        socket.room = "clients"
        socket.join(socket.room)
        winston.debug("Client " + socket.id + " connected to " + nsp.name)
    }
like image 473
patrick_corrigan Avatar asked Jul 24 '15 02:07

patrick_corrigan


People also ask

What is Socket.IO path?

path ​ Default value: /socket.io/ It is the name of the path that is captured on the server side.

Does Socket.IO use WSS?

Note: You can use either https or wss (respectively, http or ws ).

Is Socket.IO using WebSocket?

Socket.IO is NOT a WebSocket implementation. Although Socket.IO indeed uses WebSocket for transport when possible, it adds additional metadata to each packet.

How many connections Socket.IO can handle?

As we saw in the performance section of this article, a Socket.IO server can sustain 10,000 concurrent connections.


1 Answers

It is just socket.nsp, which gives the entire Namespace object. Use socket.nsp.name to get the actual name.

nsp = io.of('/' + venue_code).on('connection', function(socket) {
    winston.debug(socket.nsp)
}

Note that if you attach this same handler to the root namespace, you'll get the root namespace -- those middlewares are run before the correct namespace is attached.

like image 105
patrick_corrigan Avatar answered Sep 28 '22 02:09

patrick_corrigan