Skip to content

Commit 6607d28

Browse files
committed
📘 doc: annotate sse
1 parent 9aa47ce commit 6607d28

File tree

1 file changed

+21
-0
lines changed

1 file changed

+21
-0
lines changed

docs/essential/handler.md

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -415,6 +415,27 @@ const app = new Elysia()
415415
416416
This this example, we may stream a response by using `yield` keyword.
417417
418+
## Server Sent Events (SSE)
419+
Elysia supports [Server Sent Events](https://developer.mozilla.org/en-US/docs/Web/API/Server-sent_events) by providing a `sse` utility function.
420+
421+
```typescript twoslash
422+
import { Elysia, sse } from 'elysia'
423+
424+
new Elysia()
425+
.get('/sse', function* () {
426+
yield sse('hello world')
427+
yield sse({
428+
event: 'message',
429+
data: {
430+
message: 'This is a message',
431+
timestamp: new Date().toISOString()
432+
},
433+
})
434+
})
435+
```
436+
437+
When a value is wrapped in `sse`, Elysia will automatically set the response headers to `text/event-stream` and format the data as an SSE event.
438+
418439
### Set headers
419440
Elysia will defers returning response headers until the first chunk is yielded.
420441

0 commit comments

Comments
 (0)