'NestJs @Sse: How to unsubscribe or close a server sent event once the expected response is sent

So the flow will be :

  1. POST request from client side.
  2. We get the data present in the request and we have a async service which takes 20-30 secs to return the response.

Currently I am doing something like

 @Sse('/sse')
 async sse(@Req() request: Request): Promise<Observable<any>> {
  const { data } = request.body;
  const responseData = await this.someService(data);
   return interval(1000).pipe(
      map((_) => { 
          return { data: responseData }; 
      }),
    );
 }

As you can imaging after we get the data from the service we are returning the same data at the mentioned interval. Is there any better approach for these kind of operations if not then how should I close this server sent event from the server.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source