Optimization Streaming
Get real-time updates on optimization progress using WebSocket streaming. Build responsive UIs and monitor long-running optimization jobs as they execute.WebSocket Connection
Connect to the WebSocket endpoint and subscribe to job events:Protocol
- Client connects to the WebSocket endpoint
- Client sends a subscribe message with the job ID
- Server replays historical events (if
afterEventIdis provided) - Server streams new events as they occur
- Client sends unsubscribe to stop receiving events
Message Types
Client Messages
| Message | Description | Fields |
|---|---|---|
subscribe | Subscribe to job events | jobId, afterEventId? |
unsubscribe | Stop receiving events | jobId |
replay | Request historical events | jobId, afterEventId? |
ping | Keepalive | — |
Server Messages
| Message | Description | Fields |
|---|---|---|
subscribed | Subscription confirmed | jobId |
unsubscribed | Unsubscription confirmed | jobId |
event | Real-time job event | jobId, event |
replay | Batch of historical events | data[] |
pong | Keepalive response | — |
error | Error message | error, jobId? |
Event Types
Eachevent object within the event or replay messages contains:
| Field | Type | Description |
|---|---|---|
id | number | Event ID (for replay tracking) |
eventType | string | Type of optimization event |
iteration | number | Current iteration number |
stage | string | Current optimization stage |
data | object | Event-specific payload |
timestamp | string | ISO 8601 timestamp |
Optimization Event Types
| Event Type | Description | Data Fields |
|---|---|---|
job_started | Job has begun | promptId, datasetId, config |
iteration_completed | Iteration finished | score |
job_paused | Job was paused | pausedAt, currentIteration |
job_resumed | Job was resumed | resumedAt, resumeFromIteration |
job_completed | Optimization finished | resultPromptId, finalScore |
job_failed | Optimization failed | error, errorStack, willRetry |
job_cancelled | Job was cancelled | cancelledAt, finalIteration |
UI Integration
Build a real-time progress UI using streaming events.React Example
Vue Example
Connection Management
Reconnection with Event Replay
UseafterEventId to resume from the last received event after reconnection:
Keepalive
Send periodic ping messages to prevent connection timeout:Error Handling
Handle various error scenarios:Best Practices
Clean up connections
Clean up connections
Always close the WebSocket connection and clear keepalive intervals when the component unmounts or the job completes.
Track event IDs for replay
Track event IDs for replay
Store the last received event ID so you can resume from the correct position after reconnection without missing or duplicating events.
Batch UI updates
Batch UI updates
If receiving many events quickly (especially during replay), consider batching state updates to avoid excessive re-renders.
Persist critical state
Persist critical state
For long-running optimizations, consider persisting the best result locally in case of browser refresh.