Using Server-Sent Events (SSE)
Server-Sent Events (SSE) provide real-time, unidirectional communication from the backend to the frontend. This is useful for live updates without polling.When to Use SSE
Use SSE when you need:- Real-time updates from the server (activity streams, notifications, live stats)
- Automatic reconnection on connection loss
- Lower network overhead compared to polling
SSE is unidirectional (server � client). For bidirectional communication, use WebSockets instead.
SSE Endpoint URL Pattern
All SSE endpoints use the/stream suffix:
URL Structure
- REST:
/api/{resource}/{action}- Returns snapshot of current data - SSE:
/api/{resource}/{action}/stream- Pushes real-time updates
- Accept the same query parameters
- Return the same data structure
- Apply the same filters and limits
The useSSE Composable
TheuseSSE<T> composable handles all SSE logic including connection management, auto-reconnect, and cleanup.
Import
Basic Usage
1
Initialize the composable
Call
useSSE<T>() with your expected data type and the SSE event name:2
Connect to the SSE endpoint
Call
connect() with the full SSE stream URL:3
Use the reactive data
The
data ref will automatically update when the server sends new events:Complete Example
Here’s the complete implementation from McpClientConnectionsCard.vue:API Reference
useSSE<T>()
eventName: The SSE event name to listen for (must match backend event name)options: Optional configuration
Auto-Reconnection
The composable automatically handles reconnection when the connection is lost:- Reconnects after 5 seconds by default (configurable via
reconnectDelay) - Maintains the last URL for reconnection
- Clears any pending reconnection attempts on manual
disconnect()
The composable automatically disconnects when the component unmounts via
onUnmounted().Backend SSE Event Format
Your backend SSE endpoint must send events in this format:- Parse the JSON data
- Extract the first key’s value (e.g.,
activitiesarray) - Update the
dataref

