packages/core/useEventSource/index.md
An EventSource or Server-Sent-Events instance opens a persistent connection to an HTTP server, which sends events in text/event-stream format.
import { useEventSource } from '@vueuse/core'
const { status, data, error, close } = useEventSource('https://event-source-url')
See the Type Declarations for more options.
You can define named events with the second parameter
import { useEventSource } from '@vueuse/core'
// ---cut---
const { event, data } = useEventSource(
'https://event-source-url',
['notice', 'update']
)
Enable by default.
Establish the connection immediately when the composable is called.
Enable by default.
If url is provided as a ref, when the url changes, it will automatically reconnect to the new url.
Reconnect on errors automatically (disabled by default).
import { useEventSource } from '@vueuse/core'
// ---cut---
const { status, data, close } = useEventSource(
'https://event-source-url',
[],
{
autoReconnect: true,
}
)
Or with more controls over its behavior:
import { useEventSource } from '@vueuse/core'
// ---cut---
const { status, data, close } = useEventSource(
'https://event-source-url',
[],
{
autoReconnect: {
retries: 3,
delay: 1000,
onFailed() {
alert('Failed to connect EventSource after 3 retries')
},
},
}
)
Apply custom transformations to incoming data using a serialization function.
import { useEventSource } from '@vueuse/core'
// ---cut---
const { data } = useEventSource(
'https://event-source-url',
[],
{
serializer: {
read: rawData => JSON.parse(rawData),
},
}
)
// If server sends: '{"name":"John","age":30}'
// data.value will be: { name: 'John', age: 30 }