> ## 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/prediction
```

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": "pred.bbo",
  "payload": {
    "items": [
      {
        "token_id": "ge_token-yes"
      }
    ]
  }
}
```

服务端会先返回确认消息，然后推送频道数据。公开频道必须使用 `items`；`all: true` 仅用于私有频道。

## 公开频道

| 频道 | 订阅项 | 用途 |
| - | - | - |
| `pred.trades` | `condition_id` | 撮合成交 |
| `pred.depth` | `token_id`、`level` | 完整订单簿快照 |
| `pred.bbo` | `token_id` | 最佳买卖价与最近成交价 |
| `pred.status` | `market_id` | 市场状态和结果价格 |
| `pred.cpi` | `symbol`、`interval` | 参考价格更新 |
| `pred.candlestick` | `symbol`、`interval` | K 线更新 |
| `pred.oc_price` | `slug` 或 `market_id` | 目标价与最终价格更新 |

## 返回消息结构

```json theme={null}
{
  "t": 1720000000124,
  "trace_id": "trace-1",
  "type": "update",
  "ch": "pred.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": "pred.cpi",
  "payload": {
    "items": [
      {
        "symbol": "btcusdt",
        "interval": "1m"
      }
    ]
  }
}
```

更新消息示例：

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

## 连接恢复

连接中断后：

1. 使用退避策略重新连接；
2. 恢复公开频道订阅；
3. 使用 `/api/v4/prediction/markets` 和 `/api/v4/prediction/tokens/{token_id}/ticker` 等 REST 接口刷新权威状态；
4. 使用频道键和时间戳对重复消息去重。

完整协议请参阅「WebSocket 协议」「公共频道」和「WebSocket 认证」。


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