Skip to main content

KeepAlive Heartbeat

OpcUaSession 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

getKeepAliveIntervalMs() / setKeepAliveIntervalMs(int)​

public int  getKeepAliveIntervalMs();
public void setKeepAliveIntervalMs(int ms);
ItemValue
Unitmilliseconds
Default10000 (10 seconds)
Minimum Recommendation1000
0Disable

Changes take effect immediately (restart the timer):

ua.setKeepAliveIntervalMs(5000);  // Change it to 5 seconds

Start-Stop System​

TimingBehavior
After connect() succeedsAutomatic Startup
disconnect()Auto-Stop
Modify setKeepAliveIntervalMs()Restart
Automatic reconnection was successfulAutomatic Startup

Heartbeat Incident​

Triggered every time a heartbeat is successful events.onKeepAlive:

ua.events.onKeepAlive.add(e ->
System.out.println("KeepAlive @ " + e.serverTimeUtc + " (status=" + e.serverStatus + ")"));

Heartbeat Failure (Read throws an exception) Triggered events.onCommunicationError:

ua.events.onCommunicationError.add(e -> {
if ("keepalive".equals(e.category)) {
System.err.println("Heartbeat lost: " + e.statusCode + " " + e.message);
}
});

Relationship with SessionTimeout​

There are two values to distinguish when it comes to session timeouts: clientRequest( sessionTimeoutMs SDK configuration option, default 60 seconds), after negotiation with the serverEffective Session timeout (CreateSession the returned RevisedSessionTimeout). The effective value is determined by the server; it varies by server, so the value returned by the server takes precedence.

KeepAlive must be significantly shorter than the active session timeout; otherwise, the server will proactively clean up the session due to "prolonged inactivity," and the client will only BadSessionIdInvalid realize this after receiving the response.

Valid Session Timeout (After Server Negotiation)KeepAlive Interval
60 seconds (SDK default value for the request)10 seconds (SDK default)
5 minutes5 seconds
1 hour30 seconds

Why use ServerStatus.CurrentTime​

OPC UA There is no dedicated KeepAlive protocol message; the common practice is to send a lightweight Read request as a heartbeat. ServerStatus.CurrentTime (WellKnownNodes.SERVER_SERVER_STATUS_CURRENT_TIME = i=2258) is a standard OPC Foundation node; any server that complies with the specification has one, and the overhead of a Read request is minimal.

Custom Heartbeat Logic​

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

ua.setKeepAliveIntervalMs(0);  // Disable SDK Built-in

// Set a Timer Yourself
java.util.Timer timer = new java.util.Timer("MyHeartbeat", true);
timer.schedule(new java.util.TimerTask() {
@Override public void run() {
if (!ua.isConnected()) return;
try (OpcUaDataValue dv = ua.read(WellKnownNodes.SERVER_SERVER_STATUS_CURRENT_TIME)) {
// Using DV to Build Custom Business Solutions
} catch (OpcUaException ex) {
// Handle it yourself
}
}
}, 5000, 5000);

Impact on Performance​

KeepAlive overhead per instance:

  • 1 Read RPC (Network round trip + server-side read + serialization)
  • The time required for a single operation varies depending on the network and server (with significant differences between local area networks and remote connections); test it in your actual environment.

The default heartbeat interval is 10 seconds (keepAliveIntervalMs), with only 1 Read request sent per interval; CPU / Determine the bandwidth usage based on actual testing in your environment.

Next Step​