Considering that Nest supports Websocket out of the box, would you like to see Server-Sent Events available as well?
There are lots of cases where using a simple EventSource client-side is sufficient over adding a whole new protocol to the stack (Websocket).
On a first thought, and as it's available on top of HTTP, one could have an api like this:
// follows https://www.w3.org/TR/eventsource/#eventsource
interface MessageEvent {
data: string;
id: string;
type: string;
retry?: number;
}
@Controller('updates')
class Updates {
@Sse()
publishUpdates(): Observable<Partial<MessageEvent>> {
return of({data: "Hello"}, {data: "world!"})
}
}
On the client side:
const es = new EventSource('/updates')
// outputs
// - Hello
// - world!
es.onmessage = (message.data) => { console.log(message.data) }
Sse directive would take an optional prefix and handle the sse stream (could use https://github.com/EventSource/node-ssestream although the maintenance isn't my favorite I tend to use a custom transformer), it'd handle the id
if needed.
I'd be glad to contribute such a feature if you guys are up for it!
Looks like a nice abstraction!
Would you like to create a PR for this? However, I'd prefer not to use any additional library for this. If there's anything we must implement, let's do it as a part of the PR instead of bringing external packages.
@soyuka do you plan to use the standard nodejs http? Because in case we have a fastify app or express we could have this SEE but I'm not sure if you need to receive an Adapter like Nestjs does
Please see #4842, it's indeed based on nodejs http and it will be compatible with express or fastify adapters without changes. Indeed, we're working directly on the response stream which fastify and express are based on.
Hi, is there any update on this?
PR is ready on my end, waiting on reviews from the nest team. Feel free to try this out: https://github.com/nestjs/nest/issues/4826 and give some feedbacks.
@kamilmysliwiec any idea how soon are we releasing this? Our product currently needs this as soon as possible
same here; anyone have any updates ?
Added in 7.5.0
@kamilmysliwiec @soyuka where can we see the documentation for this?
@Sharique-Hasan Check the documentation repository but it might not be added yet
Hi documentation is in https://github.com/nestjs/docs.nestjs.com/pull/1387
A small example script is available at https://github.com/nestjs/nest/pull/4842
Last but not least I created a sample application in https://github.com/nestjs/nest/tree/master/sample/28-sse
If we use pm2 to spawn multiple instance of app, will there be multiple open connection as well ?
Another question is can pass in params to identify unique user in url ?
Another question is can pass in params to identify unique user in url ?
Probably but I'd suggest to use cookies for authentification or use the mercure protocol if things get complicated, there's an implementation in node https://github.com/Ilshidur/node-mercure also.
If we use pm2 to spawn multiple instance of app, will there be multiple open connection as well ?
The instance called will open a connection.
How UseGuards can be combinated with Sse ?
import { AuthGuard } from '@nestjs/passport';
...
@UseGuards(AuthGuard())
@Sse('events')
methodHere() {
...
}
i got unexpected 401 error on client side:
const subscription = new EventSource(`${env.API}/events`);
subscription.onerror = (error) => {
console.log({error});
};
i fixed it on client request by adding headers like this:
const subscription = new EventSource(`${env.API}/events`, {
headers: {
'Authorization': 'Bearer ' + token,
}
});
so
import { AuthGuard } from '@nestjs/passport';
...
@UseGuards(AuthGuard())
works like a charm!!
Most helpful comment
Added in 7.5.0