Back to Vueuse

useEventSource

packages/core/useEventSource/index.md

14.3.02.0 KB
Original Source

useEventSource

An EventSource or Server-Sent-Events instance opens a persistent connection to an HTTP server, which sends events in text/event-stream format.

Usage

ts
import { useEventSource } from '@vueuse/core'

const { status, data, error, close } = useEventSource('https://event-source-url')

See the Type Declarations for more options.

Named Events

You can define named events with the second parameter

ts
import { useEventSource } from '@vueuse/core'
// ---cut---
const { event, data } = useEventSource(
  'https://event-source-url',
  ['notice', 'update']
)

immediate

Enable by default.

Establish the connection immediately when the composable is called.

autoConnect

Enable by default.

If url is provided as a ref, when the url changes, it will automatically reconnect to the new url.

Auto Reconnection on Errors

Reconnect on errors automatically (disabled by default).

ts
import { useEventSource } from '@vueuse/core'
// ---cut---
const { status, data, close } = useEventSource(
  'https://event-source-url',
  [],
  {
    autoReconnect: true,
  }
)

Or with more controls over its behavior:

ts
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')
      },
    },
  }
)

Data Serialization

Apply custom transformations to incoming data using a serialization function.

ts
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 }