WebSocket Protocol
The OpenClaw Gateway WebSocket API provides real-time bidirectional communication for agent interactions, event streaming, and control plane operations.Connection
Endpoint
Connection Flow
- Open WebSocket connection
- Send
connectmethod with authentication - Receive
connect.challengeevent (optional, for device pairing) - Receive connection response
- Subscribe to events (optional)
- 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
idboolean
true for success, false for errorsobject
Method-specific response data (when
ok: true)object
Error details (when
ok: false)Error Response
Authentication
Authenticate during the initialconnect call:
string
required
Client role:
control, node, or webchatobject
required
Authentication credentials
string
Bearer token or device token
string
Password (alternative to token)
object
required
Client metadata
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
Send Message
List Sessions
Heartbeat
The Gateway sends periodictick events to keep connections alive:
Connection Management
Reconnection
If the WebSocket connection drops:- Wait a short delay (exponential backoff recommended)
- Re-establish WebSocket connection
- Re-send
connectmethod - Resume operations
Graceful Shutdown
The Gateway sends ashutdown event before closing:
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

