TheDocumentation Index
Fetch the complete documentation index at: https://docs.phala.com/llms.txt
Use this file to discover all available pages before exploring further.
WatchCVMState method streams CVM state changes in real-time using Server-Sent Events (SSE). It returns a Go channel that emits events as the CVM transitions between states, making it natural to use with for range loops.
WatchCVMState
GET /cvms/{cvmId}/state (SSE stream)
Opens an SSE connection and delivers state events through a channel. The channel is closed when the stream ends, the target state is reached, or the context is cancelled.
| Field | Type | Required | Description |
|---|---|---|---|
cvmID | string | Yes | CVM identifier |
opts | *WatchCVMStateOptions | No | Stream options (pass nil for defaults) |
| Field | Type | Default | Description |
|---|---|---|---|
Target | string | "" | Target state to wait for (e.g., "running", "stopped") |
Interval | int | 5 | Polling interval in seconds (5-30) |
Timeout | int | 60 | Server-side timeout in seconds (10-600) |
MaxRetries | *int | nil (unlimited) | Max reconnection attempts; nil = unlimited, 0 = no retries |
RetryDelay | time.Duration | 5s | Delay between reconnection attempts |
<-chan CVMStateEvent — a receive-only channel of state events.
CVMStateEvent
Each event received from the channel has this structure:Event field indicates what happened:
| Event | Meaning |
|---|---|
"state" | A state update; Data contains the current state |
"complete" | Target state reached; stream will close |
"error" | An error occurred; check the Error field |
"timeout" | Server-side timeout reached; stream will close |
Basic Usage
The simplest pattern is ranging over the channel until it closes:Cancellation with Context
Use context cancellation as the Go equivalent of JavaScript’sAbortController. Cancelling the context closes the SSE connection and the channel.
Always use a context with a timeout or deadline to avoid watching indefinitely. The server-side
Timeout option is a safety net, but client-side context timeouts give you more control.Retry Behavior
By default, the watcher reconnects indefinitely when the SSE connection drops. Configure retries to limit this behavior.CVMStateEvent with Event: "error" and the underlying error in the Error field. Then the channel closes.
Wait for Deployment
A common pattern is to provision a CVM and then watch it until it reaches the"running" state:

