title: NestJS websocket Broadcast event to clients
body: <p>I'm not able to send data from server NestJS to clients via websocket. Nothing is emitted.</p>
  <p><strong>My use case:</strong></p>
  <ul>
  <li>several clients connected to a server via websocket</li>
  <li>client sends a message to the server via websocket</li>
  <li>server broadcast the message to all client</li>
  </ul>
  <p><strong>My stack:</strong></p>
  <ul>
  <li>NestJS server with websocket</li>
  <li>Angular client and other (like chrome extension for testing websockets)</li>
  </ul>
  <p><strong>My code:</strong></p>
  <p><em>simple-web-socket.gateway.ts:</em></p>
  <pre class="lang-ts prettyprint-override"><code>import { SubscribeMessage, WebSocketGateway, WsResponse, WebSocketServer, OnGatewayConnection, OnGatewayDisconnect, OnGatewayInit } from '@nestjs/websockets';
  
  @WebSocketGateway({ port: 9995, transports: ['websocket'] })
  export class SimpleWebSocketGateway implements OnGatewayConnection, OnGatewayDisconnect, OnGatewayInit {
  
    @WebSocketServer() private server: any;
    wsClients=[];
    afterInit() {
      this.server.emit('testing', { do: 'stuff' });
    }
  
    handleConnection(client: any) {
      this.wsClients.push(client);
    }
  
    handleDisconnect(client) {
      for (let i = 0; i &lt; this.wsClients.length; i++) {
        if (this.wsClients[i].id === client.id) {
          this.wsClients.splice(i, 1);
          break;
        }
      }
      this.broadcast('disconnect',{});
    }
    private broadcast(event, message: any) {
      const broadCastMessage = JSON.stringify(message);
      for (let c of this.wsClients) {
        c.emit(event, broadCastMessage);
      }
    }
  
    @SubscribeMessage('my-event')
    onChgEvent(client: any, payload: any) {
      this.broadcast('my-event',payload);
    }
  }
  </code></pre>
  <p><em>main.ts:</em></p>
  <pre class="lang-ts prettyprint-override"><code>import { NestFactory } from '@nestjs/core';
  import { AppModule } from './app.module';
  import { WsAdapter } from '@nestjs/websockets';
  
  async function bootstrap() {
    const app = await NestFactory.create(AppModule);
    app.useWebSocketAdapter(new WsAdapter());
    await app.listen(3000);
  }
  bootstrap();
  </code></pre>
  <p><em>app.module.ts:</em></p>
  <pre class="lang-ts prettyprint-override"><code>import { Module } from '@nestjs/common';
  import { AppController } from './app.controller';
  import { AppService } from './app.service';
  import { SimpleWebSocketGateway } from 'simple-web-socket/simple-web-socket.gateway';
  
  @Module({
    imports: [],
    controllers: [AppController],
    providers: [AppService, SimpleWebSocketGateway],
  })
  export class AppModule {}
  </code></pre>
  <p><strong>Additionnal Informations:</strong></p>
  <p>Client emiting (with code line c.emit(event, broadCastMessage);) return false.</p>
  <p>I suspect an error in the framework as my usage is quite simple. But I want to double-check with the community here if I'm doing something wrong.</p>