how to get socket object from nestjs socket adapter v6 given socketId

Viewed 15

hi guys we are using customRequest hook in nestjs with redis io adapter to support scaling of the w

  • inside redis-io.adapter.ts
public createIOServer(port: number, options?: ServerOptions): any {
....
 server.of('/').adapter.customRequest = (
      userId: string,
      callback: (socketIds?: string[]) => void,
    ) => {    
       callback(
         Object.keys(server.sockets.connected)
           .map((key) => server.sockets.connected[key])
           .filter((x) => x.decodedToken.id === userId) 
           .map((x) => x.id),
       );
....
}
  • inside chat gatway we have dispatchMessage method that is used to dispatch messages to all connected sockets
private async dispatchMessage(id: string, userIds: string[], data: any) {


    (this.server.of('/').adapter as any).customRequest(
         userId,
         (error: any, socketIds: any[]) => {
           if (error) {
             // tslint:disable-next-line:no-console
            this.logger.error(error);
      }

          [].concat.apply([], socketIds).forEach((x) => {
           

             this.server.to(x).emit(id, data);
           });
         },
  );
}

now we have migrated to redis adapter version 6 that doesn't have customRequest any more, but we can access socketIds on the adapter using but we don't have access to the sockets object to get the user id

https://socket.io/blog/socket-io-redis-adapter-6-release/#customhook-and-customrequest-methods-were-removed

0 Answers
Related