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

# WebSocket Streaming

> Real-time market data via WebSocket.

## Connection

```
wss://api.arbiterapi.com/v1/ws?api_key=YOUR_API_KEY
```

## Subscribing

Send a JSON message to subscribe to a channel for a specific market:

```json theme={null}
{
  "subscribe": "trades",
  "market_id": "poly_0x24fb..."
}
```

### Available Channels

| Channel     | Data                      | Example                      |
| ----------- | ------------------------- | ---------------------------- |
| `trades`    | Real-time trade execution | Price, size, side, timestamp |
| `prices`    | Price changes             | Latest price per market      |
| `orderbook` | Orderbook updates         | Bids/asks deltas             |

### Wildcard Subscription

Subscribe to all markets on a channel:

```json theme={null}
{
  "subscribe": "trades",
  "market_id": "*"
}
```

## Message Format

### Trade Event

```json theme={null}
{
  "type": "trade",
  "market_id": "poly_0x24fb...",
  "platform": "polymarket",
  "price": 0.55,
  "size": 100.0,
  "side": "buy",
  "timestamp_ms": 1774399231269
}
```

### Price Event

```json theme={null}
{
  "type": "price",
  "market_id": "poly_0x24fb...",
  "platform": "polymarket",
  "price": 0.56,
  "timestamp_ms": 1774399231500
}
```

## Unsubscribing

```json theme={null}
{
  "unsubscribe": "trades",
  "market_id": "poly_0x24fb..."
}
```

## Example: Node.js

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

const ws = new WebSocket('wss://api.arbiterapi.com/v1/ws?api_key=YOUR_KEY');

ws.on('open', () => {
  ws.send(JSON.stringify({
    subscribe: 'trades',
    market_id: '*'
  }));
});

ws.on('message', (data) => {
  const event = JSON.parse(data.toString());
  console.log(`${event.market_id}: ${event.price} (${event.side})`);
});
```

## Example: Python

```python theme={null}
import asyncio
import websockets
import json

async def stream():
    uri = "wss://api.arbiterapi.com/v1/ws?api_key=YOUR_KEY"
    async with websockets.connect(uri) as ws:
        await ws.send(json.dumps({
            "subscribe": "trades",
            "market_id": "*"
        }))
        async for msg in ws:
            event = json.loads(msg)
            print(f"{event['market_id']}: {event['price']} ({event['side']})")

asyncio.run(stream())
```
