Skip to main content

KeepAlive Heartbeat (C)

Session keep-alive involves two distinct processes, each with its C SDK own approach:

  • Prevent the session from timing out: It depends on the server SessionTimeout; the client just needs to send requests periodically. C protocol stackDon't set the timer on your own — When an application-level heartbeat is required, the caller should periodically read ServerStatus.CurrentTime (i=2258) on its own; this is precisely what DarraUa_Session_ReadCurrentTime is designed for (see Custom Application-Level Heartbeat).
  • Connection Error Detected: This is handled by the built-in connection-loss watchdog thread—it monitors the receive SecureChannel thread; if the receive thread exits due to a socket error, it determines that the connection has been lost and triggers an automatic reconnection (see Connect / Disconnect).
Prerequisite Reading

Configuration Fields​

DarraUa_SessionConfig cfg;
DarraUa_SessionConfig_Init(&cfg);
cfg.keepalive_interval_ms = 5000; /* 5 seconds */
ItemValue
Unitmilliseconds
Default10000 (10 seconds)
0Disable
The C protocol stack does not use this field to schedule periodic tasks

keepalive_interval_ms; it is a DarraUa_SessionConfig field of that is stored with the handle solely as a configuration value. The caller initiates heartbeat probes at this interval (see Custom Application-Level Heartbeat).

keepalive_interval_ms Read only at the time DarraUa_Session_Create(&cfg, &h); subsequent changes to the cfg do not affect handles that have already been created. To change a value, you must _Close→ modify the cfg → re- _Create.

Start-Stop System​

TimingBehavior
_Connect After successStart the connection-loss watchdog thread + automatically reconnect the worker thread
_Disconnect / _CloseStop the watchdog and the reconnection worker thread, set the "user-initiated disconnect" flag, and prevent automatic reconnection from being triggered
Automatic reconnection was successfulRe-attach the watchdog to monitor the new receive thread; enable automatic restart AutoPublish of Publish if it was active before the connection was lost

Heartbeat Incident​

The C SDK exposes no separate "KeepAlive event callback"; connection-loss detection results are reported uniformly through the status callback — status moves from CONNECTED to RECONNECTING (with auto_reconnect on) or to FAILED.

static void on_state(DarraUa_SessionHandle h, DarraUa_SessionState s,
DarraUa_Status code, void* ctx)
{
if (s == UA_SESSION_RECONNECTING)
printf("Link lost, reconnecting (%s)\n", DarraUa_StatusName(code));
}
DarraUa_Session_SetStateCallback(h, on_state, NULL);

Relationship with SessionTimeout​

The client uses CreateSessionsession_timeout_ms to request the session keep-alive duration (default 60000 = 60 seconds); that is, if there are no requests to the server during this period, the session can be recycled. The application-level detection interval must be significantly shorter than this (typically about 1/10); otherwise, the server will proactively clean up the session due to “prolonged inactivity,” and the client will only UA_STATUS_BAD_SESSION_ID_INVALID discover this after receiving the response.

session_timeout_msDetection Cycle Reference
60000 (default: 60 seconds)5000
300000 (5 minutes)30000
3600000 (1 hour)300000

Why use ServerStatus.CurrentTime​

OPC UA has no dedicated KeepAlive protocol message; a heartbeat is just a lightweight Read request. DarraUa_Session_ReadCurrentTime reads ServerStatus.CurrentTime (i=2258), an OPC Foundation standard node that every specification-compliant server has; it is a single RPC, so the overhead is tiny — the header exists specifically to serve as a "lightweight KeepAlive heartbeat probe" API.

Custom Application-Level Heartbeat​

The C layer does not run a heartbeat timer; if you need a heartbeat, you must start your own thread to perform periodic checks:

#include <opcua/opcua.h>
#include <stdio.h>

#if defined(_WIN32)
# include <windows.h>
# define OPCUA_SLEEP_SEC(s) Sleep((s) * 1000)
#else
# include <unistd.h>
# define OPCUA_SLEEP_SEC(s) sleep(s)
#endif

/* ... DarraUa_Session_Create + (omitted DarraUa_Session_Connect ) ... */

/* My Own Thread */
while (running) {
OPCUA_SLEEP_SEC(5);
if (DarraUa_Session_GetState(h) != UA_SESSION_CONNECTED) continue;

DarraUa_Status st = 0;
DarraUa_DataValue* dv = DarraUa_Session_ReadNode(
h, "i=2258", UA_ATTR_VALUE, &st);
if (dv) DarraUa_DataValue_Delete(dv);

if (!UA_STATUS_IS_GOOD(st)) {
/* Handle disconnections on your own */
}
}

Impact on Performance​

One probe = 1 Read RPC (request + server read + response); the time taken depends on the network. Set the probe interval to approximately session_timeout_ms 1/10; there is no need to make it more frequent.

Next Step​