Available languages: [Python](https://databento.com/docs/api-live/client/add-reconnect-callback.python.md) · [C++](https://databento.com/docs/api-live/client/add-reconnect-callback.cpp.md) · [Rust](https://databento.com/docs/api-live/client/add-reconnect-callback.rust.md) · [Raw](https://databento.com/docs/api-live/client/add-reconnect-callback.raw.md)

Source: https://databento.com/docs/api-live/client/add-reconnect-callback


### Live.add_reconnect_callback

Add a reconnect callback to the live client. This callback must take two arguments.
When a reconnection policy is set on the `Live` client, this callback will be executed when a reconnection occurs with two arguments:
- The last received record's `ts_event` timestamp, or `start` timestamp from the session `Metadata` if no records were received yet.
- The `start` timestamp from the reconnected session's `Metadata`.

These values can be used to record any gaps in the received data due to a disconnection.
Exceptions raised in a callback can be handled explicitly by specifying an `exception_callback`.

The callbacks are executed in the order they are added.

> **Info**
> It is recommended that callback functions be non-blocking to ensure the networking event loop remains running.

#### API method

```python
Live.add_reconnect_callback(
    reconnect_callback: Callable[[pd.Timestamp, pd.Timestamp], None]
    exception_callback: Callable[[Exception], None] | None = None,
) -> None
```

#### Parameters

| Parameter | Type | Required | Description |
| `reconnect_callback` | Callable[[pd.Timestamp, pd.Timestamp], None] | required | A callback for handling reconnections. The callback must take two arguments. |
| `exception_callback` | Callable[[Exception], None] | optional | A callback to dispatch exceptions to that are raised when executing `reconnect_callback`. The callback must take a single exception argument. |

#### Example usage

```python
import databento as db

# Create a callback to handle reconnections
def reconnect_callback(start, end) -> None:
    print(f"reconnection gap from {start} to {end}")

# Create a callback to handle exceptions from `reconnect_callback`
def error_handler(exception: Exception) -> None:
    print(f"an error occurred {exception}")

# Create a live client with a reconnect policy
client = db.Live(
    key="$YOUR_API_KEY",
    reconnect_policy="reconnect",
)

# Subscribe to the trades schema for all ES futures
client.subscribe(
    dataset="GLBX.MDP3",
    schema="trades",
    stype_in="parent",
    symbols="ES.FUT",
)

# Add the reconnect callback to the client
client.add_reconnect_callback(
    reconnect_callback=reconnect_callback,
    exception_callback=error_handler,  # optional error handler
)
```
