Skip to main content

Subscription

Subscription A subscription container that manages a set of MonitoredItem (MI) and receives DataChange notifications via callback closures.

Subpage Redirect

One-Minute Overview​

use darra_opcua::{Session, AttributeId, MonitoringMode};

let mut s = Session::new("opc.tcp://localhost:4840")?;
s.connect()?;

// Create
let sub = s.create_subscription(500.0)?;

// Add Order MI + Closure Callback
sub.add_node("ns=2;s=Counter", |args| {
println!("{:?} = {:?} ({})",
args.node_id, args.value_string, args.status);
})?;
sub.add_node("ns=2;s=Temperature", |args| {
println!("temp = {:?}", args.value_string);
})?;

// Bulk Add (Share a Callback)
let nodes: Vec<String> = (1..=100).map(|i| format!("ns=2;s=T{}", i)).collect();
let nodes_ref: Vec<&str> = nodes.iter().map(String::as_str).collect();
let results = sub.add_many(&nodes_ref, AttributeId::Value, |args| {
println!("{:?} = {:?}", args.node_id, args.value_string);
})?;

// Change the sampling interval MI for a single item
let h = results[0].0;
sub.modify_item(h, 100.0, 1, true)?;

// Pause / Resume
sub.set_mode(h, MonitoringMode::Disabled)?;
sub.set_mode(h, MonitoringMode::Reporting)?;

// Remove a single
sub.remove(h);

// When the sub-scope ends, sub Drop automatically cleans _Subscription_Delete up all MI

Subscribe to Lifecycle Events​

Collected through the Unified s.events.lock().unwrap().on_any Channel:

use darra_opcua::OpcUaEventCategory;

s.events.lock().unwrap().on_any.push(Box::new(|e| {
match e.category {
OpcUaEventCategory::SubscriptionCreated => println!("sub created: {}", e.source),
OpcUaEventCategory::MonitoredItemAdded => println!("mi added: {}", e.source),
OpcUaEventCategory::MonitoredItemRemoved => println!("mi removed: {}", e.source),
OpcUaEventCategory::SubscriptionLost => println!("sub lost: {}", e.source),
_ => {}
}
}));

For more information, see Session Events.

Drop Auto Cleanup​

{
let sub = s.create_subscription(500.0)?;
sub.add_node("ns=2;s=X", |_| {})?;
// ... using sub ...
} // <- sub Drop, Automatic _Subscription_Delete (clears both server and local)

No need to manually adjust the Subscription_Delete.items table—all entries will be automatically Arc<TrampolineCtx> dropped.

Next Step​