useEventSource
一個 EventSource 或 Server-Sent-Events 實例開啟與 HTTP 伺服器的持久連線,該伺服器以 text/event-stream 格式傳送事件。
用法
js
import { useEventSource } from '@vueuse/core'
const { status, data, error, close } = useEventSource('https://event-source-url')
請參閱類型宣告以了解更多選項。
命名事件
您可以使用第二個參數定義命名事件
ts
import { useEventSource } from '@vueuse/core'
const { event, data } = useEventSource('https://event-source-url', ['notice', 'update'] as const)
js
import { useEventSource } from '@vueuse/core'
const { event, data } = useEventSource('https://event-source-url', [
'notice',
'update',
])
immediate
預設啟用。
在呼叫 composable 時立即建立連線。
autoConnect
預設啟用。
如果 url 作為 ref 提供,當 url 變更時,它將自動重新連線到新的 url。
錯誤時自動重新連線
在發生錯誤時自動重新連線(預設停用)。
js
const { status, data, close } = useEventSource('https://event-source-url', [], {
autoReconnect: true,
})
或使用更多控制其行為的方式
js
const { status, data, close } = useEventSource('https://event-source-url', [], {
autoReconnect: {
retries: 3,
delay: 1000,
onFailed() {
alert('Failed to connect EventSource after 3 retries')
},
},
})
類型宣告
顯示類型宣告
typescript
export type EventSourceStatus = "CONNECTING" | "OPEN" | "CLOSED"
export interface UseEventSourceOptions extends EventSourceInit {
/**
* Enabled auto reconnect
*
* @default false
*/
autoReconnect?:
| boolean
| {
/**
* Maximum retry times.
*
* Or you can pass a predicate function (which returns true if you want to retry).
*
* @default -1
*/
retries?: number | (() => boolean)
/**
* Delay for reconnect, in milliseconds
*
* @default 1000
*/
delay?: number
/**
* On maximum retry times reached.
*/
onFailed?: Fn
}
/**
* Immediately open the connection when calling this composable
*
* @default true
*/
immediate?: boolean
/**
* Automatically connect to the websocket when URL changes
*
* @default true
*/
autoConnect?: boolean
}
export interface UseEventSourceReturn<Events extends string[], Data = any> {
/**
* Reference to the latest data received via the EventSource,
* can be watched to respond to incoming messages
*/
data: ShallowRef<Data>
/**
* The current state of the connection, can be only one of:
* 'CONNECTING', 'OPEN' 'CLOSED'
*/
status: ShallowRef<EventSourceStatus>
/**
* The latest named event
*/
event: ShallowRef<Events[number] | null>
/**
* The current error
*/
error: ShallowRef<Event | null>
/**
* Closes the EventSource connection gracefully.
*/
close: EventSource["close"]
/**
* Reopen the EventSource connection.
* If there the current one is active, will close it before opening a new one.
*/
open: Fn
/**
* Reference to the current EventSource instance.
*/
eventSource: Ref<EventSource | null>
/**
* The last event ID string, for server-sent events.
* @see https://developer.mozilla.org/en-US/docs/Web/API/MessageEvent/lastEventId
*/
lastEventId: ShallowRef<string | null>
}
/**
* Reactive wrapper for EventSource.
*
* @see https://vueuse.dev.org.tw/useEventSource
* @see https://developer.mozilla.org/en-US/docs/Web/API/EventSource/EventSource EventSource
* @param url
* @param events
* @param options
*/
export declare function useEventSource<Events extends string[], Data = any>(
url: MaybeRefOrGetter<string | URL | undefined>,
events?: Events,
options?: UseEventSourceOptions,
): UseEventSourceReturn<Events>
原始碼
貢獻者
更新日誌
v12.8.1
於 3/5/2025v12.8.0
於 3/5/2025v12.5.0
於 1/22/2025v12.2.0-beta.1
於 12/23/2024v12.0.0-beta.1
於 11/21/2024v10.10.0
於 5/27/2024v10.8.0
於 2/20/2024v10.1.1
於 5/1/2023