# L2 Book Stream

This channel streams level 2 (L2) order book. It describes the individual price levels in the book with aggregated order quantities at each level. Only top 20 levels are included.

## Update Speed

* Updates are pushed on each block, but only if at least 0.5 seconds have passed since the last update

## Subscribe Request

* Subscribe Schema

See [`WsL2BookSubscriptionParams`](https://sodex.com/documentation/rest-v1/schema#wsl2booksubscriptionparams) in [Schema](https://sodex.com/documentation/api/rest-v1/schema).

```typescript
interface SubscribeRequest {
  op: "subscribe";
  id: number | null; // Optional client originated request identifier sent as acknowledgment in the response.
  params: WsL2BookSubscriptionParams;
}
```

* Subscribe Ack Schema

See [`WsL2BookSubscriptionResult`](https://sodex.com/documentation/rest-v1/schema#wsl2booksubscriptionresult) in [Schema](https://sodex.com/documentation/api/rest-v1/schema).

```typescript
interface SubscriptionResult {
  op: "subscribe";
  id: number | null; // Optional client originated request identifier sent as acknowledgment in the response.
  result: WsL2BookSubscriptionResult | null;
  success: boolean; // Indicates if the request was successfully processed by the engine.
  connID: string;
  error: string | null; // Condition: If success is false. Error message.
  time_in: number; // The timestamp when the subscription was received on the wire, just prior to parsing data, in milliseconds.
  time_out: number; // The timestamp when the acknowledgement was sent on the wire, just prior to transmitting data, in milliseconds.
}
```

* Example: Subscribe

```json
{
  "op": "subscribe",
  "params": {
    "channel": "l2Book",
    "symbol": "WSOSO_vUSDC",
    "tickSize": "0.0001"
  }
}
```

* Example: Subscribe Ack

```json
{
  "op": "subscribe",
  "result": {
    "channel": "l2Book",
    "symbol": "WSOSO_vUSDC",
    "tickSize": "0.0001"
  },
  "success": true,
  "connID": "0xb0922dfe2b14dadb1195ea4db2b45508",
  "time_in": 1672515782130,
  "time_out": 1672515782136
}
```

## Unsubscribe Request

* Unsubscribe Schema

See [`WsL2BookSubscriptionParams`](https://sodex.com/documentation/rest-v1/schema#wsl2booksubscriptionparams) in [Schema](https://sodex.com/documentation/api/rest-v1/schema).

```typescript
interface UnsubscribeRequest {
  op: "unsubscribe";
  id: number | null; // Optional client originated request identifier sent as acknowledgment in the response.
  params: WsL2BookSubscriptionParams;
}
```

* Unsubscribe Ack Schema

See [`WsL2BookSubscriptionResult`](https://sodex.com/documentation/rest-v1/schema#wsl2booksubscriptionresult) in [Schema](https://sodex.com/documentation/api/rest-v1/schema).

```typescript
interface UnsubscriptionResult {
  op: "unsubscribe";
  id: number | null; // Optional client originated request identifier sent as acknowledgment in the response.
  result: WsL2BookSubscriptionResult | null;
  success: boolean; // Indicates if the request was successfully processed by the engine.
  connID: string;
  error: string | null; // Condition: If success is false. Error message.
  time_in: number; // The timestamp when the subscription was received on the wire, just prior to parsing data, in milliseconds.
  time_out: number; // The timestamp when the acknowledgement was sent on the wire, just prior to transmitting data, in milliseconds.
}
```

* Example: Unsubscribe

```json
{
  "op": "unsubscribe",
  "params": {
    "channel": "l2Book",
    "symbol": "WSOSO_vUSDC",
    "tickSize": "0.0001"
  }
}
```

* Example: Unsubscribe Ack

```json
{
  "op": "unsubscribe",
  "result": {
    "channel": "l2Book",
    "symbol": "WSOSO_vUSDC",
    "tickSize": "0.0001"
  },
  "success": true,
  "connID": "0xb0922dfe2b14dadb1195ea4db2b45508",
  "time_in": 1672515782130,
  "time_out": 1672515782136
}
```

## Snapshot Response

* Snapshot Schema

See [`WsDepthSnapshotData`](https://sodex.com/documentation/rest-v1/schema#wsdepthsnapshotdata) in [Schema](https://sodex.com/documentation/api/rest-v1/schema).

```typescript
interface L2BookResponse {
  channel: "l2Book";
  type: "snapshot";
  data: WsDepthSnapshotData;
}
```

* Example: Snapshot

```json
{
  "channel": "l2Book",
  "type": "snapshot",
  "data": {
    "s": "WSOSO_vUSDC",
    "u": 12345,
    "E": 1672515782136,
    "b": [
      ["0.5666", "4831.75496356"],
      ["0.5665", "4831.75496356"],
      ["0.5664", "4831.75496356"],
      ["0.5663", "4831.75496356"],
      ["0.5662", "4831.75496356"]
    ],
    "a": [
      ["0.5668", "4410.79769741"],
      ["0.5669", "4410.79769741"],
      ["0.567", "4410.79769741"],
      ["0.5671", "4410.79769741"],
      ["0.5672", "4410.79769741"]
    ]
  }
}
```
