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

# 即時資料

> 訂閱 DexBuilder Prediction Markets 即時資料流。

DexBuilder 透過 WebSocket Broker 提供 Prediction Markets 即時資料。公開行情頻道不需要認證。

## 連接地址

Prediction Markets：

```text theme={null}
wss://broker.dexbuilder.com/ws/dex-builder/event-contracts
```

Event Contracts 使用獨立產品連接：

```text theme={null}
wss://broker.dexbuilder.com/ws/dex-builder/event-contracts
```

以下示例使用 Prediction Markets 的 `pred` 前綴；Event Contracts 使用對應的 `ec` 前綴。

## 訂閱公開頻道

訂閱請求包含請求時間戳 `t`、客戶端 `id`、操作、頻道和頻道對應的訂閱項。

```json theme={null}
{
  "t": 1720000000123,
  "id": "sub-1",
  "op": "subscribe",
  "ch": "ec.bbo",
  "payload": {
    "items": [
      {
        "token_id": "ge_token-yes"
      }
    ]
  }
}
```

服務端會先返回確認消息，然後推送頻道資料。公開頻道必須使用 `items`；`all: true` 僅用於私有頻道。

## 公開頻道

| 頻道 | 訂閱項 | 用途 |
| - | - | - |
| `ec.trades` | `condition_id` | 撮合成交 |
| `ec.depth` | `token_id`、`level` | 完整訂單簿快照 |
| `ec.bbo` | `token_id` | 最佳買賣價與最近成交價 |
| `ec.status` | `market_id` | 市場狀態和結果價格 |
| `ec.cpi` | `symbol`、`interval` | 參考價格更新 |
| `ec.candlestick` | `symbol`、`interval` | K 線更新 |
| `ec.oc_price` | `slug` 或 `market_id` | 目標價與最終價格更新 |

## 返回消息結構

```json theme={null}
{
  "t": 1720000000124,
  "trace_id": "trace-1",
  "type": "update",
  "ch": "ec.bbo",
  "err": null,
  "result": {
    "condition_id": "ge_condition-1",
    "token_id": "ge_token-yes",
    "bid_px": "0.4400",
    "ask_px": "0.4600",
    "last_px": "0.4500",
    "ts": 1720000000123
  }
}
```

帶快取狀態的頻道可能先發送 `type: "snapshot"`，之後發送 `update`。訂單簿快照包含完整的 `bids` 和 `asks` 陣列，不是增量更新。

## CPI 示例

訂閱加密資產參考價格：

```json theme={null}
{
  "t": 1720000000123,
  "id": "cpi-1",
  "op": "subscribe",
  "ch": "ec.cpi",
  "payload": {
    "items": [
      {
        "symbol": "btcusdt",
        "interval": "1m"
      }
    ]
  }
}
```

更新消息示例：

```json theme={null}
{
  "t": 1720000000124,
  "trace_id": "trace-1",
  "type": "update",
  "ch": "ec.cpi",
  "err": null,
  "result": {
    "symbol": "btcusdt",
    "interval": "1m",
    "price": "100000.10",
    "price_timestamp": "1720000000123",
    "ts": 1720000000123
  }
}
```

## 連接恢復

連接中斷後：

1. 使用退避策略重新連接；
2. 恢復公開頻道訂閱；
3. 使用 `/api/v4/event_contracts/markets` 和 `/api/v4/event_contracts/tokens/{token_id}/ticker` 等 REST 介面刷新權威狀態；
4. 使用頻道鍵和時間戳對重復消息去重。

完整協議請參閱「WebSocket 協議」「公共頻道」和「WebSocket 認證」。


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