Skip to main content

KeepAlive Heartbeat

DarraOpcUa built-in KeepAlive heartbeat: periodically reads ServerStatus.CurrentTime (i=2258) as a probe, both keeping the session from timing out and detecting connection anomalies at the earliest moment.

Prerequisite Reading

KeepAliveIntervalMs​

public uint KeepAliveIntervalMs { get; set; }
ItemValue
Unitmilliseconds
Default10000 (10 seconds)
0Disable

Changes take effect immediately:

ua.KeepAliveIntervalMs = 5000;  // Change it to 5 seconds

Start-Stop System​

TimingBehavior
After Connect() succeedsAutomatic Startup
Disconnect()Auto-Stop
Dispose()Auto-Stop
Modify KeepAliveIntervalMs (when connected)Out with the Old, In with the New
Automatic ReconnectionKeep your heart beating; keep running at the same pace.

Heartbeat Incident​

Triggered every time a heartbeat is successful Events.KeepAlive:

ua.Events.KeepAlive += (s, e) =>
Console.WriteLine($"KeepAlive: 服务器时间 {e.ServerTimeUtc:HH:mm:ss}, status={e.ServerStatus}");

KeepAliveEventArgs Field: ServerTimeUtc (server time read during a heartbeat) / ServerStatus (StatusCode) / TimestampUtc (local callback time).

Heartbeat Failure (Read throws an exception) Triggered Events.CommunicationError:

ua.Events.CommunicationError += (s, e) =>
{
if (e.Category == "keepalive")
Console.WriteLine($"Heartbeat lost: {e.StatusCode} {e.Message}");
};

CommunicationErrorEventArgs Field: StatusCode / Category (this is "keepalive") / Message / TimestampUtc.

Relationship with SessionTimeout​

The session timeout is determined sessionTimeoutMs by the constructor (default 60000 ms = 60 seconds, which the client requests from the server when establishing a session (do not set it too short). If the server does not receive any activity within the session timeout window, it will terminate the session, and the client will subsequently receive BadSessionIdInvalid / BadSessionClosed.

Therefore the KeepAlive interval must be significantly shorter than sessionTimeoutMs, otherwise the heartbeat cannot keep up with the cleanup cadence. The relationship between the two defaults:

sessionTimeoutMsKeepAliveIntervalMsRatio
60000 ms (Default)10000 ms (Default)1/6

sessionTimeoutMs When increasing the value, increase KeepAlive by the same proportion; for example, configure sessionTimeoutMs: 600_000 it as follows KeepAliveIntervalMs = 100_000.

Why use ServerStatus.CurrentTime​

OPC UA has no dedicated KeepAlive protocol message; the SDK's heartbeat is a lightweight Read of WellKnownNodes.Server_ServerStatus_CurrentTime (i.e. i=2258). ServerStatus.CurrentTime is a standard OPC Foundation node that every specification-compliant server has, and reading it returns a single scalar.

Custom Heartbeat Logic​

If you want to completely disable the SDK built-in heartbeat, implement your own:

ua.KeepAliveIntervalMs = 0;  // Disable SDK Built-in

// Set a Timer Yourself
var timer = new System.Threading.Timer(_ =>
{
if (!ua.IsConnected) return;
try
{
using var dv = ua.Read("i=2258"); // Or read "Business Heartbeat Node"
}
catch (OpcUaException ex)
{
Console.WriteLine($"自定义心跳失败: {ex.StatusCode} {ex.Message}"); // Handle it yourself
}
}, null, 5000, 5000);

Impact on Performance​

KeepAlive sends only 1 Read request per cycle RPC; the server reads a scalar value and returns it DataValue, at the same cost as a single standard Read operation, with no additional protocol overhead. With the default 10-second cycle, there are only 6 such requests per minute RPC.

Next Step​