WebSocket
IvyWebSocket class for real-time job progress via WebSocket with auto-reconnection
IvyWebSocket lets you stream a job's progress in real time, pushing phase, percentage, and status changes to your app as the computation runs instead of polling for them. Reach for it when you want live updates and are willing to manage the connection yourself; it handles reconnection, keepalive, and multiple subscribers per job for you. In React apps you'd typically consume it through the useIvyWebSocket hook rather than instantiating this class directly.
SDK WebSocket Reference
Planned specification. @ivybiosciences/sdk is not released, and there is no package to install. This page describes the SDK's planned interface. To call the platform today, use the REST API, the ivybloom CLI, or the MCP server.
The IvyWebSocket class provides real-time job progress updates via WebSocket connections. It handles automatic reconnection, keepalive pings, and multi-listener subscriptions.
Access via ivy.websocket or instantiate directly.
Overview
External clients cannot open a job WebSocket today (see WebSocket Protocol), so until the SDK ships, follow jobs with the SSE job event stream; the Python tabs below use it. Inside the platform, the backend pushes status updates whenever a job's phase, progress percentage, or status changes — typically every 1-5 seconds during active computation.
Features
- Automatic reconnection with exponential backoff on unexpected disconnection
- 25-second keepalive pings to prevent proxy/load balancer timeouts
- Multi-listener support (multiple subscribers per job)
- Transparent URL scheme conversion (
https→wss,http→ws)
Constructor
| Parameter | Type | Description |
|---|---|---|
baseUrl | string | API base URL (https:// or http://). Converted to wss:// or ws:// |
getToken | () => Promise<string> | Async function returning the auth token |
options | WebSocketOptions | Connection behavior configuration |
WebSocketOptions
| Field | Type | Default | Description |
|---|---|---|---|
autoReconnect | boolean | true | Reconnect on unexpected close (non-1000 code) |
maxReconnectAttempts | number | 5 | Max consecutive reconnection attempts |
reconnectInterval | number | 1000 | Base delay in ms (doubles each attempt) |
onReconnect | (attempt: number) => void | — | Called on each reconnection attempt |
onClose | (code: number, reason: string) => void | — | Called when connection closes |
websocket.connect(jobId)
Connect to the authenticated WebSocket endpoint for a specific job.
No public WebSocket URL serves job updates. Until the SDK ships, follow a job with the SSE stream GET /api/v1/jobs/{jobId}/events (SSE Streaming).
Parameters:
| Parameter | Type | Description |
|---|---|---|
jobId | string | The job identifier to subscribe to |
Throws: IvyWebSocketError if the connection fails.
Examples
websocket.subscribe(jobId, callback)
Subscribe to progress updates for a specific job.
Multiple subscribers can listen to the same job simultaneously.
Parameters:
| Parameter | Type | Description |
|---|---|---|
jobId | string | The job identifier to listen for |
callback | (data: JobProgress) => void | Invoked on each progress message |
Returns: An unsubscribe function — call it to stop receiving events.
Examples
websocket.disconnect()
Gracefully disconnect the WebSocket connection.
Sends close code 1000 (normal closure) to the server, stops keepalive pings, and clears connection state. Does not trigger auto-reconnect.
websocket.isConnected
Whether the WebSocket is currently in the OPEN state.
Reconnection Strategy
When the connection drops unexpectedly (non-1000 close code):
The onReconnect callback fires on each attempt, useful for showing UI indicators.
Message Format
The WebSocket receives JSON messages with this structure:
The SDK normalizes this to a JobProgress object:
WebSocket Close Codes
| Code | Meaning | SDK Behavior |
|---|---|---|
1000 | Normal closure | No reconnect |
1001 | Server going away (deploy) | Auto-reconnect |
1006 | Abnormal closure (network) | Auto-reconnect |
1008 | Policy violation (bad token) | Auto-reconnect (new token fetched) |
4001 | Too many connections per user | No reconnect (close other connections) |
4002 | Too many connections per job | No reconnect (close other connections) |
4003 | Authentication failed | No reconnect (re-authenticate) |
4004 | Guest feature disabled | No reconnect |
Full Example
Was this page helpful?