Skip to main content

Subscription

darra::opcua::Subscription This is a subscription container that manages a set of MonitoredItem (MI), and pushes data via std::function callbacks DataValue. Move-only, The destructor automatically deallocates and destroys the DarraUa_Subscription_Delete server subscription (client.hpp of ~Subscription).

Subpage Redirect
  • To create a subscription, please refer to Create a Subscription.
  • Add / Add in Bulk MonitoredItem Please refer to Add MonitoredItem.
  • Modify publishingInterval / lifetime / sampling—please refer to Modify.
  • For "Disabled / Sampling / Reporting," please refer to SetMonitoringMode.
  • To request a resend for a specific SeqNo, please refer to Republish.
  • For information on data callback fields and threading models, please refer to Data Callback.
  • For OPC UA alarms and event subscriptions, see Event Subscription.

One-Minute Overview​

// Create
auto sub = s.CreateSubscription(/*publishing_interval_ms=*/500.0);

// Add MI + Callback (lambda)
auto h = sub.AddNode("ns=2;s=Counter", [](DataValue const& dv) {
std::cout << dv.Value().AsString() << "\n";
});

// Add in Bulk
std::vector<std::string> nodes;
for (int i = 1; i <= 100; ++i) nodes.push_back("ns=2;s=T" + std::to_string(i));
auto results = sub.AddMany(nodes, [](DataValue const& dv) {
std::cout << dv.Value().AsString() << "\n";
});

// Change the sampling interval MI for a single item
sub.ModifyItem(h, /*sampling_interval_ms=*/100.0);

// Pause / Resume Collection
sub.SetMode(h, MonitoringMode::Disabled);
sub.SetMode(h, MonitoringMode::Reporting);

// Remove
sub.Remove(h);

Subscriptions / Session Events​

// client.hpp ... / OnStateChange ... StartKeepAlive are the only two SDK automatic distribution points
s.Events().on_any = [](OpcUaEventEntry const& e) {
if (e.category == OpcUaEventCategory::StateChanged) { /* Session State Changes */ }
if (e.category == OpcUaEventCategory::KeepAlive) { /* Keep Alive */ }
};

C++'s SessionEvents has no subscription-level callback fields — the SDK only pushes the two categories listed above, StateChanged / KeepAlive, to on_any. Subscription-level states (lifetime expired / recreated / late) expose only the C-side entry point DarraUa_Subscription_SetStateCallback (opcua_subscribe.h ), callback to UA_SUB_STATE_GOOD / LATE / KEEP_ALIVE / LIFETIME_EXPIRED / RECREATING ; C++ Session / Subscription Currently not wrapped.

The SubscriptionCreated / MonitoredItemAdded / SubscriptionLost values in OpcUaEventCategory are only labels left for the upper layer to raise itself; the SDK does not push them automatically. To raise one, use PushEvent(...) / PushExtEvent(...) from events.hpp; the receiving side subscribes with EventBus::Instance().Subscribe(...).

For more information, see Session Events.

Next Step​