Empty message event in SSE using NestJS

Discussão em 'Angular' iniciado por Michał B, Setembro 25, 2024.

  1. Michał B

    Michał B Guest

    I'm trying to do event emitter on SSE in NestJS. Here's my code:

    @Sse('sse')
    sse(): Observable<MessageEvent> {
    return interval(1000).pipe(map((_) => ({ data: { hello: 'world' } }) as MessageEvent));
    // return fromEvent(this.eventEmitter, 'new-order').pipe(
    // map((data) => {
    // return new MessageEvent('new-order', { data: 'new order' });
    // }),
    // );
    }


    I tried to replicate interval function from docs to check if this will be working. In both of above return functions events are emitted but on the frontend side only event that is working is onopen.

    this.eventSource = new EventSource('http://localhost:3000/sse');
    this.eventSource.onopen = (event) => {
    console.log('Open');
    };
    this.eventSource.onmessage = (event) => {
    console.log('New message: ' + event.data);
    };


    In Devtools Network interval is registered every second, but Eventstream tab is empty.

    [​IMG]

    Continue reading...

Compartilhe esta Página