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 events through a channel. A valid complete event ends the watch. The channel also closes on a terminal error or context cancellation.
WatchCVMStateOptions fields:
Returns:
<-chan CVMStateEvent — a receive-only channel of state events.
CVMStateEvent
Each event received from the channel has this structure:Event field indicates what happened:
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 after a timeout or a stream ending before completion. Configure retries to limit this behavior. An SSEerror event can be followed by more events; it does not itself close the stream.
CVMStateEvent with Event: "error" and the underlying error in Error. An incomplete EOF produces a *WatchStreamError that unwraps to io.ErrUnexpectedEOF. A permanent HTTP failure produces a terminal *APIError without retrying.
The default watch client does not apply the ordinary 60-second total HTTP timeout. Explicit WithTimeout or WithHTTPClient settings still apply. Use a context deadline to limit total waiting time.
Wait for Deployment
A common pattern is to provision a CVM and then watch it until it reaches the"running" state:

