javascriptnode.jsexpresswebsocketsocket.io

Using UUID In Dynamic Namespace In Socket IO


Am developing a multi-tenant chat app and want each tenant to connect to their own namespace in socket io. Am using the id of the tenant which is UUID (ex: 5d056752-6643-4300-926f-5bcd5ed65722) as the namespace but am not able to connect to the server.

// Server

const namespace = this.io.of(/^[a-zA-Z0-9._-]+$/);
  namespace.on('connection', (socket: SocketWithUser) => {
}

The following regex don't work as well:

- this.io.of(/^\/\w+$/);
- this.io.of(/^[\w.\-]+$/);
- this.io.of(/^[0-9a-f]{8}-[0-9a-f]{4}-[0-5][0-9a-f]{3}-[089ab][0-9a-f]{3}-[0-9a-f]{12}$/);

// Client

const socket = io('localhost:5002/5d056752-6643-4300-926f-5bcd5ed65722', {
            reconnection: true,
            autoConnect: false,});
socket.connect();

The problem lies in the regex because it works if i use non-uuid string (ex: verizon) as namespace from the client.

What am i doing wrong?

Any pointers will help.


Solution

  • /^\/\w+$/ failed because of the hyphens in the UUID, /^[\w.\-]+$/ lacks of forward slash, same goes to the third one. You just need to add forward slash to the second/third regex:

    /^\/[0-9a-f]{8}-[0-9a-f]{4}-[0-5][0-9a-f]{3}-[089ab][0-9a-f]{3}-[0-9a-f]{12}$/