Skip to content

Dynamic namespace connect sometime not triggers namespace scope with Socket io, Kubernetes #4015

@FA-QuangLT

Description

@FA-QuangLT

I have faced a strange issue with Socket io dynamic namespace, Kubernetes. Client connected to socketio server but sometimes connection events never trigger the namespace instance.

I'm experiencing intermittent issue when using a regex expression for dynamic namespace.

But, In case:

  • Running a single socket.io node.
  • Or only one namespace
  • Or without namespace
    => it works normally !!!

The code is working fine most of the time. It only failed when the socket.io client failed to trigger the namespace connect event.

Note:

  • I have only one the code snippet for namespace instance in my app.
  • This issue occurs when running on multiple node deployed in Kubernetes with many pods.

Package

"socket.io": "^4.1.1",
"socket.io-client": "^4.1.1",

Server code:

const tenantWorkspaces: socketio.Namespace = chatServer.io.of(/^\/\w+$/);
tenantWorkspaces.on('connection', async (socket: socketio.Socket) => {
    const tenantWorkspace = socket.nsp;
    console.log('========children namespace======');

    socket.on('clientEmit', (data) => {
      console.log('clientEmit', data);
    });

    tenantWorkspace.to(socket.id).emit('serverEmit', {data: 'server emitted !!!'});

    workspaceId = isString(socket.handshake.query.workspace_id) ? socket.handshake.query.workspace_id : null;

    console.log(`workspace: ${workspaceId}, contact: ${contactId} connected!`); // debug
};

Client code

     const options = {
      query: {
        'workspace_id': window.LINEBASE_WORKSPACE_ID || '',
      },
      resource: "socket.io",
      transports: ['websocket'],
      upgrade: false,
    };
    socket = io(`${socketUrl}/${namespace}`, options);

    socket.on("connect", () => {
      console.log('socket connected: ' + socket.connected); // true
      console.log('socketID:', socket.id);
    });
    
    socket.on('serverEmit', (data) => {
      console.log(`================ Connect namespace ${namespace} successfully ==============`, data);
    })

I spent almost two weeks to find the solution but still have no result.
Is there any solution for this issue ? My application has many namespaces so I needs using dynamic namespace instead of hard code.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions