> ## Documentation Index
> Fetch the complete documentation index at: https://docs.runaether.dev/llms.txt
> Use this file to discover all available pages before exploring further.

# WebSocket Overview

> Open a live connection to a task's machine over a single multiplexed WebSocket

A task's machine exposes one realtime connection, its live socket: a multiplexed WebSocket that carries terminal I/O, file operations, live agent events, port changes, and git diffs over a single socket. The web app, the phone and the `aether` CLI all speak this same protocol.

## Endpoint

```
GET /live/{liveConnectionID}
```

`{liveConnectionID}` is the live connection [`POST /tasks/{taskID}/connect`](https://docs.runaether.dev/api-reference/endpoints/tasks/post-tasks-by-taskid-connect) answered. Do not build this address yourself: dial the `websocket_url` that call answers.

The ticket that call answers is the socket's only credential. For a sandbox, the API checks it, dials the task's sandbox and copies text frames both ways. Every other documented surface is REST.

## Connecting

Name the task, not the workspace. Call [`POST /tasks/{taskID}/connect`](https://docs.runaether.dev/api-reference/endpoints/tasks/post-tasks-by-taskid-connect) first. It answers a union on `state`.

**Opening a live connection never starts a sandbox unless you ask it to.** `start_sandbox` is optional and false by default, so merely viewing a task can never bring its sandbox up. Starting is an explicit act: `"start_sandbox": true`.

**1. Open a live connection**

Call `POST /tasks/{taskID}/connect` with `{"protocols": [1]}`, the live protocol versions you speak.

**2. Start a stopped sandbox, if the person chooses to**

`state: "stopped"` means the sandbox is stopped. Offer to start it, then call again with `"start_sandbox": true`, **once**, on the call that expresses that intent, and with it false on every later call of that attempt.

**3. Wait while it comes up**

`state: "starting"` carries `since`, `retry_after_ms` and `waiting` (its `code` and a `message` to show). Call again after `retry_after_ms`, and give up by your own deadline, counted from `since`.

**4. Open the socket**

`state: "ready"` carries `websocket_url`, a `ticket`, `ticket_expires_at`, the `protocol` chosen, and `preview_url_template`. Dial `websocket_url` within 30 seconds, offering the ticket in the subprotocol header (see below). A ticket opens one socket once: every attempt to open a socket starts with a fresh call.

### When it refuses

A refusal carries a `code`. The task's `can.connect_live` shows the refusals that come from the task and its sandbox before you call: read it from [`GET /tasks/{taskID}`](https://docs.runaether.dev/api-reference/endpoints/tasks/get-tasks-by-taskid) to show the reason without trying. Only the call can answer `live_protocol_unsupported`, a full computer's `too_many_live_connections`, and a start's runtime admission (`402`), so handle those on the call itself.

| Status and `code` | Meaning |
| - | - |
| `409 task_start_pending`, `409 task_start_failed` | The task's start is pending, or failed. |
| `409 sandbox_deleted`, `409 sandbox_failed`, `409 sandbox_needs_message` | The sandbox is gone, failed, or has no machine until the task's next message brings it back. |
| `409 task_creator_deleted` | The task's creator was deleted, so only a re-created task can start. |
| `409 live_protocol_unsupported` | No live protocol in common: a sandbox speaks protocol 1. |
| `402` | Runtime admission refused the start `start_sandbox` asked for. |
| `403 control_forbidden`, `403 org_role_insufficient` | The caller may not connect to this task. |

The `ready` response shape:

```json theme={"dark"}
{
  "state": "ready",
  "live_connection_id": "9b2c...",
  "websocket_url": "wss://api.runaether.dev/live/9b2c...",
  "ticket": "live.v1.9b2c....client.1763300000.4f1a...",
  "ticket_expires_at": "2026-11-16T12:00:30Z",
  "protocol": 1,
  "frame_key": null,
  "preview_url_template": "https://{port}-abc12345-ab12cd34ef56ab12cd34ef56ab12cd34.preview.runaether.dev"
}
```

`preview_url_template` is the address of a port on the machine, with `{port}` to fill in. See [preview URLs](https://docs.runaether.dev/api-reference/websocket/ports.md#preview-urls).

## Authentication

The socket takes the ticket alone. Offer two WebSocket subprotocols, `bearer` and then the ticket; the server selects `bearer`. Never put the ticket in the address.

```javascript theme={"dark"}
const ws = new WebSocket(ready.websocket_url, ["bearer", ready.ticket]);
```

```bash theme={"dark"}
wscat -c "$WEBSOCKET_URL" -s bearer -s "$TICKET"
```

Your own session token or API key opens no socket, in a header or in the address: authenticate `POST /tasks/{taskID}/connect` with it, and dial with the ticket that call answers. A ticket that is malformed, expired, already used or for another connection is refused with `401` and no body before the upgrade.

## Message Format

Every message is JSON with a `channel` field identifying the target subsystem and a `type` field identifying the message within that channel. Six channels are multiplexed over the connection:

| Channel | Direction | Purpose |
| - | - | - |
| [`terminal`](https://docs.runaether.dev/api-reference/websocket/terminal) | both | PTY terminal sessions |
| [`files`](https://docs.runaether.dev/api-reference/websocket/files) | both | File operations and change notifications |
| [`agent`](https://docs.runaether.dev/api-reference/websocket/agent) | both | Subscribe to a task's live event stream |
| [`ports`](https://docs.runaether.dev/api-reference/websocket/ports) | both | Open-port snapshot/changes and process kill |
| [`git`](https://docs.runaether.dev/api-reference/websocket/git) | both | Workspace diff, checkpoints, restore, revert |
| `error` | server only | Out-of-band workspace errors |

Client messages are validated against strict schemas — extra or misnamed fields are rejected. The canonical shapes live in `packages/workspace-protocol/src/messages.ts`.

```json theme={"dark"}
{
  "channel": "terminal",
  "type": "input",
  "sessionId": "cli-123",
  "data": "ls -la\n"
}
```

## The `error` Channel

The server pushes errors on a dedicated channel that you never send to. It is emitted after the socket opens when the VM fails to initialize or an inbound message fails to parse.

```json theme={"dark"}
{
  "channel": "error",
  "type": "error",
  "error": "Invalid workspace message: ..."
}
```

Every close carries a code and its name as the reason. When the API cannot reach the sandbox, the socket closes with `4410 machine_left`; when access to the task was taken away, with `4403 access_ended`. Either way, open a new live connection with a fresh call to `POST /tasks/{taskID}/connect`, which says what changed.

## Connection Keepalive

| Parameter | Value |
| - | - |
| Ping interval | 15 seconds |
| Pong timeout | 120 seconds |
| Write timeout | 30 seconds |
| Max message size | 1 MB |

The API sends WebSocket ping frames every 15 seconds. If no pong arrives within 120 seconds, it closes the connection.

## Reconnection

On reconnect, what comes back depends on the channel:

* **Terminal** sessions keep running on the VM while you're disconnected; reattach by sending `create` with the same `sessionId`.
* **Ports** sends a fresh `snapshot` of currently-open ports on connect.
* **Agent** resumes only live `task_event` messages for tasks you re-`subscribe` to. Conversation history is **not** replayed over the socket — fetch [turn summaries](https://docs.runaether.dev/api-reference/endpoints/tasks/get-tasks-by-taskid-conversation-turns) and per-turn activity over REST.
* **Files** does not replay change events that occurred while disconnected; re-`list` or `read` to get current state.

> [!NOTE]
>
> Prompting an agent, responding to its questions, and stopping a task all happen over REST, not the WebSocket. See the [agent channel](https://docs.runaether.dev/api-reference/websocket/agent.md).


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.