Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
FA-QuangLT opened this issue Jul 13, 2021 · 6 comments
Labels
unable to reproduce We were unable to reproduce the issue
Milestone

Comments

@FA-QuangLT
Copy link

FA-QuangLT commented Jul 13, 2021

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.

@FA-QuangLT FA-QuangLT added the bug Something isn't working label Jul 13, 2021
@FA-QuangLT FA-QuangLT changed the title Dynamic namespace connect sometime not trigger with Socket io, Kubernetes Dynamic namespace connect sometime not triggers namespace scope with Socket io, Kubernetes Jul 13, 2021
@RosenTomov
Copy link

I wanted to update from v2, but encountered the same issue. Until it's resolved, I'd suggest sticking to v2.3.0...

@FA-QuangLT
Copy link
Author

I wanted to update from v2, but encountered the same issue. Until it's resolved, I'd suggest sticking to v2.3.0...

Thank you for your suggestion. Unfortunately, v2 is far from my current version :( . Do you think the cause of this issue is socket.io core ?

@RosenTomov
Copy link

Didn't have much time, so we decided to leave it with v2 and move on to other projects.

Last thing I remember was messing around with the dynamic namespaces unit tests
https://github.com/socketio/socket.io/blob/master/test/socket.io.ts#L921
and trying to create a demo that reproduces the problem outside our project, but to no avail.

@mewhit
Copy link

mewhit commented Oct 12, 2021

Sorry for my English,

We got the same problem, but we are not supposed to create dynamic workspace. You should have static name space with dynamic room.
https://socket.io/docs/v4/rooms/

So we change dynamic workspace for room, and we fixed ours problem.

Hope is good for you too.

@darrachequesne
Copy link
Member

Unfortunately, as I said in #3960, we would need a way to reproduce the issue...

@darrachequesne darrachequesne added unable to reproduce We were unable to reproduce the issue and removed bug Something isn't working labels Oct 18, 2021
darrachequesne added a commit that referenced this issue Feb 20, 2023
Namespaces that match the regex of a parent namespace will now be added
as a child of this namespace:

```js
const parentNamespace = io.of(/^\/dynamic-\d+$/);
const childNamespace = io.of("/dynamic-101");
```

Related:

- #4615
- #4164
- #4015
- #3960
haneenmahd pushed a commit to haneenmahd/socket.io that referenced this issue Apr 15, 2023
Namespaces that match the regex of a parent namespace will now be added
as a child of this namespace:

```js
const parentNamespace = io.of(/^\/dynamic-\d+$/);
const childNamespace = io.of("/dynamic-101");
```

Related:

- socketio#4615
- socketio#4164
- socketio#4015
- socketio#3960
dzad pushed a commit to dzad/socket.io that referenced this issue May 29, 2023
Namespaces that match the regex of a parent namespace will now be added
as a child of this namespace:

```js
const parentNamespace = io.of(/^\/dynamic-\d+$/);
const childNamespace = io.of("/dynamic-101");
```

Related:

- socketio#4615
- socketio#4164
- socketio#4015
- socketio#3960
@darrachequesne
Copy link
Member

This should be fixed by 0d0a7a2, included in version 4.6.1.

Please reopen if needed.

@darrachequesne darrachequesne added this to the 4.6.1 milestone Jun 19, 2023
This issue was closed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
unable to reproduce We were unable to reproduce the issue
Projects
None yet
Development

No branches or pull requests

4 participants