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

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


### Live.add_callback

Add a callback to the live client. This callback must take a single record argument.
Refer to the [What's a schema](https://databento.com/docs/schemas/whats-a-schema.md) article for documentation on the fields contained with each record type.

A callback will receive error messages from the gateway with the `ErrorMsg` record type.
This indicates a problem occurred with the session.
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_callback(
    record_callback: Callable[[DBNRecord], None],
    exception_callback: Callable[[Exception], None] | None = None,
) -> None
```

#### Parameters

| Parameter | Type | Required | Description |
| `record_callback` | Callable[[DBNRecord], None] | required | A callback to dispatch records to. The callback must take a single record argument. If no exception callback is provided, any exceptions encountered will be logged and raised as warnings for visibility. |
| `exception_callback` | Callable[[Exception], None] | optional | A callback to dispatch exceptions to that are raised when executing `record_callback`. The callback must take a single exception argument. |

#### Example usage

```python
import databento as db

# Create a callback to handle DBN records
def user_callback(record: db.DBNRecord) -> None:
    print(f"callback run for {record}")

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

# Create a live client
client = db.Live(key="$YOUR_API_KEY")

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

# Add the user_callback to the client
client.add_callback(
    record_callback=user_callback,
    exception_callback=error_handler,  # optional error handler
)
```
