how to send the exception/error for nestjs websocket of adapter @nestjs/platform-ws

Discussão em 'Angular' iniciado por Sairam, Outubro 19, 2024.

  1. Sairam

    Sairam Guest

    I am trying to send the exception using nestjs websocket based on conditions, tried using

    throw new WsException('Invalid data');

    but not sending any exception

    Here is the sample code

    import WebSocket from 'ws';
    import {
    SubscribeMessage,
    WebSocketGateway,
    WsException,
    } from '@nestjs/websockets';

    @WebSocketGateway({ path: '/api' })
    export class MainGateway {
    @SubscribeMessage('message')
    handleMessage(client: WebSocket, payload: any) {
    if (payload.id === 4) {
    throw new WsException('Invalid Data');
    }
    client.send(JSON.stringify({ id: payload.id }));
    }
    }


    and I'm creating the connection using angular here is the code snippet

    export class WsComponent implements OnInit {
    public value!: number;
    public subject$ = webSocket('ws://localhost:3000/api');

    ngOnInit(): void {
    const event = { event: 'message', data: { id: 4 } };

    this.subject$.subscribe({
    next: (v: any) => (this.value = v.id),
    error: (e) => console.error(e),
    complete: () => console.info('complete'),
    });

    this.subject$.next(event);
    }
    }



    Please help me to solve the issue

    Continue reading...

Compartilhe esta Página