You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
You can enable Server-Sent events on a route with Nest. This can be used to push real-time updates to your client using HTTP. More informations about this specification can be found [on the whatwg website](https://html.spec.whatwg.org/multipage/server-sent-events.html).
3
+
Server-Sent Events (SSE) is a server push technology enabling a client to receive automatic updates from a server via HTTP connection. Each notification is sent as a block of text terminated by a pair of newlines (learn more [here](https://developer.mozilla.org/en-US/docs/Web/API/Server-sent_events)).
4
4
5
5
#### Usage
6
6
7
-
In this example, we set a route named `/sse` that will allow you to propagate real time updates. These events can be listened to using the Javascript [EventSource API](https://developer.mozilla.org/en-US/docs/Web/API/EventSource). On the `/`route, for the sake of the example, we're logging the received data in the browser's console.
7
+
To enable Server-Sent events on a route (route registered within a **controller class**), annotate the method handler with the `@Sse()` decorator.
8
8
9
-
Note the `@Sse` decorator that hooks the Server-Sent events mechanism on the chosen path:
The events are sent via an Observable emitting a `MessageEvent`. The MessageEvent should respect the following interface to match the specification:
16
+
> info **Hint** The `@Sse()` decorator is imported from the `@nestjs/common`, while `Observable`, `interval`, `and map` are imported from the `rxjs` package.
34
17
35
-
```
18
+
> warning **Warning** Server-Sent Events routes must return an `Observable` stream.
19
+
20
+
In the example above, we defined a route named `sse` that will allow us to propagate real-time updates. These events can be listened to using the [EventSource API](https://developer.mozilla.org/en-US/docs/Web/API/EventSource).
21
+
22
+
The `sse` method returns an `Observable` that emits multiple `MessageEvent` (in this example, it emits a new `MessageEvent` every second). The `MessageEvent` object should respect the following interface to match the specification:
You can find a working example in the [Nest repository](https://github.com/nestjs/nest/tree/master/sample/28-sse).
33
+
With this in place, we can now create an instance of the `EventSource` class in our client-side application, passing the `/see` (which is a route string matching what we have passed into the `@Sse()` decorator above) as a constructor argument.
34
+
35
+
`EventSource` instance opens a persistent connection to an HTTP server, which sends events in `text/event-stream` format. The connection remains open until closed by calling `EventSource.close()`.
36
+
37
+
Once the connection is opened, incoming messages from the server are delivered to your code in the form of events. If there is an event field in the incoming message, the triggered event is the same as the event field value. If no event field is present, then a generic `message` event is fired ([source](https://developer.mozilla.org/en-US/docs/Web/API/EventSource)).
38
+
39
+
```javascript
40
+
consteventSource=newEventSource('/sse');
41
+
eventSource.onmessage= ({ data }) => {
42
+
console.log('New message', JSON.parse(data));
43
+
};
44
+
```
45
+
46
+
You can find a working example [here](https://github.com/nestjs/nest/tree/master/sample/28-sse).
0 commit comments