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

# TypeScript — Orderbook

## Orderbook Streaming

The SDK includes a dedicated orderbook client for real-time book data from `ob.polynode.dev`. This is a separate WebSocket connection from the event stream, with its own URL, protocol, and message format.

### Subscribe

```typescript theme={null}
// Lazy-initialized, connects on first subscribe
await pn.orderbook.subscribe(['token_id_1', 'token_id_2']);
```

### Event Handlers

```typescript theme={null}
pn.orderbook.on('snapshot', (snap) => {
  // Full book snapshot on subscribe
  console.log(snap.asset_id, snap.bids.length, 'bids', snap.asks.length, 'asks');
});

pn.orderbook.on('update', (delta) => {
  // Incremental delta. A level with size "0" means removal.
  console.log(delta.asset_id, delta.bids.length, 'bid changes');
});

pn.orderbook.on('price', (change) => {
  // Summary price movement across assets in a market
  for (const asset of change.assets) {
    console.log(asset.outcome, asset.price);
  }
});

pn.orderbook.on('snapshots_done', (msg) => {
  console.log(`All ${msg.total} snapshots received`);
});

// Catch-all for snapshot, update, and price events
pn.orderbook.on('*', (update) => {
  console.log(update.type, update);
});
```

### LocalOrderbook Helper

Maintain a sorted local copy of the book:

```typescript theme={null}
import { LocalOrderbook } from 'polynode-sdk';

const book = new LocalOrderbook();

pn.orderbook.on('snapshot', (snap) => book.applySnapshot(snap));
pn.orderbook.on('update', (delta) => book.applyUpdate(delta));

// Query state
const fullBook = book.getBook(tokenId);   // { bids, asks }
const bestBid = book.getBestBid(tokenId);  // { price, size }
const bestAsk = book.getBestAsk(tokenId);  // { price, size }
const spread = book.getSpread(tokenId);    // number
```

### Compression

Zlib compression is enabled by default (\~50% bandwidth savings). No configuration needed.

```typescript theme={null}
// To disable (not recommended):
const ob = pn.configureOrderbook({ compress: false });
```

### Configuration

```typescript theme={null}
const pn = new PolyNode({
  apiKey: 'pn_live_...',
  obUrl: 'wss://ob.polynode.dev/ws',  // default
});

// Or configure options separately
pn.configureOrderbook({
  compress: true,
  autoReconnect: true,
  maxReconnectAttempts: Infinity,
  reconnectBaseDelay: 1000,
  reconnectMaxDelay: 30000,
});
```

### System Events

```typescript theme={null}
pn.orderbook.onConnect(() => console.log('connected'));
pn.orderbook.onDisconnect((reason) => console.log('disconnected:', reason));
pn.orderbook.onReconnect((attempt) => console.log('reconnected, attempt', attempt));
pn.orderbook.onError((err) => console.error(err));
```

### Cleanup

```typescript theme={null}
pn.orderbook.unsubscribe();   // unsubscribe from all markets
pn.orderbook.disconnect();     // close connection
```
