Nest: feature(gateways): support multiple websocket gateways on different paths(WsAdapter) #722

Created on 14 Nov 2019  路  7Comments  路  Source: nestjs/nest

Current behavior

 ts-node .\src\main.ts    
[Nest] 10052   - 2019-11-14 16:00:07   [NestFactory] Starting Nest application...
[Nest] 10052   - 2019-11-14 16:00:07   [InstanceLoader] AppModule dependencies initialized +30ms
[Nest] 10052   - 2019-11-14 16:00:07   [TerminalsAdapter] Creating with:{"path":"/terminals"} port:0
[Nest] 10052   - 2019-11-14 16:00:07   [TerminalsGateway] Server path:/terminals
[Nest] 10052   - 2019-11-14 16:00:07   [TerminalsTestGateway] Server path:/terminals
[Nest] 10052   - 2019-11-14 16:00:07   [RoutesResolver] AppController {/}: +19ms
[Nest] 10052   - 2019-11-14 16:00:07   [RouterExplorer] Mapped {/, GET} route +18ms
[Nest] 10052   - 2019-11-14 16:00:07   [NestApplication] Nest application successfully started +17ms

Input Code

@WebSocketGateway({ path: '/terminals' })
export class TerminalsGateway implements OnGatewayInit, OnGatewayConnection, OnGatewayDisconnect {
  private clientMap: Map<WebSocket, number>;

  protected readonly logger = new Logger(TerminalsGateway.name);

  constructor() {
    this.clientMap = new Map<WebSocket, number>();
  }

  async afterInit(server: WebSocket.Server): Promise<void> {
    this.logger.log(`Server path:${server.options.path}`);
  }

  @WebSocketServer()
  server: WebSocket.Server;

  async handleConnection(client: WebSocket): Promise<void> {
    // A client has connected
    client.send('Hello, the world\n');
    this.clientMap.set(client, Date.now());
  }

  async handleDisconnect(client: WebSocket): Promise<void> {
    this.clientMap.delete(client);
  }
}

@WebSocketGateway({ path: '/terminals-test' })
export class TerminalsTestGateway
  implements OnGatewayInit, OnGatewayConnection, OnGatewayDisconnect {
  private clientMap: Map<WebSocket, number>;

  protected readonly logger = new Logger(TerminalsTestGateway.name);

  constructor() {
    this.clientMap = new Map<WebSocket, number>();
  }

  async afterInit(server: WebSocket.Server): Promise<void> {
    this.logger.log(`Server path:${server.options.path}`);
  }

  @WebSocketServer()
  server: WebSocket.Server;

  async handleConnection(client: WebSocket): Promise<void> {
    // A client has connected
    client.send('Hello, the world\n');
    this.clientMap.set(client, Date.now());
  }

  async handleDisconnect(client: WebSocket): Promise<void> {
    this.clientMap.delete(client);
  }
}


Expected behavior


TerminalsGateway and TerminalsTestGateway show serve on different endpoint /terminals, /terminals-test respectively

Possible Solution

Environment


Nest version: 6.9.0

For Tooling issues:

  • Node version: 12.13
  • Platform: Windows
websockets type

Most helpful comment

@visurel Not everyone wants to use socket.io though.

Is there any update on this?

All 7 comments

i've got the same problem

Hello, this is still open for ws-adapter on "@nestjs/websockets": "^6.11.3", "@nestjs/core": "^6.11.4", right?

I think not only WsAdapter but also SocketIoAdapter leaks this feature.

@Menci Can't this be solved in Socket.io using the "namespace" feature?

@visurel Not everyone wants to use socket.io though.

Is there any update on this?

In the case that a minimum reproduction is still needed I've created one here with reproduction steps in the README

+1

Was this page helpful?
0 / 5 - 0 ratings

Related issues

cojack picture cojack  路  3Comments

FranciZ picture FranciZ  路  3Comments

yanshuf0 picture yanshuf0  路  3Comments

mishelashala picture mishelashala  路  3Comments

VRspace4 picture VRspace4  路  3Comments