How does a client connect to the OpenClaw Gateway's WebSocket API, and why must its first frame be connect?
answer
- introduce yourself before anything else
- challenge nonce from the Gateway
- role, scopes, device, auth
- hello-ok with snapshot and policy
- anything else first: hard close
basics
~20 sA client opens a WebSocket to the Gateway, receives a connect.challenge nonce, and sends a connect request declaring its role, scopes, auth and signed device identity. The Gateway answers hello-ok; any non-connect first frame is a hard close.
solid answer
~40 sThe Gateway first pushes a `connect.challenge` event with a nonce and timestamp. The client's first frame must be a `connect` request carrying its protocol range, client description, `role` (`operator` or `node`), requested scopes, node caps and commands, the shared secret if configured, and a device identity that signs the nonce. The Gateway checks auth and pairing and replies `hello-ok` with the negotiated protocol, discovery lists of methods and events, a presence-and-health `snapshot`, the granted role and scopes, and payload limits. After that, traffic is `req`/`res` pairs plus pushed events, with idempotency keys on `send` and `agent`, and no replay of missed events. Connect comes first because the Gateway must know who is calling, and with what authority, before it dispatches anything; a non-JSON or non-connect first frame is closed hard.
code
json · 20 lines{
"type": "req",
"id": "c1",
"method": "connect",
"params": {
"minProtocol": 4,
"maxProtocol": 4,
"client": { "id": "cli", "version": "1.0.0", "platform": "linux", "mode": "operator" },
"role": "operator",
"scopes": ["operator.read"],
"auth": { "token": "<gateway-token>" },
"device": {
"id": "<device-id>",
"publicKey": "<public-key>",
"signature": "<signature-over-nonce>",
"signedAt": 1737264000000,
"nonce": "<nonce-from-connect.challenge>"
}
}
}go deeper
Recall the order: the Gateway sends a challenge, the client answers with connect, the Gateway replies hello-ok, and only then do requests and events flow.
Explain what connect declares (role, scopes, caps, auth, device identity) and what hello-ok returns, especially the granted auth block, the snapshot and the policy limits.
Show the operational edges: retryable startup errors, idempotency keys on send and agent, refreshing on event gaps because nothing is replayed, and proving RPC with gateway status --require-rpc.
Argue why one authenticated, typed handshake on a single port lets very different clients share the Gateway safely, and what that costs a team writing its own client.
## One port, one control plane Every operator tool and every device reaches the OpenClaw Gateway the same way: a **WebSocket** on the Gateway's port, `18789` unless changed, carrying text frames with JSON payloads. The protocol is typed. TypeBox schemas define it, JSON Schema is generated from them, and the Gateway validates each inbound frame against that schema. The published `@openclaw/gateway-protocol` package carries the schemas and types, and `@openclaw/gateway-client` is the reference client. What makes the protocol safe to expose to several kinds of client is that nothing happens on a socket until it has introduced itself. That introduction is the **connect handshake**. ## The handshake, step by step 1. **The Gateway challenges.** Right after the socket opens, the Gateway pushes an event named `connect.challenge` whose payload carries a `nonce` and a `ts` timestamp. 2. **The client sends `connect`.** Its first frame is a request, `{type: "req", id, method: "connect", params}`. Device-auth clients sign the challenge nonce with their device key and use the challenge `ts` as the signing time. 3. **The Gateway checks and answers.** It verifies auth, device identity and pairing, settles the role and scopes, and replies with a response whose payload type is `hello-ok`, or with an error and a close. 4. **Normal traffic begins.** Only now may the client call methods or receive the events it is entitled to. While the Gateway is still finishing startup work, `connect` can return a retryable `UNAVAILABLE` error with `details.reason: "startup-sidecars"` and a `retryAfterMs`; a client should retry within its budget rather than give up. ## What the connect request carries - **Protocol range:** `minProtocol` and `maxProtocol`; the 2026.9.9 Gateway speaks version 4. - **Client description:** an id, version, platform and mode, plus locale and user agent. - **Role:** `operator` for control-plane clients such as the CLI or Control UI, `node` for capability hosts, `worker` for cloud execution on its own closed protocol. - **Scopes:** for operators, a subset of `operator.read`, `operator.write`, `operator.admin`, `operator.approvals`, `operator.pairing` and the rest of the closed set. - **Caps, commands and permissions:** what a node offers, treated by the Gateway as claims it checks against its own allowlists. - **Auth and device:** the shared secret if one is configured, and a device identity with public key and signature. ## What hello-ok returns | Field | What it tells the client | | --- | --- | | `protocol` | the negotiated protocol version | | `server` | Gateway version and this connection's id | | `features` | discovery lists of methods and events, not a dump of every route | | `snapshot` | the current presence and health picture to start from | | `auth` | the granted role and scopes, plus a `deviceToken` when one is issued | | `policy` | limits such as `maxPayload` and the tick interval | The `auth` block is worth reading: it reports what this socket may actually do, which can be less than what the client asked for. ## After the handshake - **Requests** are `{type: "req", id, method, params}` and get `{type: "res", id, ok, payload | error}` back. - **Events** arrive as `{type: "event", event, payload, seq?, stateVersion?}`, for example `presence`, `tick`, `agent` and `chat`. - **Side-effecting methods** such as `send` and `agent` require an idempotency key so a retry is safe; the Gateway keeps a short-lived dedupe cache. - **Events are not replayed.** A client that sees a gap must refresh its state, and the transport rules say to retire the connection and reconnect before applying more live deltas. - **Agent requests** answer twice: an immediate acknowledgement with a `runId` and status `accepted`, streamed `agent` events, then a final response with the outcome. ## Why connect must come first The architecture page lists it as an invariant: **any non-JSON or non-connect first frame is a hard close.** The reasons follow from what the Gateway controls: - It cannot authorise a method before it knows the caller's role and scopes, and the same port serves operators, nodes and workers with very different powers. - Device identity and pairing are decided inside `connect`, so a new, unapproved device can be stopped before it touches anything. - Version negotiation has to happen before either side interprets a payload. - Pre-connect frames are capped at 64 KiB and unauthenticated sockets per client address are budgeted, which keeps an unauthenticated peer cheap to refuse. A quick way to see the handshake succeed without writing a client is `openclaw gateway status --require-rpc`, which proves read-scope RPC works rather than only that the port answers.
- What should an OpenClaw client do when connect fails with UNAVAILABLE and reason startup-sidecars?Retry. The Gateway is still finishing startup work and returns a retryable `UNAVAILABLE` with `details.reason: "startup-sidecars"` and a `retryAfterMs` hint. A client should wait that long and reconnect within its own connection budget instead of treating it as a final handshake failure.
- Why does the hello-ok auth block matter even when connect succeeded?It reports the role and scopes this socket was actually granted, which can be narrower than what the client requested, and it carries a `deviceToken` when one is issued for later connects. A client that assumes its requested scopes were granted will hit authorisation errors on methods such as `config.*`, which need `operator.admin`.
- A client can call health first and send connect when it needs write access.
- The Gateway replays missed events after a reconnect, so gaps fix themselves.
- The features list in hello-ok is a complete dump of every callable route.
- Node caps and commands are grants the Gateway accepts as declared.
- Retrying a send without an idempotency key is safe because the Gateway dedupes by content.
Assumes OpenClaw 2026.9.9, whose Gateway speaks WebSocket protocol version 4; the handshake shape is stable but the version number moves with protocol changes.