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

# WebSocket 快速開始

> 快速連接到 Polymarket WebSocket

<Callout type="success">
  還沒有帳號？[點擊這裡註冊 Polymarket](https://polymarket.com/?r=2026VIP) 並完成入金，才能用錢包私鑰為訂單籤名、實際成交。
</Callout>

<Warning>
  **法律合規提醒**：在使用 Polymarket 服務或 API 前，請確認您所在地區的法律規定。Polymarket 目前不支援比利時、法國、新加坡、泰國、中國大陸等地區，政策可能隨時變化。
</Warning>

快速開始使用 WebSocket 獲取實時數據。

## Python 示例

```python theme={null}
import websocket
import json

def on_message(ws, message):
    data = json.loads(message)
    print(f"收到消息: {data}")

def on_open(ws):
    # 訂閱市場
    subscribe_msg = {
        "type": "subscribe",
        "channel": "market",
        "market_id": "YOUR_MARKET_ID"
    }
    ws.send(json.dumps(subscribe_msg))

ws = websocket.WebSocketApp(
    "wss://ws-subscriptions-clob.polymarket.com/ws/",
    on_message=on_message,
    on_open=on_open
)

ws.run_forever()
```

## TypeScript 示例

```typescript theme={null}
import WebSocket from 'ws';

const ws = new WebSocket('wss://ws-subscriptions-clob.polymarket.com/ws/');

ws.on('open', () => {
  // 訂閱市場
  ws.send(JSON.stringify({
    type: 'subscribe',
    channel: 'market',
    market_id: 'YOUR_MARKET_ID'
  }));
});

ws.on('message', (data) => {
  const message = JSON.parse(data.toString());
  console.log('收到消息:', message);
});
```

## 下一步

<CardGroup cols={2}>
  <Card title="認證" icon="key" href="/api-tutorial/websocket/authentication">
    學習 WebSocket 認證
  </Card>

  <Card title="用戶頻道" icon="user" href="/api-tutorial/websocket/user-channel">
    訂閱用戶更新
  </Card>
</CardGroup>
