Skip to content

No possibility to close connection while using @Sse decorator #3296

@gh-igor

Description

@gh-igor

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)),
  );
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions