> For the complete documentation index, see [llms.txt](https://docs.flash.trade/flash-trade/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.flash.trade/flash-trade/flash-trade-protocol/build-on-flash/flash-trade-v2/reference/websocket.md).

# WebSocket

`GET /owner/{owner}/ws`

Upgrades to a WebSocket that streams the owner's basket state. Task walkthrough: [Stream live state](/flash-trade/flash-trade-protocol/build-on-flash/flash-trade-v2/guides/stream-live-state.md).

```
wss://flashapi.trade/owner/{owner}/ws?updateIntervalMs=1000
```

| Param              | In    | Description                                                      |
| ------------------ | ----- | ---------------------------------------------------------------- |
| `owner`            | path  | Owner wallet pubkey (base58)                                     |
| `updateIntervalMs` | query | Metric-tick cadence, ms (default `1000`, min `100`, max `10000`) |

## Message protocol

Two JSON message types.

### `basket` — full snapshot

Sent once on connect, then again on every basket account update (gRPC-driven). Same shape as [`GET /owner/{owner}`](/flash-trade/flash-trade-protocol/build-on-flash/flash-trade-v2/reference/account-and-positions.md).

```json
{ "type": "basket", "data": {
  "owner": "…", "basketPubkey": "…", "basketData": "base64…",
  "positionMetrics": { "<marketPubkey>": { /* PositionMetricsDto */ } },
  "orderMetrics": { "<marketPubkey>": { /* OrderMetricsDto */ } }
} }
```

### `metrics` — metrics-only tick

Sent on each oracle tick (at `updateIntervalMs`) so PnL/leverage/liq refresh without re-shipping the basket bytes.

```json
{ "type": "metrics", "data": { "<marketPubkey>": { /* PositionMetricsDto */ } } }
```

## Connection behavior

* The first message is always a full `basket`, so you have bytes before any tick.
* `basket` re-sends whenever the basket account changes on-chain.
* `metrics` sends on the `updateIntervalMs` cadence.
* The server pings every 30s and closes if no pong arrives within 10s.
* The client doesn't send anything after connecting.

## Limits

| Limit                 | Value  | On breach |
| --------------------- | ------ | --------- |
| Per-owner connections | 5      | `429`     |
| Global connections    | 10,000 | `503`     |

## DTOs <a href="#dtos" id="dtos"></a>

### `PositionMetricsDto`

UI-ready strings plus raw native values.

| Field                                                         | Description                                 |
| ------------------------------------------------------------- | ------------------------------------------- |
| `marketSymbol`, `collateralSymbol`, `sideUi`                  | Market, collateral, `"Long"`/`"Short"`      |
| `entryPriceUi`, `liquidationPriceUi`, `leverageUi`            | UI-formatted prices / leverage              |
| `sizeAmountUi`, `sizeAmountUiKmb`, `sizeUsdUi`                | Size in token / abbreviated / USD           |
| `collateralAmountUi`, `collateralUsdUi`                       | Collateral in token / USD                   |
| `pnlWithFeeUsdUi`, `pnlPercentageWithFee`                     | Signed PnL net of fees, and %               |
| `pnlWithoutFeeUsdUi`, `pnlPercentageWithoutFee`               | Signed PnL before fees, and %               |
| `profitUsd`, `lossUsd`, `marginUsd`                           | Raw USD values                              |
| `exitFeeUsd`, `borrowFeeUsd`, `priceImpactUsd`, `totalFeeUsd` | Raw fee components                          |
| `leverage`                                                    | Raw leverage in BPS (divide by `BPS_POWER`) |
| `liquidationPrice`, `exitPrice`                               | `{ price, exponent, … }` objects            |

### `OrderMetricsDto`

| Field                                | Description                |
| ------------------------------------ | -------------------------- |
| `marketSymbol`, `sideUi`             | Market, `"Long"`/`"Short"` |
| `limitOrders`                        | `LimitOrderMetricsDto[]`   |
| `takeProfitOrders`, `stopLossOrders` | `TriggerOrderMetricsDto[]` |

Each limit order carries reserve/size/collateral amounts (UI + USD), entry & liquidation prices, leverage, and attached TP/SL. Each trigger order carries `orderId`, `type` (`"TP"`/`"SL"`), `triggerPriceUi`, size, and leverage.


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://docs.flash.trade/flash-trade/flash-trade-protocol/build-on-flash/flash-trade-v2/reference/websocket.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
