I am using Nest.js and I want to send Observable stream to the client
The Nest controller is:
const stopTimer$ = timer(3000);
@Sse('getGeneratedTitle')
async test() {
return interval(1000).pipe(
map(_ => ({ data: { hello: 'world' } })),
takeUntil(stopTimer$)
);
}
And this is the method on ServiceApi on the client side:
getStreamCompletion = () => {
const eventSource = new EventSource('api/getGeneratedTitle');
eventSource.onmessage = (event) => {
console.log(event.data);
};
eventSource.onerror = (error) => {
console.error("Oops!" + error);
};
};
The problem is, I'm always getting an error on the client when the stream ended:
ServiceApi.ts:21 {"hello":"world"}
ServiceApi.ts:21 {"hello":"world"}
ServiceApi.ts:25(onerror) Oops! Event {isTrusted: true, type: 'error', target: EventSource, currentTarget: EventSource, eventPhase: 2, …}
Any thoughts what is the issue and how to solve it? Thanks!