Skip to main content

Subscription Resumes Automatically

Overview​

After a session reconnection, the server-side subscription may still be active, or it may have been cleaned up.There is no "automatic subscription renewal" feature on the C++ side. — When reconnecting, each layer API handles a segment:

  1. On reconnect: TransferSubscriptions tries to re-attach the server-side subscriptions to the new session; if that fails the local subscription table is cleared and the business layer recreates it. The SDK does not re-send CreateMonitoredItemsRequest by itself — when the server does not support TransferSubscriptions you have to rebuild.
  2. API surface: exactly one manual method, Session::TransferSubscriptions — no caching, no rebuilding, no subscription-level events.

Therefore, the business side must store the configuration MonitoredItem itself and rebuild it as needed.

API​

NameCategoryReading and WritingNote
Session::TransferSubscriptions(subscription_ids, send_initial_values = true)MethodsWriteManually Transfer a Subscription, Returns std::vector<Status>
Subscription::Handle()MethodsReadHere you go DarraUa_SubscriptionHandle—it can handle Layer C state callbacks.
SessionEvents::on_reconnecting / on_reconnected / on_disconnectedEventReadSession-level (field assignment),No subscription-level events
namespace darra::opcua {

/// Transfer the external ( Subscription Id server-side) list to the current session.
/// Returns a list of the Status values for each subscription, in the same subscription_ids order as .
std::vector<Status> TransferSubscriptions(
std::vector<uint32_t> const& subscription_ids,
bool send_initial_values = true);

DarraUa_SubscriptionHandle Subscription::Handle() const;
}
ParametersDefaultNote
subscription_ids—List of Subscription IDs (uint32_t) from the server's perspective
send_initial_valuestrueAfter the transfer is successful, should the server resend the current value once?

Return Value: std::vector<Status>, in the same subscription_ids order as. A failure at the transmission level throws Exception("TransferSubscriptions failed", rc), and individual failures are added to the returned vector.

C++ does not have OnSubscriptionLost or OnSubscriptionRestored

SessionEvents has only the 8 session-level fields (on_connected / on_disconnected / on_reconnecting / on_reconnected / on_keepalive / on_state_changed / on_communication_error / on_any), no subscription-level callback, and no type such as SubscriptionLostEventArgs either. Only C# has Events.SubscriptionLost / Events.SubscriptionRestored.

In C++ you can only observe subscription-level state by attaching DarraUa_Subscription_SetStateCallback to sub.Handle(); the callback takes (DarraUa_SubscriptionHandle, DarraUa_SubscriptionState, DarraUa_Status, void*) and the state enum is UA_SUB_STATE_GOOD / UA_SUB_STATE_LATE / UA_SUB_STATE_KEEP_ALIVE / UA_SUB_STATE_LIFETIME_EXPIRED / UA_SUB_STATE_RECREATING.

C++ No Caching MonitoredItem Configuration

Subscription records only MonitoredItemHandle → NodeId 字符串; it does not store AttributeId / SamplingInterval / QueueSize / DiscardOldest / DataChangeFilter / EventFilter / Triggering Links. There is no client_handle concept either — only the server-supplied DarraUa_MonitoredItemHandle.

To ensure that "the system is restored to its original state after a disconnection," the business side must store the configuration itself.

Code Example​

#include <darra/opcua/client.hpp>
#include <cstdint>
#include <iostream>
#include <string>
#include <vector>

using namespace darra::opcua;

// The business side stores its own MI configuration for use in restoration.
struct MiSpec {
std::string node_id;
AttributeId attr = AttributeId::Value;
double sampling_ms = -1.0;
};

std::vector<MiSpec> g_specs = {
{"ns=2;s=Temp", AttributeId::Value, 200.0},
{"ns=2;s=Vibration.X", AttributeId::Value, 50.0},
};

Subscription Build( Session& s ) {
Subscription sub = s.CreateSubscription(500.0);
for (auto const& spec : g_specs) {
sub.AddNode(spec.node_id, OnValue, spec.attr, spec.sampling_ms);
}
return sub;
}

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

s.Events().on_reconnected = [&](std::string const&) {
// The local subscription table had already been cleared when the transfer failed — it was rebuilt here
std::vector<uint32_t> old_ids = { sub.Handle() };
auto rcs = s.TransferSubscriptions(old_ids, /*send_initial_values=*/true);
for (auto rc : rcs) {
if (rc != Status::Good) {
std::cerr << "转移失败, 重建订阅\n";
sub = Build(s);
break;
}
}
};

s.Connect();
Subscription sub = Build(s);

// You can also manually transfer them at any time if needed.
std::vector<Status> r = s.TransferSubscriptions({ sub.Handle() }, true);

Recovery Process​

Reconnect succeeded
↓
Try TransferSubscriptions for the subscriptions already on the server
├─ Success → subscriptions attached to the new Session, local subscription table kept
└─ Failure → clear the local subscription table (do not re-send CreateMonitoredItemsRequest)
↓
Business side detects this via the state callback → rebuilds Sub + all MI itself

Best Practices​

  • The business side should store its own configuration MI (NodeId / AttributeId / SamplingInterval / QueueSize / DiscardOldest / filter); don't count on it to keep SDK track of it for you.
  • DarraUa_MonitoredItemHandle This is provided by the server; it will change after a rebuild, so do not persist it.
  • Try it in on_reconnected first TransferSubscriptions; if it returns anything other than "Good," rebuild it.
  • After changing key parameters, pause and then resume

Cross-Language Comparison​

ItemC#PythonJavaC++RustC
Transfer SubscriptionTransferSubscriptionstransfer_subscriptionstransferSubscriptionsTransferSubscriptionstransfer_subscriptionsDarraUa_Session_TransferSubscriptions
Subscribe to Lost EventsEvents.SubscriptionLostNoneNoneNoneNoneNone
Subscribe to Recovery EventsEvents.SubscriptionRestoredNoneNoneNoneNoneNone

Automatic subscription reconstruction is currently supported only in C#.(First TransferSubscriptions, a failure, then a full rebuild.) Python, Java, Rust, C++, and C all require manual execution TransferSubscriptions.

Next Step​