Replies: 3 comments 3 replies
-
Hi!
You must not leave the default room ( socket.rooms.forEach((room) => {
if (room !== socket.id) {
socket.leave(room);
}
}); Depending on your use case, you may also use the server.in(socketId).socketsLeave("some-room");
That should not happen. Could you please provide the full code reproducing the issue? |
Beta Was this translation helpful? Give feedback.
-
one await this.server.of('/admin').fetchSockets();
console.log('server of:', this.server.of); // of is undefined error message: TypeError: this.server.of is not a function
at SocketServer.findSocketById (/Users/mumu/Desktop/nest-socket/src/socket.server.ts:57:23)
at AppController.getSocket (/Users/mumu/Desktop/nest-socket/src/app.controller.ts:19:44)
at /Users/mumu/Desktop/nest-socket/node_modules/.pnpm/@nestjs+core@10.3.3_@nestjs+common@10.3.3_@nestjs+platform-express@10.3.3_@nestjs+websockets@_h3leh7b3mhe2cmhloksxb646bq/node_modules/@nestjs/core/router/router-execution-context.js:38:29
at processTicksAndRejections (node:internal/process/task_queues:95:5)
at /Users/mumu/Desktop/nest-socket/node_modules/.pnpm/@nestjs+core@10.3.3_@nestjs+common@10.3.3_@nestjs+platform-express@10.3.3_@nestjs+websockets@_h3leh7b3mhe2cmhloksxb646bq/node_modules/@nestjs/core/router/router-execution-context.js:46:28
at /Users/mumu/Desktop/nest-socket/node_modules/.pnpm/@nestjs+core@10.3.3_@nestjs+common@10.3.3_@nestjs+platform-express@10.3.3_@nestjs+websockets@_h3leh7b3mhe2cmhloksxb646bq/node_modules/@nestjs/core/router/router-proxy.js:9:17 two await this.server.of('/admin').in('room').fetchSockets(); error message: [Nest] 34525 - 03/23/2024, 4:47:48 PM ERROR [ExceptionsHandler] this.server.of is not a function
TypeError: this.server.of is not a function
at SocketServer.findSocketById (/Users/mumu/Desktop/nest-socket/src/socket.server.ts:57:23)
at AppController.getSocket (/Users/mumu/Desktop/nest-socket/src/app.controller.ts:19:44)
at /Users/mumu/Desktop/nest-socket/node_modules/.pnpm/@nestjs+core@10.3.3_@nestjs+common@10.3.3_@nestjs+platform-express@10.3.3_@nestjs+websockets@_h3leh7b3mhe2cmhloksxb646bq/node_modules/@nestjs/core/router/router-execution-context.js:38:29
at processTicksAndRejections (node:internal/process/task_queues:95:5)
at /Users/mumu/Desktop/nest-socket/node_modules/.pnpm/@nestjs+core@10.3.3_@nestjs+common@10.3.3_@nestjs+platform-express@10.3.3_@nestjs+websockets@_h3leh7b3mhe2cmhloksxb646bq/node_modules/@nestjs/core/router/router-execution-context.js:46:28
at /Users/mumu/Desktop/nest-socket/node_modules/.pnpm/@nestjs+core@10.3.3_@nestjs+common@10.3.3_@nestjs+platform-express@10.3.3_@nestjs+websockets@_h3leh7b3mhe2cmhloksxb646bq/node_modules/@nestjs/core/router/router-proxy.js:9:17 |
Beta Was this translation helpful? Give feedback.
-
@JanYork
|
Beta Was this translation helpful? Give feedback.
-
This is my dependency information, I am using socket in Nest.
I want to get the socket instance with the corresponding ID in the socket.io server through the socketID. The method is currently as follows:
const socket = first(await server.server.in(socketId).fetchSockets());
This can be obtained and is normal until I need to clean up all rooms bound to a certain socket.
When I removed all rooms and added new ones, I found that I could not obtain the socket instance through the above method. Why?
I tried to compare all the data of the socket.io server object and found that the rooms in the adapter have changed, as follows:
So I came to a result:
await server.server.in(socketId).fetchSockets()
is associated to the instance based on the k-v structure in rooms, but this is the easiest way to get the instance. Why does the author of socket.io Why not provide a more direct method without side effects? Or maybe I didn't find it?I tried to find other methods, but I didn't find that the container that actually stores the Socket instance associated with the socketID is the sockets in the socket.io server. This is a Map, the socketID is the key, and the Socket instance is the value, but I found that socket.io The developer has disabled the get method of this Map, and it seems that they don't want me to get the socket instance from it.
But I tried to bypass this restriction,
const socket = (this.server?.sockets as any).get(id);
This way I can successfully get the data I want, but I think this is not the best way, because the author deliberately limited the get() of this Map.In typescript, I saw a field prompt. There is also such a field,
this.server.sockets.sockets.get(id);
, this.server is the server of socket.io.But
this.server.sockets
is a map, why do we need to add another socket to this map object? Moreover, this socket is also a Map, and its get() is publicly available. Unfortunately, this attribute is of no use.this.server.sockets.sockets
is undefined data. Why? Wrong typescript type? Or is it intentional by the author of socket.io?Well, I haven't found the optimal method yet. Can anyone give me an answer to this question?
const sockets = await io.in("room1").fetchSockets();
to obtain the instance, but when I remove all rooms of the socket, this method is not available. Why is this? Design this method?const sockets = await io.of("/admin").in("room1").fetchSockets();
from the official documentation, but the compiler told me that of() uses undefined method, but typescript tells me that it exists, including the documentation.this.server.sockets;
be disabled? And why.sockets
is an undefined parameter, but it exists in the typescript type?I'd like to get these answers, can anyone answer me? Thanks.
Beta Was this translation helpful? Give feedback.
All reactions