useWebSocket (useWebSocket/index.ts)

vueuse · packages/core/useWebSocket/index.ts · typescript · MIT
realtime

Reactive Vue WebSocket composable with auto-reconnect, heartbeat, send buffering, and lifecycle hooks for real-time client connections.

// Source: https://github.com/vueuse/vueuse | License: MIT | Copyright 2019-PRESENT Anthony Fu<https://github.com/antfu>
// Provided via justclaw.it.com; keep this notice with the code.
import type { AnyFn, Fn, TimerHandle } from '@vueuse/shared'
import type { MaybeRefOrGetter, ShallowRef } from 'vue'
import type { ConfigurableScheduler } from '../_configurable'
import { isClient, isWorker, toRef, tryOnScopeDispose, useIntervalFn } from '@vueuse/shared'
import { shallowRef, toValue, watch } from 'vue'
import { useEventListener } from '../useEventListener'

export type WebSocketStatus = 'OPEN' | 'CONNECTING' | 'CLOSED'
export type WebSocketHeartbeatMessage = string | ArrayBuffer | Blob

const DEFAULT_PING_MESSAGE = 'ping'

export interface UseWebSocketOptions {
  onConnected?: (ws: WebSocke

... (truncated -- full source via MCP)

See the full source, get the GitHub permalink, and search for more like it.

Get a free API key