nestjs / nest

A progressive Node.js framework for building efficient, scalable, and enterprise-grade server-side applications with TypeScript/JavaScript 🚀
https://nestjs.com
MIT License
67.73k stars 7.63k forks source link

SubscribeMessage Events Fire Multiple Times When using Class Inheritance #2557

Closed nawilson closed 5 years ago

nawilson commented 5 years ago

Bug Report

Current Behaviour

I have created a websocket gateway (child.ts) that extends an abstract class (parent.ts). When the same event, e.g. @SubscribeMessage('test1') is in both the parent and the child, the event response is fired twice. I.e. on a single event, test1, the server will provide 2 responses: ["test1", "child"] and ["test1", "child"].

Input Code

parent.ts

import { SubscribeMessage, WsResponse } from '@nestjs/websockets';
import { Observable, of } from 'rxjs';

export abstract class Parent {
  protected constructor() {
  }

  @SubscribeMessage('test1')
  public onTest1(client: any, data?: any): Observable<WsResponse<any>> {
    const event = 'test1';

    return of({event, data: 'parent'});
  }

  @SubscribeMessage('test2')
  public onTest2(client: any, data?: any): Observable<WsResponse<any>> {
    const event = 'test2';

    return of({event, data: 'parent'});
  }
}

child.ts

import { SubscribeMessage, WebSocketGateway, WsResponse } from '@nestjs/websockets';
import { Observable, of } from 'rxjs';
import { Parent } from './parent';

@WebSocketGateway()
export class Child extends Parent {
  constructor() {
    super();
  }

  @SubscribeMessage('test1')
  public onTest1(client: any, data?: any): Observable<WsResponse<any>> {
    const event = 'test1';

    return of({event, data: 'child'});
  }
}

app.module.ts

import { Module } from '@nestjs/common';
import { Child } from './child';

@Module({
  imports: [],
  providers: [
    Child
  ],
  controllers: []
})
export class ApplicationModule {
}

main.ts

import { NestFactory } from '@nestjs/core';
import { ApplicationModule } from './app.module';

async function bootstrap() {
  const app = await NestFactory.create(ApplicationModule);

  await app.listen(80);
}

bootstrap();

Expected Behaviour

The overridden event listener should only fire once based on the method provided in the child. The response ["test1", "child"] should only be received once by the client.

My goal is to have a generic class with standardized event listeners, that can be extended and overridden by a more specific implementation as required based on different project/gateway requirements.

Environment


Nest version: 6.5.2
Typescript: 3.5.3

For Tooling issues:
- Node version: 10.15.0
- Platform:  Mac            
kamilmysliwiec commented 5 years ago

Fixed in 6.7.0 :)

lock[bot] commented 4 years ago

This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.