LiveVue 1.0
Examples Connection Status

Connection Status

Monitor the WebSocket connection state in real-time using the useLiveConnection() hook. Track when the connection is open, connecting, closing, or closed.

What this example shows

1
useLiveConnection()
Hook for connection state
2
WebSocket State
Reactive connection status
3
Real-time Feedback
Visual connection indicator
WebSocket Connection
closed
Disconnected
Connection History
closed05:33:37.779 PM
Open your browser's DevTools and throttle the network to see state changes, or navigate away and back.

How it works

1 The useLiveConnection() hook

The useLiveConnection() hook provides reactive access to the WebSocket connection state. It returns connectionState (a ref with values "connecting", "open", "closing", or "closed") and isConnected (a computed boolean for convenience).

const { connectionState, isConnected } = useLiveConnection()

2 Reactive state tracking

Use Vue's watch() to track state changes over time. This example maintains a history of the last 5 state transitions with timestamps, demonstrating how connection state changes propagate through your component.

watch(
  connectionState,
  (newState) => {
    stateHistory.value.push({ state: newState, timestamp: new Date() })
  },
  { immediate: true }
)

3 Visual feedback

Bind the connection state to your UI with dynamic classes and animations. Show users when they're connected with a pulsing green indicator, or warn them about connection issues with red/amber states. This creates a responsive, transparent user experience.

<span :class="isConnected ? 'bg-green-500 animate-pulse' : 'bg-red-500'" />
Next up: Simple Form with useLiveForm()
View example →