# Live updates over WebSockets

> Follow Wagyu orders in real time over WebSockets (one socket per order, or many orders on one socket), plus public feeds of recent swaps and bridges.

WebSockets make an order screen feel instant. They speed things up but are not the record: keep the REST read as the source of truth, and read it again after every reconnect.

Every socket sends **complete snapshots, never patches**: one right after connecting, then after each change, then periodically as a safety net. Frames can repeat or be merged.

| Socket | Sends | Authentication |
| --- | --- | --- |
| `/v2/swaps/{id}/events` | Snapshots of one swap, same shape as `GET /v2/swaps/{id}` | `?verify=` or `X-Session-ID` header |
| `/v2/bridges/{id}/events` | Snapshots of one bridge order | `?verify=` or `X-Session-ID` header |
| `/v2/coinflips/{id}/events` | Snapshots of one coinflip table | `?verify=` or `X-Session-ID` header |
| `/v2/orders/events` | Snapshots of every order you subscribe to | Session inside each subscribe message |
| `/v2/recent-swaps/events` | The public list of recent swaps | None |
| `/v2/bridges/recent/events` | The public list of recent bridge orders | None |
| `/v2/coinflips/recent/events` | The public list of recent coinflip rounds | None |

All sockets live at `wss://api.wagyu.xyz`. The machine-readable contract is the AsyncAPI document at [`/v2/asyncapi`](https://api.wagyu.xyz/v2/asyncapi).

## Follow one order

A browser cannot set headers on a WebSocket, so it proves access with the order's exact destination address. This proof is read-only:

```javascript
const url = `wss://api.wagyu.xyz/v2/swaps/${orderId}/events?verify=${encodeURIComponent(destinationAddress)}`;
const ws = new WebSocket(url);
ws.onmessage = (event) => {
  const order = JSON.parse(event.data); // a full snapshot, same shape as GET /v2/swaps/{id}
  render(order.customerState);
};
ws.onclose = (event) => {
  // 4404: missing, unauthorized or nonexistent order (deliberately indistinguishable)
  // Otherwise: reconnect with backoff, then re-read the order over REST.
};
```

Clients that can set headers may send the owner's `X-Session-ID` instead. Never put a session in a URL.

## Follow many orders on one socket

`/v2/orders/events` multiplexes owned orders. After connecting, send one message per order with its session; the session travels in the message, never in the URL:

```json
{ "type": "subscribe", "orderId": "<orderId>", "sessionId": "<sessionId>" }
```

The server answers with a snapshot of that order right away, then sends snapshots as it changes. Frames are the bare order object, with no wrapper. Send `{ "type": "unsubscribe", "orderId": "<orderId>" }` when you no longer need one.

> **One bad subscription closes the socket:** Subscribing to an order that does not exist, or with the wrong session, closes the whole connection with code `4404`. So does a message that is not valid JSON. Validate before you send, and resubscribe the remaining orders after reconnecting.

## Public activity feeds

`/v2/recent-swaps/events`, `/v2/bridges/recent/events` and `/v2/coinflips/recent/events` need no authentication. Each frame is the complete current list, the same data as the matching REST route. Clients send nothing; a reconnect simply receives the next frame. Connections beyond the per-client or server capacity are closed with code `1013`.

```javascript
const ws = new WebSocket('wss://api.wagyu.xyz/v2/recent-swaps/events');
ws.onmessage = (event) => showTicker(JSON.parse(event.data)); // an array of completed swaps
```

In these lists `orderId` is an opaque key for the row, stable across frames. It is not the order's real id and opens no order.

## Close codes

| Code | Meaning | What to do |
| --- | --- | --- |
| `4404` | Missing, unauthorized or nonexistent order, a bad subscription, or an invalid message. Deliberately the same for all. | Do not use it to probe whether an order exists. Check your proof or session. |
| `1013` | Too many connections for the client or the server. | Reconnect later with backoff. |

The `4404` behaviour above was checked against the live API.

## Make it robust

- Treat every frame as a hint to refresh the REST order, and render from the REST answer. A dropped frame then never leaves a stale screen.
- Reconnect with backoff, and read the order over REST after every reconnect, browser sleep or network change.
- Keep polling as a fallback: every 30 seconds or slower until the order ends.

---
Source: https://docs.wagyu.xyz/live-updates · Built from the live Wagyu API on 2026-12-04.
