Dynamic namespace connect sometime not triggers namespace scope with Socket io, Kubernetes
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.
I wanted to update from v2, but encountered the same issue. Until it's resolved, I'd suggest sticking to v2.3.0...
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 ?
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.
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.
Unfortunately, as I said in https://github.com/socketio/socket.io/issues/3960, we would need a way to reproduce the issue...
This should be fixed by https://github.com/socketio/socket.io/commit/0d0a7a22b5ff95f864216c529114b7dd41738d1e, included in version 4.6.1.
Please reopen if needed.