Skip to main content

watchCvmState

GET /cvms/{cvmId}/state (Server-Sent Events) Watches CVM state changes in real-time via Server-Sent Events (SSE). Resolves when the CVM reaches the target status; rejects on timeout or abort. Automatically retries on connection timeouts. Parameters:
FieldTypeRequiredDescription
idstringYesCVM identifier
targetstringYesTarget status to wait for (e.g., "running", "stopped")
intervalnumberNoPolling interval in seconds (5-30, default: 5)
timeoutnumberNoPer-attempt timeout in seconds (10-600, default: 300)
maxRetriesnumberNoMaximum retry attempts (default: Infinity)
retryDelaynumberNoDelay between retries in ms (default: 5000)
Options:
FieldTypeDescription
signalAbortSignalCancellation signal
onEvent(event) => voidCallback for each SSE event
Returns: CvmState — the final state when target is reached.
FieldTypeDescription
idstringCVM ID
instance_idstringInstance ID
namestringCVM name
statusstringFinal status (matches target)
uptimestring?Uptime duration
boot_progressstring?Boot progress
boot_errorstring?Boot error if failed
Errors:
ErrorDescription
WatchAbortedErrorThrown when the operation is cancelled via AbortSignal
MaxRetriesExceededErrorThrown when maxRetries is exceeded without reaching target
Example — Wait for CVM to start:
const state = await client.watchCvmState({
  id: "my-app",
  target: "running",
  timeout: 120,
  onEvent: (event) => {
    console.log(`Status: ${event.status}, Progress: ${event.boot_progress}`);
  },
});

console.log(`CVM is now ${state.status}`);
Example — With abort support:
const controller = new AbortController();

// Cancel after 60 seconds
setTimeout(() => controller.abort(), 60_000);

try {
  const state = await client.watchCvmState({
    id: "my-app",
    target: "running",
    signal: controller.signal,
  });
} catch (error) {
  if (error instanceof WatchAbortedError) {
    console.log("Watch was cancelled");
  }
}
Unlike other SDK functions, watchCvmState does not have a safe variant. Use try/catch for error handling.