Create a Subscription
pub fn Session::create_subscription(&self, publishing_interval_ms: f64)
-> Result<Subscription, OpcUaError>;
Prerequisite Reading / Supplementary Materials
- Add MonitoredItem Please see Add MonitoredItem.
- DataChange See the fields DataChange Callback.
Parameters
| Parameters | Default | Note |
|---|---|---|
| publishing_interval_ms | Recommendations 500.0 | The minimum interval for the server to push data; the server may revise (adjust) it |
Examples
let sub = s.create_subscription(500.0)?;
println!("Subscription created with id={}", sub.subscription_id());
println!("Actual interval = {}ms", sub.publishing_interval_ms()); // Server-side revised
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 streams; do not combine them into a single one:
let fast = s.create_subscription(100.0)?;
fast.add_node("ns=2;s=AxisPosition", |args| {
println!("axis = {:?}", args.value_string);
})?;
let slow = s.create_subscription(5000.0)?;
slow.add_node("ns=2;s=AmbientTemperature", |args| {
println!("ambient = {:?}", args.value_string);
})?;
Each subscription has its own release schedule, and they do not affect one another.
Subscription Public Properties
| Category | Property | Type | Access | Description |
|---|---|---|---|---|
| Identifiers | subscription_id() | u32 | R | Subscription ID from the server's perspective (approximate, for display purposes) |
handle() | u32 | R | C-layer handle (for internal use) | |
session_handle() | u32 | R | Session handle | |
| Configuration | publishing_interval_ms() | f64 | R | Actual publishing interval (revised) |
set_publishing_enabled(b) | StatusCode | W | Enable or disable publishing for the entire subscription | |
| MonitoredItems | monitored_node_ids() | Vec<String> | R | Snapshot of all current MI NodeIds |
Subscription Public Methods
| Methods | Note |
|---|---|
| add_node(node_id, cb) | Add Order MI |
| add_node_with(node_id, attr, sampling_ms, cb) | Add Complete Parameters |
| add_with_filter(node_id, attr, sampling_ms, filter, cb) | With DataChangeFilter Add |
| add_many(node_ids, attr, cb) | Bulk Add (Share cb) |
| remove(mi_handle) | Removal Order MI |
| remove_by_node_id(node_id) | Press NodeId to remove all matching items |
| set_publishing_enabled(b) | Enable or disable publishing for the entire subscription |
| pump(timeout_ms) | Manually publish once |
| modify(...) | Modify subscription parameters (&mut self) |
| set_monitoring_mode(mode, &[mi]) | Batch Cut Mode |
| set_mode(mi, mode) | Slice individually MI mode |
| modify_monitored_items(&[req]) | Batch Change Sampling |
| modify_item(mi, sampling_ms, queue_size, discard_oldest) | Amendment MI |
| republish(seq_no) | Request Resend |
Closure Callback Signatures
F: FnMut(&DataChangeEventArgs) + Send + 'static
For more information, see DataChange Callback.
Drop Auto Cleanup
{
let sub = s.create_subscription(500.0)?;
sub.add_node("ns=2;s=X", |_| {})?;
} // <- Here, "sub Drop" automatically calls the server's DeleteSubscription
The C layer's Subscription_Delete cleans up all MIs at once; there is no need to remove them one by one.