Skip to main content

WebSocket Protocol

The OpenClaw Gateway WebSocket API provides real-time bidirectional communication for agent interactions, event streaming, and control plane operations.

Connection

Endpoint

For secure connections:

Connection Flow

  1. Open WebSocket connection
  2. Send connect method with authentication
  3. Receive connect.challenge event (optional, for device pairing)
  4. Receive connection response
  5. Subscribe to events (optional)
  6. Invoke RPC methods

Example Connection

Message Format

All messages follow JSON-RPC 2.0 format.

Request Frame

string
required
Must be "2.0"
string | number
required
Unique request identifier for matching responses
string
required
RPC method name (e.g., "agent", "send", "sessions.list")
object
Method-specific parameters

Response Frame

string | number
Matches request id
boolean
true for success, false for errors
object
Method-specific response data (when ok: true)
object
Error details (when ok: false)

Error Response

Authentication

Authenticate during the initial connect call:
string
required
Client role: control, node, or webchat
object
required
Authentication credentials
string
Bearer token or device token
string
Password (alternative to token)
object
required
Client metadata
See Authentication for details.

Event Streaming

The Gateway broadcasts events to subscribed clients.

Event Frame

string
Event name
object
Event-specific data

Available Events

See Events for detailed event schemas.

Subscribing to Events

Events are automatically sent to all connected clients. No explicit subscription is required for most events. For session-specific events, use session subscriptions:

Common Methods

Invoke Agent

See Agent Protocol for details.

Send Message

See Messages for details.

List Sessions

See Sessions for details.

Heartbeat

The Gateway sends periodic tick events to keep connections alive:
Clients should respond to heartbeats or implement connection keep-alive logic.

Connection Management

Reconnection

If the WebSocket connection drops:
  1. Wait a short delay (exponential backoff recommended)
  2. Re-establish WebSocket connection
  3. Re-send connect method
  4. Resume operations

Graceful Shutdown

The Gateway sends a shutdown event before closing:
Clients should close the connection and attempt reconnection after a delay.

Error Handling

Common Error Codes

Example Error Response

Example: Complete Flow

Next Steps

Agent Protocol

Invoke agents via WebSocket

Events

Subscribe to Gateway events

Sessions

Manage agent sessions

Messages

Send messages via channels