Loading source
Pulling the file list, source metadata, and syntax-aware rendering for this listing.
Source from repo
Apply VueUse composables in Vue 3/Nuxt projects to replace custom implementations with battle-tested utilities.
Files
Skill
Size
Entrypoint
Format
Open file
Syntax-highlighted preview of this file as included in the skill package.
references/useEventSource.md
1---2category: Network3---45# useEventSource67An [EventSource](https://developer.mozilla.org/en-US/docs/Web/API/EventSource) or [Server-Sent-Events](https://developer.mozilla.org/en-US/docs/Web/API/Server-sent_events) instance opens a persistent connection to an HTTP server, which sends events in text/event-stream format.89## Usage1011```ts12import { useEventSource } from '@vueuse/core'1314const { status, data, error, close } = useEventSource('https://event-source-url')15```1617See the [Type Declarations](#type-declarations) for more options.1819### Named Events2021You can define named events with the second parameter2223```ts24import { useEventSource } from '@vueuse/core'25// ---cut---26const { event, data } = useEventSource(27'https://event-source-url',28['notice', 'update']29)30```3132### immediate3334Enable by default.3536Establish the connection immediately when the composable is called.3738### autoConnect3940Enable by default.4142If url is provided as a ref, when the url changes, it will automatically reconnect to the new url.4344### Auto Reconnection on Errors4546Reconnect on errors automatically (disabled by default).4748```ts49import { useEventSource } from '@vueuse/core'50// ---cut---51const { status, data, close } = useEventSource(52'https://event-source-url',53[],54{55autoReconnect: true,56}57)58```5960Or with more controls over its behavior:6162```ts63import { useEventSource } from '@vueuse/core'64// ---cut---65const { status, data, close } = useEventSource(66'https://event-source-url',67[],68{69autoReconnect: {70retries: 3,71delay: 1000,72onFailed() {73alert('Failed to connect EventSource after 3 retries')74},75},76}77)78```7980### Data Serialization8182Apply custom transformations to incoming data using a serialization function.8384```ts85import { useEventSource } from '@vueuse/core'86// ---cut---87const { data } = useEventSource(88'https://event-source-url',89[],90{91serializer: {92read: rawData => JSON.parse(rawData),93},94}95)9697// If server sends: '{"name":"John","age":30}'98// data.value will be: { name: 'John', age: 30 }99```100101## Type Declarations102103```ts104export type EventSourceStatus = "CONNECTING" | "OPEN" | "CLOSED"105export interface UseEventSourceOptions<Data> extends EventSourceInit {106/**107* Enabled auto reconnect108*109* @default false110*/111autoReconnect?:112| boolean113| {114/**115* Maximum retry times.116*117* Or you can pass a predicate function (which returns true if you want to retry).118*119* @default -1120*/121retries?: number | (() => boolean)122/**123* Delay for reconnect, in milliseconds124*125* @default 1000126*/127delay?: number128/**129* On maximum retry times reached.130*/131onFailed?: Fn132}133/**134* Immediately open the connection when calling this composable135*136* @default true137*/138immediate?: boolean139/**140* Automatically connect to the websocket when URL changes141*142* @default true143*/144autoConnect?: boolean145/**146* Custom data serialization147*/148serializer?: {149read: (v?: string) => Data150}151}152export interface UseEventSourceReturn<Events extends string[], Data = any> {153/**154* Reference to the latest data received via the EventSource,155* can be watched to respond to incoming messages156*/157data: ShallowRef<Data | null>158/**159* The current state of the connection, can be only one of:160* 'CONNECTING', 'OPEN' 'CLOSED'161*/162status: ShallowRef<EventSourceStatus>163/**164* The latest named event165*/166event: ShallowRef<Events[number] | null>167/**168* The current error169*/170error: ShallowRef<Event | null>171/**172* Closes the EventSource connection gracefully.173*/174close: EventSource["close"]175/**176* Reopen the EventSource connection.177* If there the current one is active, will close it before opening a new one.178*/179open: Fn180/**181* Reference to the current EventSource instance.182*/183eventSource: Ref<EventSource | null>184/**185* The last event ID string, for server-sent events.186* @see https://developer.mozilla.org/en-US/docs/Web/API/MessageEvent/lastEventId187*/188lastEventId: ShallowRef<string | null>189}190/**191* Reactive wrapper for EventSource.192*193* @see https://vueuse.org/useEventSource194* @see https://developer.mozilla.org/en-US/docs/Web/API/EventSource/EventSource EventSource195* @param url196* @param events197* @param options198*/199export declare function useEventSource<Events extends string[], Data = any>(200url: MaybeRefOrGetter<string | URL | undefined>,201events?: Events,202options?: UseEventSourceOptions<Data>,203): UseEventSourceReturn<Events, Data>204```205