user5962711
user5962711

Reputation: 23

How to capture the namespace origin from a client side socket.emit serverside socket.io

If you have a client in a specific namespace/room that performs a socket.emit and you do a "socket on" on the server how can you capture the namespace origin as a string ?

IE:

Client:

socket.emit('foo', $('#bar').val());
return false;

Server:

socket on('foo', function (foo) {
var usersubmitteddata = foo  
var namespaceorigin = ???
});

Upvotes: 2

Views: 1395

Answers (1)

bolav
bolav

Reputation: 6998

To list what rooms a socket belongs to just use socket.rooms:

console.log(socket.rooms);

socket.io does not expose which namespace the client connected on, but you can see the current namespace on the server with socket.nsp.name, and which exists on the server with socket.nsp.server.nsps.

The namespaces that a client should connect to must be created before you connect. If not you will get an Invalid namespace error message. But after that it's actually every message that contains the namespace, and not the socket. In the protocol itself you can have a client send messages on different namespaces.

Upvotes: 2

Related Questions