Example from doc:
@Sse('sse')
sse(): Observable<MessageEvent> {
return interval(1000).pipe(map((_) => ({ data: { hello: 'world' } })));
}
Client side:
const eventSource = new EventSource('/sse');
eventSource.onmessage = ({ data }) => {
console.log('New message', JSON.parse(data));
};
in case we close eventSource eventSource.close() there is no possibility to stop interval(1000) and new connection will lead to create another interval(1000) which might end with unwanted memory leaks
It would be nice to have a possibility to handle connection close event on the server side and unsubscribe from interval
I'm using nest (@nestjs/core: 10.4.6) + FastifyAdapter (@nestjs/platform-fastify: 10.4.6)
The only solution I came up so far is unsubscribe from interval by timer:
@Sse('sse')
sse(): Observable<MessageEvent> {
return interval(1000).pipe(
map((_) => ({ data: { hello: 'world' } }),
takeUntil(timer(3000)),
);
}
Example from doc:
Client side:
in case we close eventSource
eventSource.close()there is no possibility to stop interval(1000) and new connection will lead to create another interval(1000) which might end with unwanted memory leaksIt would be nice to have a possibility to handle connection close event on the server side and unsubscribe from interval
I'm using nest (@nestjs/core: 10.4.6) + FastifyAdapter (@nestjs/platform-fastify: 10.4.6)
The only solution I came up so far is unsubscribe from interval by timer: