Skip to main content

Subscription Resumes Automatically

Overview​

The SDK has built-in automatic reconnection: after a successful connect it starts a watchdog thread that monitors the SecureChannel reader thread; if the reader BadConnectionClosed exits BadCommunicationError, the reconnect worker thread will cyclically rebuild the connection according to SessionConfig (TCP → SecureChannel → ActivateSession); after successful reconnection,Automatic Invocation TransferSubscriptions Take Over Subscription。

  1. TransferSubscriptions — Transfer the old data to SubscriptionId the current session; the server retains all sub-state, MI queue, and sampling configurations—no need to rebuild them. MonitoredItem
  2. Clear the local subscription table in case of failure — C SDK No Automatically recreate the subscription; the business side must reconfigure it using the original parameters +Create AddNode / AddEventItem

Reconnection is handled SDK automatically TransferSubscriptions by [...]; the business side needs to do the following:Subscription Status Callback andReconstruction After a Failed Transfer. An explicit call by the user DarraUa_Session_Disconnect will halt the entire reconnection process.

API​

Functions / CallbacksCategoryReading and WritingNote
DarraUa_Session_TransferSubscriptions(h, subscription_ids, count, send_initial_values, out_results)MethodsWriteTransfer the subscription to the current session
DarraUa_Session_SetStateCallback(h, cb, user_context)CallbackReadSession Status (RECONNECTING / CONNECTED / FAILED)
DarraUa_Subscription_SetStateCallback(sub, cb, user_context)CallbackReadSubscription Status (LIFETIME_EXPIRED / RECREATING)

DarraUa_Session_TransferSubscriptions Input parameter: is subscription_idsServer-side Subscription Id (Not local DarraUa_SubscriptionHandle), out_results an array allocated count by the caller;
DarraUa_Status NULL may be passed to ignore it; SDK no allocation is made, and no release is required.

Code Example​

#include <opcua/opcua.h>

/* Session Status Callback — Logs when a reconnection is detected, to facilitate prompts UI */
static void on_session_state(DarraUa_SessionHandle h,
DarraUa_SessionState s,
DarraUa_Status code, void* ctx)
{
(void)h; (void)ctx;
if (s == UA_SESSION_RECONNECTING)
fprintf(stderr, "会话断开, 重连中 (%s)\n", DarraUa_StatusName(code));
else if (s == UA_SESSION_CONNECTED)
printf("会话已恢复\n");
}

/* Data Change Callback — See signature [DataChange Event](./events) */
static void on_change(DarraUa_SubscriptionHandle sub,
DarraUa_MonitoredItemHandle mi,
const DarraUa_DataValue* dv, void* ctx)
{
(void)sub; (void)mi; (void)dv; (void)ctx;
}

/* Subscription Status Callback — Business-Side Reconstruction Upon Transfer Failure or Lifetime Expiration */
static void on_sub_state(DarraUa_SubscriptionHandle sub,
DarraUa_SubscriptionState s,
DarraUa_Status code, void* ctx)
{
(void)sub; (void)ctx;
if (s == UA_SUB_STATE_LIFETIME_EXPIRED ||
s == UA_SUB_STATE_RECREATING) {
fprintf(stderr, "订阅需重建: state=%d %s\n",
(int)s, DarraUa_StatusName(code));
}
}

int main(void) {
DarraUa_SessionConfig cfg;
DarraUa_SessionConfig_Init(&cfg);
cfg.endpoint_url = "opc.tcp://localhost:4840";
cfg.security_mode = UA_MSG_SECURITY_MODE_NONE;
cfg.security_policy_uri = "http://opcfoundation.org/UA/SecurityPolicy#None";
cfg.user_token_type = UA_USER_TOKEN_ANONYMOUS;

DarraUa_SessionHandle h = UA_INVALID_SESSION_HANDLE;
DarraUa_Session_Create(&cfg, &h);
DarraUa_Session_SetStateCallback(h, on_session_state, NULL);
DarraUa_Session_Connect(h);

/* Create sub + MI */
DarraUa_SubscriptionConfig sc;
DarraUa_SubscriptionConfig_Init(&sc);
sc.publishing_interval_ms = 500.0;
sc.publishing_enabled = 1;

DarraUa_SubscriptionHandle sub = UA_INVALID_SUBSCRIPTION_HANDLE;
DarraUa_Subscription_Create(h, &sc, &sub);
DarraUa_Subscription_SetStateCallback(sub, on_sub_state, NULL);

DarraUa_MonitoredItemHandle mi = UA_INVALID_MONITORED_ITEM_HANDLE;
DarraUa_Subscription_AddNode(sub, "ns=2;s=Temp", UA_ATTR_VALUE,
200.0, on_change, NULL, &mi);

/* Unplug the Ethernet cable for 30 seconds: The log shows RECONNECTING → CONNECTED,
SDK Automatic TransferSubscriptions, data continues automatically */

/* ... Business ... */

DarraUa_Subscription_Delete(sub);
DarraUa_Session_Disconnect(h);
DarraUa_Session_Close(h);
return 0;
}

Recovery Process​

reader thread exits due to BadConnectionClosed / BadCommunicationError
↓
watchdog triggers the reconnect worker thread, which cyclically rebuilds per SessionConfig
↓
TCP + SecureChannel + ActivateSession (reusing sessionId / auth_token)
↓
TransferSubscriptions(old sub id)
├─ Good → subscriptions kept, server-side sub state / MI queue / sampling config all retained
└─ Bad → SDK clears the local subscription table, business side rebuilds
↓
If AutoPublish was enabled before, the publish thread is restarted automatically

What should the business side do after a transfer fails?​

The SDK handles detection and transfer only, not reconstruction. If the transfer fails the local subscription table is cleared and the business layer must replay the data with the original parameters:

  • DarraUa_Subscription_Create + DarraUa_SubscriptionConfig (publishing_interval_ms / lifetime_count / max_keep_alive_count / max_notifications / priority / publishing_enabled)
  • MonitoredItem: NodeId / AttributeId / sampling_interval_ms / queue_size / discard_oldest / deadband_type / deadband_value (see DarraUa_MonitoredItemConfig)
  • Callback and Re-registration user_context Required (AddNode / AddNodes / MonitoredItem_SetCallback / AddEventItem)

The server-side ID mi may change, The business logic should reconcile transactions based on the business identifier included in the callback DarraUa_MonitoredItemHandle or the one it set in user_context, and should not persist the server-side ID.

Best Practices​

  • Use the business identifier for user_context reconciliation; do not persist the server ID mi.
  • Register DarraUa_Session_SetStateCallback Connection Re-establishment Detection, Register Subscription Expiration DarraUa_Subscription_SetStateCallback Detection
  • Move the rebuild logic to the business side (triggered in the subscription status callback); do not assume that it SDK will rebuild automatically.
  • For changes to key parameters Modify, which is safer than deleting and rebuilding

Cross-Language Comparison​

C#PythonJavaC++RustC
TransferSubscriptionstransfer_subscriptionstransferSubscriptionsTransferSubscriptionstransfer_subscriptionsDarraUa_Session_TransferSubscriptions

Next Step​