Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions docs/docs/api/EventSource.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,31 @@ eventSource.onmessage = (event) => {
}
```

## Connection lifecycle (`text/event-stream`)

`EventSource` keeps a long-lived `text/event-stream` connection open and
automatically reconnects when appropriate.

Call `.close()` when you no longer need events to close the stream and release
its underlying connection resources:

```mjs
import { EventSource } from 'undici'

const es = new EventSource('http://localhost:3000/events')

es.onmessage = (event) => {
console.log('event:', event.data)
}

es.onerror = (error) => {
console.error('event stream error', error)
}

// Later, for example on shutdown:
es.close()
```

## Using a custom Dispatcher

undici allows you to set your own Dispatcher in the EventSource constructor.
Expand Down
Loading