I have two nestjs gateways handling events emitted from the client. One for the 'admin' namespace and one for the 'chat' namespace. When handling one of the events on the 'admin' gateway, i would like to retrieve all of sockets that are connected to the the 'chat' namespace, but i'm not sure how to do this.
I have the admin gateway:
@WebSocketGateway({ namespace: 'admin' })
export class AdminGateway implements OnGatewayConnection, OnGatewayDisconnect {
constructor(
private logger: Logger,
) {}
handleConnection(socket: Socket) {
this.logger.log(
'Client connected to admin channel... Welcome user ',
socket.user.username,
' (#',
socket.user._id,
') on node',
process.env.port || 8080,
);
}
@SubscribeMessage('online-agents')
handleAgents();
The handle agents function is where I need to retrieve all sockets connected to the 'chat' namespace.
This is the socket-io adapter:
export class SocketIOAdapter extends IoAdapter {
private readonly logger = new Logger(SocketIOAdapter.name);
private adapterConstructor: ReturnType<typeof createAdapter>;
constructor(
private app: INestApplicationContext,
) {
super(app);
}
async connectToRedis(): Promise<void> {
const pubClient = createClient({ url: process.env.app_worker_redis_uri });
const subClient = pubClient.duplicate();
await Promise.all([pubClient.connect(), subClient.connect()]);
this.adapterConstructor = createAdapter(pubClient, subClient);
}
createIOServer(port: number, options?: ServerOptions) {
const jwtService = this.app.get(JwtService);
const usersService = this.app.get(UsersService);
const server: Server = super.createIOServer(port, options);
server.adapter(this.adapterConstructor);
server.of('admin').use(createTokenMiddleware(jwtService, usersService, this.logger));
server.of('chat').use(createTokenMiddleware(jwtService, usersService, this.logger));
return server;
}
}
I can retrieve the sockets connected to the 'chat' namespace on the adapter using server.of('chat').sockets
but I can't do this in the admin gateway. Is there perhaps a way to have communication between the gateway and the adapter, where I can call a function on the adapter or emit an event that the adapter catches and returns the sockets?