Skip to main content

Streaming History Read

Overview​

When the IDE loads historical data for a trend chart, loading ten thousand points into std::vector all at once can make the process lag and then OOM. ReadHistoryStream uses a callback model — one synchronous callback per record read, and the business side handles one record at a time; when the entire batch of results is needed, use ReadHistoryStreamAsync to get a std::future. Both are free functions in the darra::opcua namespace; the first parameter is Session& — they are not member methods of Session.

Corresponding section of the standard: OPC UA Part 11(Historical Access) §6.5.

API​

FunctionCategoryReading and WritingNote
ReadHistoryStreamHistoryReadCallback-based streaming read; a callback is triggered synchronously for each record
ReadHistoryStreamAsyncHistoryReadBatch asynchronous read, return std::future<std::vector<DataValue>>
namespace darra::opcua {

// Callback-based. on_value For each item, synchronous DataValue triggering (read thread). Returns the overall status; the status of each individual item is determined by dv.IsGood() self-checking.
Status ReadHistoryStream(
Session& s,
std::string const& nid,
int64_t start_ft, int64_t end_ft,
std::function<void(DataValue const&)> on_value,
uint32_t batch_size = 1000,
std::shared_ptr<std::atomic<bool>> cancel = {});

// Asynchronous batch processing. Retrieve everything at once using `future.get()`.
std::future<std::vector<DataValue>> ReadHistoryStreamAsync(
Session& s,
std::string const& nid,
int64_t start_ft, int64_t end_ft,
uint32_t batch_size = 1000);
}
ParametersDefaultNote
s—Connected Session
nid—Historical Milestones NodeId String
start_ft—Since (FileTime, UTC 100ns since 1601)
end_ft—Range stop; Returns immediately <= start_ft when triggered Status::Good; no callback
on_value—void(DataValue const&) Callback; Pass an empty function object to return Status::DarraInvalidArgument
batch_size1000What is the maximum number of points you can earn at one time? (Hint: See "Implementation Limitations")
cancelEmptystd::shared_ptr<std::atomic<bool>>; Set to true to stop the callback

Return Value: ReadHistoryStream Returns Status (Status::Good / Status::DarraInvalidArgument), ReadHistoryStreamAsync Returns std::future<std::vector<DataValue>>. Exceptions thrown in the callback are swallowed one by one, and the process continues to the next item; the business logic must handle this on its own dv.IsGood().

Code Example​

#include <darra/opcua/client.hpp>
#include <darra/opcua/history_stream.hpp>
#include <iostream>

using namespace darra::opcua;
using namespace std::chrono;

Session s("opc.tcp://localhost:4840");
s.Connect();

// Time uses FileTime (UTC 100ns since 1601-01-01)
constexpr int64_t WIN_EPOCH_OFFSET = 116444736000000000LL;
auto now_ns = duration_cast<nanoseconds>(system_clock::now().time_since_epoch()).count();
int64_t t1 = WIN_EPOCH_OFFSET + now_ns / 100;
int64_t t0 = t1 - 24LL * 3600 * 10000000LL; // 24 hours ago

// 1) Render as you go
ReadHistoryStream(s, "ns=2;s=Temp", t0, t1,
[&](DataValue const& dv) {
double d = 0;
if (dv.Value().TryGetDouble(d))
chartSeries->AddPoint(dv.SourceTimestamp(), d);
},
/*batch_size=*/1000);

// 2) Early exit — Do not process the "cancel" flag; stop after checking each item in the read loop one by one.
auto cancel = std::make_shared<std::atomic<bool>>(false);
stopButton->OnClicked = [cancel] { cancel->store(true); };

ReadHistoryStream(s, "ns=2;s=Temp", t0, t1,
[&](DataValue const& dv) { Process(dv); },
/*batch_size=*/1000, cancel);

// 3) Counting bad values
size_t bad = 0;
ReadHistoryStream(s, "ns=2;s=Temp", t0, t1,
[&](DataValue const& dv) {
if (!dv.IsGood()) ++bad;
},
/*batch_size=*/1000);
std::cout << "24h 内 " << bad << " 个坏值\n";

// 4) Use the "future" version when you need the entire batch
auto fut = ReadHistoryStreamAsync(s, "ns=2;s=Temp", t0, t1);
auto values = fut.get(); // Block and Take It All

Implementation Constraints​

  • Currently, Session::ReadHistory no points are exposed ContinuationPoint; a single call RPC returns all points within the interval (subject to the batch_size limit).
  • Business units in large regions can slice their own data
  • batch_size It's just a hint about "the maximum number of points a caller can retrieve at a time"; a single call still returns all points.
  • Early exit = Set cancel to true; the loop stops and returns after the callback is executed Status::Good; the callback signature is void; there is no such thing as "returning false to exit."

Best Practices​

  • The callback runs on the reading threadSynchronize Execution: Long-running operations in callbacks can slow down the entire read cycle—delegate these tasks to worker threads or a queue.
  • For time intervals greater than 1 hour, it is recommended to use outer slices.
  • UI Render Throttling: Refresh after 100 entries have accumulated

Cross-Language Comparison​

C#PythonJavaC++RustC
ReadHistoryStreamAsyncread_history_streamreadHistoryStreamReadHistoryStreamread_history_streamDarraUa_HistoryStream_New/_Next/_IsEnd/_Free

Next Step​