Skip to content

Realtime Hooks

Hooks for WebSocket-based realtime data with automatic connection management.


useRealtimeChannel

Subscribe to a specific WebSocket channel/topic.

Signature

ts
function useRealtimeChannel<T = unknown>(
  options: UseRealtimeOptions & { channel: string }
): UseRealtimeChannelResult<T>

Options

PropertyTypeDefaultDescription
pathstringRequiredWebSocket endpoint path (e.g., /ws/app/realtime)
channelstringRequiredChannel name to subscribe to
debugbooleanfalseEnable debug logging
reconnectAttemptsnumber5Maximum reconnection attempts
reconnectIntervalnumber3000Interval between reconnection attempts (ms)

Return Type

PropertyTypeDescription
isConnectedbooleanCurrent connection state
lastMessageRealtimeMessage<T> | nullMost recent message received
send(message: object) => voidSend a message to the server
reconnect() => voidForce reconnection

RealtimeMessage<T>

ts
interface RealtimeMessage<T = unknown> {
  type: string
  channel?: string
  data: T
  timestamp?: string
}

useRealtimeConnection

Low-level hook for WebSocket connection management.

Signature

ts
function useRealtimeConnection(
  options: UseRealtimeOptions
): UseRealtimeConnectionResult

Options

PropertyTypeDefaultDescription
pathstringRequiredWebSocket endpoint path
debugbooleanfalseEnable debug logging
reconnectAttemptsnumber5Maximum reconnection attempts
reconnectIntervalnumber3000Interval between reconnections (ms)

Return Type

PropertyTypeDescription
isConnectedbooleanConnection state
send(message: object) => voidSend message
reconnect() => voidForce reconnect
managerWebSocketManagerUnderlying manager instance

useRealtimeMode

Global realtime mode configuration (persisted to localStorage).

Signature

ts
function useRealtimeMode(): RealtimeModeState

Return Type

PropertyTypeDescription
mode'websocket' | 'polling'Current mode
setMode(mode: RealtimeMode) => voidSet mode

RealtimeMode

ts
type RealtimeMode = 'websocket' | 'polling'

useRealtimeCounter

Higher-level hook for realtime counter values with trend tracking.

Signature

ts
function useRealtimeCounter(
  options: UseRealtimeCounterOptions
): UseRealtimeCounterResult

Options

PropertyTypeDefaultDescription
pathstringRequiredWebSocket endpoint path
channelstringRequiredChannel name
countFieldstring'count'Field in message data containing the count
debugbooleanfalseEnable debug logging

Return Type

PropertyTypeDescription
countnumber | nullCurrent count value
previousCountnumber | nullPrevious count value
trend'up' | 'down' | 'stable'Trend direction
isConnectedbooleanConnection state
refetch() => voidForce reconnection

useRealtimeList

Higher-level hook for realtime lists with new-item highlighting.

Signature

ts
function useRealtimeList<T>(
  options: UseRealtimeListOptions<T>
): UseRealtimeListResult<T>

Options

PropertyTypeDefaultDescription
pathstringRequiredWebSocket endpoint path
channelstringRequiredChannel name
maxItemsnumber20Maximum items to keep in list
extractItem(msg: RealtimeMessage) => T | nullmsg.data as TExtract item from message
getItemId(item: T) => stringitem.idGet unique ID from item
debugbooleanfalseEnable debug logging

Return Type

PropertyTypeDescription
itemsT[]Current list of items
newItemIdsSet<string>IDs of newly added items (for highlighting)
isConnectedbooleanConnection state
refetch() => voidForce reconnection

useRealtimeMessages

Subscribe to all messages from a WebSocket endpoint (no channel filtering).

Signature

ts
function useRealtimeMessages<T = unknown>(
  options: UseRealtimeOptions
): UseRealtimeMessagesResult<T>

Return Type

PropertyTypeDescription
isConnectedbooleanConnection state
lastMessageRealtimeMessage<T> | nullMost recent message
send(message: object) => voidSend message
reconnect() => voidForce reconnection

useRealtimeWithFallback

Utility hook for automatic mode switching between polling and WebSocket.

Signature

ts
function useRealtimeWithFallback<TPolling, TWebSocket>(
  pollingHook: () => TPolling,
  websocketHook: () => TWebSocket,
  options?: { forceMode?: RealtimeMode }
): (TPolling | TWebSocket) & { mode: RealtimeMode }

Parameters

ParameterTypeDescription
pollingHook() => TPollingHook that returns polling data
websocketHook() => TWebSocketHook that returns WebSocket data
options.forceModeRealtimeModeOverride global mode

WebSocketManager

Singleton manager for WebSocket connections.

Static Methods

ts
// Get or create a manager instance for a URL
static getInstance(config: WebSocketManagerConfig): WebSocketManager

// Get existing manager by URL
static getInstanceByUrl(url: string): WebSocketManager | undefined

Instance Methods

MethodSignatureDescription
subscribe(listener: MessageListener) => () => voidSubscribe to all messages
subscribeToChannel(channel: string, listener) => () => voidSubscribe to a channel
subscribeToConnection(listener: ConnectionListener) => () => voidSubscribe to connection state
subscribeToErrors(listener: ErrorListener) => () => voidSubscribe to errors
send(message: object) => voidSend a message
reconnect() => voidForce reconnection
getConnectionState() => booleanGet connection state
getUrl() => stringGet WebSocket URL

buildWebSocketUrl

ts
function buildWebSocketUrl(path: string): string
// Builds full WebSocket URL: ws://host/path or wss://host/path

See also