# L4 Book Stream

This subscription first sends a snapshot of the entire book and then forwards order diffs by block.

## Update Speed

* Pushed on each block.

## Subscribe Request

* Subscribe Schema

See [`WsL4BookSubscriptionParams`](https://sodex.com/documentation/rest-v1/schema#wsl4booksubscriptionparams) 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: WsL4BookSubscriptionParams;
}
```

* Subscribe Ack Schema

See [`WsL4BookSubscriptionResult`](https://sodex.com/documentation/rest-v1/schema#wsl4booksubscriptionresult) 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: WsL4BookSubscriptionResult | 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": "l4Book",
    "symbol": "BTC-USD"
  }
}
```

* Example: Subscribe Ack

```json
{
  "op": "subscribe",
  "result": {
    "channel": "l4Book",
    "symbol": "BTC-USD",
    "level": 10
  },
  "success": true,
  "connID": "0x14d927b9ee587b4f345219617e931984",
  "time_in": 1672515782130,
  "time_out": 1672515782136
}
```

## Unsubscribe Request

* Unsubscribe Schema

See [`WsL4BookSubscriptionParams`](https://sodex.com/documentation/rest-v1/schema#wsl4booksubscriptionparams) 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: WsL4BookSubscriptionParams;
}
```

* Unsubscribe Ack Schema

See [`WsL4BookSubscriptionResult`](https://sodex.com/documentation/rest-v1/schema#wsl4booksubscriptionresult) 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: WsL4BookSubscriptionResult | 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": "l4Book",
    "symbol": "BTC-USD",
    "level": 1000
  }
}
```

* Example: Unsubscribe Ack

```json
{
  "op": "unsubscribe",
  "result": {
    "channel": "l4Book",
    "symbol": "BTC-USD",
    "level": 1000
  },
  "success": true,
  "connID": "0x14d927b9ee587b4f345219617e931984",
  "time_in": 1672515782130,
  "time_out": 1672515782136
}
```

## Snapshot / Update 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 L4BookResponse {
  channel: "l4Book";
  type: "snapshot";
  data: WsDepthSnapshotData;
}
```

* Update Schema

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

```typescript
interface L4BookResponse {
  channel: "l4Book";
  type: "update";
  data: WsDepthUpdateData;
}
```

* Example: Snapshot

```json
{
  "channel": "l4Book",
  "type": "snapshot",
  "data": {
    "s": "BTC-USD",
    "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"]
    ]
  }
}
```

* Example: Update

```json
{
  "channel": "l4Book",
  "type": "update",
  "data": {
    "s": "BTC-USD",
    "u": 12345,
    "U": 12345,
    "E": 1672515782136,
    "b": [["0.5666", "4831.75496356"]],
    "a": [
      ["0.5668", "4410.79769741"],
      ["0.5669", "4410.79769741"]
    ]
  }
}
```
