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.
Parameters
| Parameter | Type | Description |
|---|---|---|
metadata_callback | std::function<void(Metadata&&> | A callback for receiving DBN Metadata associated with the subscriptions. This callback will be called exactly once, before the first time record_callback is called. |
record_callback | std::function<KeepGoing(const Record&> | A callback for receiving the Records 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<ExceptionAction(const std::exception&> | A callback for when an exception is thrown in the spawned thread. Resubscribe and return ExceptionAction::Restart to start 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 or the struct definitions in
<databento/record.hpp>.
API method
void Start(RecordCallback record_callback);
void Start(MetadataCallback metadata_callback,
RecordCallback record_callback);
void Start(MetadataCallback metadata_callback,
RecordCallback record_callback,
ExceptionCallback exception_callback); Example usage
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
#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
1 2 3 4 5 6 7 8 9 10 11 12 13
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
}