NestJS Gateways: path with slugs

1.1k views Asked by At

I am trying to create a websockets gateway in NestJS that responds to multiple paths. This is a requirement as what I am trying to build would replace an existing system. I am using WsAdapter as the underlying adapter for this.

The websockets paths could be expressed with slugs (e.g. /ws/:key) or regular expressions. The WebSocketGateway decorator, however, only accepts a single string and does not recognize slugs as such.

import { OnGatewayInit, SubscribeMessage, WebSocketGateway } from '@nestjs/websockets';
import { Logger } from '@nestjs/common';

@WebSocketGateway({path: '/ws/:key'})  // FIXME: ":key" should be treated as a slug here
export class ManagementGateway implements OnGatewayInit {
  private readonly logger = new Logger(ManagementGateway.name, false);

  afterInit(server: any): any {
    this.logger.debug('Websockets gateway initialized');
  }

  @SubscribeMessage('ctl')
  handle(client: any, data: unknown): void {
    this.logger.debug('ctl');
    // TODO
  }
}

The previous legacy implementation I am replacing does this with ws, express and regexp-based path matching.

Any hints or pointers would be greatly appreciated.

0

There are 0 answers