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

Source: https://databento.com/docs/api-live/client/live-threaded-start


### LiveThreaded::Start

Instructs the live gateway to start sending data.
This call spawns a thread to process the incoming records and call the given
callback.

> **Info**
> A session cannot be started more than once.

#### API method

```cpp
void Start(RecordCallback record_callback);
void Start(MetadataCallback metadata_callback,
           RecordCallback record_callback);
void Start(MetadataCallback metadata_callback,
           RecordCallback record_callback,
           ExceptionCallback exception_callback);
```

#### Parameters

| Parameter | Type | Description |
| `metadata_callback` | std::function\ | A callback for receiving [DBN](https://databento.com/docs/conventions/databento-binary-encoding.md) `Metadata` associated with the subscriptions. This callback will be called exactly once, before the first time `record_callback` is called. |
| `record_callback` | std::function\ | A callback for receiving the `Record`s for all subscriptions. The callback will be called as data is received. The particular record types will depend on the schemas of the active subscriptions. Return `KeepGoing::Continue` to continue receiving records or `KeepGoing::Stop` to end the session. |
| `exception_callback` | std::function\ | A callback for when an exception is thrown in the spawned thread. Resubscribe and return `ExceptionAction::Restart` to [start](https://databento.com/docs/api-live/client/live-threaded-start.md) a new session or `ExceptionAction::Stop` to close the connection and stop the thread. |

#### Returns

Returns data via the callbacks `metadata_callback` and `record_callback`.

> **Warning**
>
> The reference passed to `record_callback` is only valid during the callback.
> Copy the record or particular fields to persist data between calls to the callback.

A full list of fields returned through the `record_callback` for each schema is available in the
[list of fields by schema](https://databento.com/docs/schemas/whats-a-schema.md) or the struct definitions in
`<databento/record.hpp>`.

#### Example usage

```cpp
#include <atomic>
#include <databento/live.hpp>
#include <iostream>
#include <thread>

using namespace databento;

std::atomic<std::uint32_t> trade_count{};
auto client = LiveThreaded::Builder()
                  .SetKey("$YOUR_API_KEY")
                  .SetDataset("OPRA.PILLAR")
                  .SetCompression(Compression::Zstd)
                  .BuildThreaded();
client.Subscribe({"SPXW.OPT"}, Schema::Trades, SType::Parent);
auto print_metadata = [](Metadata&& metadata) {
  std::cout << "DBN metadata version "
            // cast to avoid printing version as a char
            << static_cast<std::uint16_t>(metadata.version)
            << " and dataset " << metadata.dataset << '\n';
};
auto print_record = [&trade_count](const Record& record) {
  if (auto* trade = record.GetIf<TradeMsg>()) {
    std::cout << *trade << '\n';
    ++trade_count;
    return KeepGoing::Stop;
  }
  return KeepGoing::Continue;
};
client.Start(print_metadata, print_record);
while (trade_count == 0) {
  std::this_thread::yield();
}
```
#### Example response

```plaintext
DBN metadata version 3 and dataset OPRA.PILLAR
TradeMsg {
    hd = RecordHeader { length = 12, rtype = Mbp0, publisher_id = 22, instrument_id = 1191196634, ts_event = 2024-06-03T16:22:26.845122048Z },
    price = 9.000000000,
    size = 1,
    action = Trade,
    side = None,
    flags = LAST | TOB (194),
    depth = 0,
    ts_recv = 2024-06-03T16:22:26.845330950Z,
    ts_in_delta = 0,
    sequence = 1949252493
}
```
