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.
- For Heartbeat Event Callbacks, see Events.
- For the automatic reconnection process, please see Connection Management.
keep_alive_interval_ms
@property
def keep_alive_interval_ms(self) -> int: ...
@keep_alive_interval_ms.setter
def keep_alive_interval_ms(self, value: int) -> None: ...
| Item | Value |
|---|---|
| Unit | milliseconds |
| Default | 10000 (10 seconds) |
<= 0 | Disabled (Heartbeat thread does not start) |
Changes take effect immediately:
ua.keep_alive_interval_ms = 5000 # Change it to 5 seconds
When the value is changed while a connection is active, the old thread will be paused before a new thread is started.
Constructing a time-transmitted message:
OpcUaSession(..., keepalive_interval_ms=5000) # The constructor parameter name is keepalive_interval_ms
Start-Stop System
| Timing | Behavior |
|---|---|
| After connect() succeeds | Start |
| disconnect() | Stop |
| Modify keep_alive_interval_ms (when connected) | Out with the Old, In with the New |
| SDK Automatic Reconnection | No-Heartbeat Thread — The thread automatically resumes polling is_connected after recovery |
| close() / dispose() | Stop |
KeepAlive runs in a separate background thread; the thread name does not DarraOpcUa-KeepAlive prevent the process from exiting.
Heartbeat Incident
Triggered every time a heartbeat succeeds events.keep_alive; the parameters are KeepAliveEventArgs (server_time_utc / server_status / timestamp_utc):
ua.events.keep_alive.append(
lambda e: print(f"KeepAlive @ {e.server_time_utc.isoformat()} (status={e.server_status.name})"))
Heartbeat failure (triggered by an exception) occurred events.communication_error; the parameters are CommunicationErrorEventArgs (status_code / category / message); the heartbeat path is always category a string "keepalive":
def on_err(e):
if e.category == "keepalive":
print(f"Heartbeat lost: {e.status_code.name} {e.message}")
ua.events.communication_error.append(on_err)
Relationship with SessionTimeout
The SDK default value for session_timeout_ms the client constructor parameter is 60000 ms (60 seconds), sent to the server for session keep-alive. The KeepAlive interval must be significantly shorter than the one actually used by the server SessionTimeout; otherwise, the server will proactively clean up the session due to "prolonged inactivity," and the client will have to wait until the next request to RPC obtain it BadSessionIdInvalid.
| Server-side SessionTimeout | KeepAlive Reference |
|---|---|
| 60 seconds (SDK default value) | 10 seconds (SDK default heartbeat) |
| 5 minutes | 5 seconds |
| 1 hour | 30 seconds |
Why use ServerStatus.CurrentTime
The client's SDK heartbeat sends a lightweight read request as a probe.ServerStatus.CurrentTime (i=2258) is a standard OPC Foundation node found on any server that complies with the specification; it reads a scalar value DateTime, and the request body is very small. The value read is directly source_timestamp used as the keep_alive event's server_time_utc.
Custom Heartbeat Logic
If you want to completely disable the SDK built-in heartbeat, implement your own:
import threading, time
from darra_opcua import OpcUaException # Module Name darra_opcua
ua.keep_alive_interval_ms = 0 # <= 0 Disabled SDK Built-in
stop = threading.Event()
def hb():
while not stop.wait(5.0):
if not ua.is_connected:
continue
try:
with ua.read("i=2258") as dv: # Or read "Business Heartbeat Node"
pass
except OpcUaException as ex:
print(f"heartbeat: {ex}") # ex.status / ex.status_code Get status code
threading.Thread(target=hb, daemon=True).start()
Impact on Performance
Each heartbeat = 1 read operation ( RPC network round-trip + server reading a scalar + serialization). If the heartbeat interval is extended or disabled <= 0, the overhead is zero.