Posting JSON request and reading EventStreams in Angular11

882 views Asked by At

Tried following the article below to read eventstreams. Instead of using formData like it is done in the article, I'm using Json to post. It works smoothly with a 200 response. Even tells that some kb of data was sent over the wire, but the eventstream tab shows nothing. The same curl works on my terminal.

https://medium.com/swlh/how-do-server-sent-events-sse-or-eventsource-work-in-angular-e9e27b6a3295

Network call of the stream

EventStream tab in the network call

1

There are 1 answers

0
Shyam On

As shown in the article

    return Observable.create((observer) => {
      const eventSource = this.sseService.getEventSourceWithPost(url, data);
      // Launch query
      eventSource.stream();
      // on answer from message listener 
      eventSource.onmessage = (event) => {
        this.zone.run(() => {
          observer.next(event.progress);
        });
      };
      eventSource.onerror = (error) => {
        this.zone.run(() => {
          observer.error(error);
        });
      };
    });

Changing this part to

    return Observable.create((observer) => {
      const eventSource = this.sseService.getEventSourceWithPost(url, data);
      // Launch query
      eventSource.stream();
      // on answer from message listener 
      eventSource.addEventListener('EVENT_NAME_FROM_STREAM', (event) => {
        this.zone.run(() => {
          observer.next(event.data);
        });
      };
    });

Got the data to show up. But in the network tab I still don't see the events.