Skip to main content

Create a Subscription

def create_subscription(self, publishing_interval_ms: float = 500.0) -> Subscription: ...
Prerequisite Reading / Supplementary Materials

Parameters​

ParametersDefaultNote
publishing_interval_ms500.0The minimum interval for the server to push data; the server may revise (adjust) it.

Examples​

with ua.create_subscription(publishing_interval_ms=500) as sub:
print(f"Subscription created with id={sub.subscription_id}")
print(f"Requested interval = {sub.publishing_interval_ms}ms")
publishing_interval_ms is not a server-side revision value

sub.publishing_interval_ms returns the value passed at creation time. The actual interval after a server "revise" can only be obtained by calling sub.modify(...) (ModifySubscriptionResult.revised_publishing_interval_ms); at the same time, modify writes the revised value back to sub.publishing_interval_ms.

Multiple Subscription Channels​

If an app requires both 100 ms high-frequency subscriptions (fitness data) and 5000 ms low-frequency subscriptions (ambient temperature), create 2 separate subscription branches; do not combine them into a single one:

with ua.create_subscription(100) as fast, ua.create_subscription(5000) as slow:
fast.add_node("ns=2;s=AxisPosition")
slow.add_node("ns=2;s=AmbientTemperature")
# ...

Each time it runs create_subscription, a separate subscription is DarraUa_Subscription_Create created, each with its own publishing interval configuration; changes to one do not affect the other.

OpcUaSubscription Public Attributes​

CategoryPropertyTypeAccessDescription
Identifierssubscription_idintRLocal approximate ID = handle & 0x0FFFFFFF (not server-side SubscriptionId; cannot be passed directly to transfer_subscriptions)
handleintRHandle value; generally not used directly
sessionOpcUaSessionRSession
Configurationpublishing_interval_msfloatRThe publishing interval provided at creation; it is overwritten by the server-side value only after a successful modify() call
MonitoredItemsmonitored_node_idsList[str]RSnapshot of all current MI NodeIds
len(sub)intRMI Length (using Python's built-in len())
Events (List[Callable])data_changedList[Callable[[DataChangeEventArgs], None]]RWList of data change callbacks

Iteration Support: for mi_handle, node_id in sub: ...

OpcUaSubscription Public Methods​

MethodsNote
add_node(node_id, callback=None, attribute=Value, sampling_interval_ms=-1.0, mode=Reporting, queue_size=1, discard_oldest=True, filter=None)Add Order MI, Returns int Handle
add_many(node_ids, callback=None, attribute=Value)Add in Bulk, Returns List[Tuple[int, StatusCode]]
remove(mi_handle)Removal Order MI
remove_by_node_id(node_id)Press NodeId to remove all matching items MI; returns int the removal count.
set_publishing_enabled(enabled)Turn on/off posts for the entire subscription, Returns StatusCode
modify(publishing_interval_ms, lifetime_count, ...)Change Subscription Settings
set_monitoring_mode(mode, mi_handles)Batch Cut mode, return List[StatusCode]
set_mode(handle, mode)Cut a single item MI mode, return StatusCode
modify_monitored_items(requests)Batch Change Sampling
modify_item(handle, sampling_interval_ms, queue_size=1, discard_oldest=True)Amendment MI
republish(retransmit_sequence_number)Request to resend, returned StatusCode
add_triggering_links(trigger_mi_id, linked_mi_ids)SetTriggering Add Linkage (Currently Returns Constant BadNotSupported)
remove_triggering_links(trigger_mi_id, linked_mi_ids)SetTriggering Unlink (Returns the current constant BadNotSupported)
dispose() / close()Release, Server-side DeleteSubscription
subscription_id cannot be passed directly to transfer_subscriptions

sub.subscription_id is a local approximation of handle & 0x0FFFFFFF, whereas transfer_subscriptions needs the SubscriptionId from the server's perspective. If you want to use transfer, you must keep track of the server-side id yourself; do not use this property to make the call.

Event​

sub.data_changed: List[Callable[[DataChangeEventArgs], None]]

For more information, see data_changed event.

Next Step​