user2650606
user2650606

Reputation: 271

NestJS websocket Broadcast event to clients

I'm not able to send data from server NestJS to clients via websocket. Nothing is emitted.

My use case:

My stack:

My code:

simple-web-socket.gateway.ts:

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 < 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);
  }
}

main.ts:

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();

app.module.ts:

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 {}

Additionnal Informations:

Client emiting (with code line c.emit(event, broadCastMessage);) return false.

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.

Upvotes: 12

Views: 25032

Answers (3)

Cyclonecode
Cyclonecode

Reputation: 30131

I tried to broadcast a message to all connected clients using client.send():

broadcastMessage(event: string, payload: any) {
  for (const client of this.clients) {
    client.send(event, payload);
  }
}

testBroadcast() {
  this.broadcastMessage('msgInfo', { name: 'foo' });
}

the data sent ended up looking like this:

["message", "msgInfo", { "name": "foo" }]

The above did not work for me, so instead I used the client.emit() which works fine:

broadcastMessage(event: string, payload: any) {
  for (const client of this.clients) {
    client.emit(event, payload);
  }
}

now the data looks like this:

["msgInfo", { "name": "foo" }]

Upvotes: 3

Philippe
Philippe

Reputation: 1059

With socket.io we could use socket.broadcast.emit()?:

@SubscribeMessage('my-event')
onChgEvent(
    @MessageBody() message: any,
    @ConnectedSocket() socket: Socket,
): void {
   socket.broadcast.emit('my-event', message);
}

Upvotes: 5

user2650606
user2650606

Reputation: 271

As mentionned in the previous comment, c.send() works fine with the following snippet:

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 < this.wsClients.length; i++) {
      if (this.wsClients[i] === client) {
        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.send(event, broadCastMessage);
    }
  }

  @SubscribeMessage('my-event')
  onChgEvent(client: any, payload: any) {
    this.broadcast('my-event',payload);
  }
}

Upvotes: 13

Related Questions