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.
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 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:

