Skip to content

Problem with namespaces and regex #4615

@ksowa

Description

@ksowa

Describe the bug
There seems to be a problem with connection callback on the server if the following occur:

  1. Server uses dynamic namespaces (e.g. io.of(/^.*$/)...)
  2. Server sends a message to the namespace before any client connects to that namespace
  3. When at this point client connects to the server, the connection callback is not triggerred

To Reproduce

Socket.IO server version: 4.5.4

Server

import { Server } from "socket.io";

const io = new Server(3000, {
    cors: {
        origin: (origin, callback) => callback(null, origin),
    },
});

io.of(/^.*$/).on('connection', (socket) => {
    console.log('Connected!', socket.id);
});

io.of('testing/namespace').emit('message', 'testing message');

Socket.IO client version: 4.5.4

Client

import { io } from "socket.io-client";

const socket = io("ws://localhost:3000/testing/namespace", {});

socket.on("connect", () => {
  console.log(`connect ${socket.id}`);
});

Expected behavior

The connection callback should be triggered regardless if the first message has been emitted to the namespace before or after any clients connect to that namespace.

Additional context

If we change the server code to the below, it works without any problem.

(first message emitted after first client connects)

const {Server} = require('socket.io');

const io = new Server(3000, {
    cors: {
        origin: (origin, callback) => callback(null, origin),
    },
});

io.of(/^.*$/).on('connection', (socket) => {
    console.log('Connected!', socket.id);
    io.of('testing/namespace').emit('message', 'test');
});

OR (namespace is not dynamic):

const {Server} = require('socket.io');

const io = new Server(3000, {
    cors: {
        origin: (origin, callback) => callback(null, origin),
    },
});

io.of('testing/namespace').on('connection', (socket) => {
    console.log('Connected!', socket.id);
});

io.of('testing/namespace').emit('message', 'test');

OR (client connecting to another namespace):

import { io } from "socket.io-client";

const socket = io("ws://localhost:3000/some/other/namespace", {});

socket.on("connect", () => {
  console.log(`connect ${socket.id}`);
});

Metadata

Metadata

Assignees

No one assigned

    Labels

    questionFurther information is requested

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions