Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
lygstate opened this issue Nov 14, 2019 · 10 comments

Comments

@lygstate
Copy link

lygstate commented Nov 14, 2019

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
@lygstate lygstate added the needs triage This issue has not been looked into label Nov 14, 2019
@mzuccaroli
Copy link

i've got the same problem

@kamilmysliwiec kamilmysliwiec added scope: websockets type: enhancement 🐺 and removed needs triage This issue has not been looked into labels Jan 5, 2020
@eduardosanzbBCG
Copy link

eduardosanzbBCG commented Jan 28, 2020

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

@Menci
Copy link

Menci commented Feb 18, 2020

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

@visurel
Copy link

visurel commented Apr 20, 2020

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

@ssilve1989
Copy link
Contributor

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

Is there any update on this?

@jmcdo29
Copy link
Member

jmcdo29 commented Jun 6, 2020

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

@psi-4ward
Copy link

+1

niksauer added a commit to CC-Wolke7/chat-like-microservice that referenced this issue Jan 13, 2021
@pxr64
Copy link

pxr64 commented Jan 26, 2021

Same issue here

@kamilmysliwiec
Copy link
Member

Let's track this here #6321

@Bennison-Devadoss
Copy link

Try the bellow one

@WebSocketGateway({ namespace: 'terminals' }) 

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests