Skip to main content

Session — Session

Session This is the main Client class; one instance corresponds to one OPC UA Session. All access operations (read / write / browse / subscribe / call / history) originate from this object.

Subpage Redirect
  • For structural and connection specifications, please refer to Constructor.
  • For connecting/disconnecting, please refer to Connection Management.
  • For single-item and batch read/write operations, please refer to Read / Write.
  • For information on the unified channel for internal incidents, please refer to Events.
  • For namespace lookup, please refer to Namespaces.
  • For information on heartbeat and session keep-alive, please refer to KeepAlive.

Public Fields / Property Methods​

CategoryPropertyTypeAccessDescription
Connectionsendpoint_url()&strRThe endpoint URL specified at construction
handle()u32RC-layer handle (for internal use)
Statusstate()SessionStateRCurrent session status (Disconnected / Connecting / Connected / Reconnecting / Closing / Failed)
is_connected()boolRWhether the connection is active (= state() == Connected)
Child Objectsnodes()NodeCollectionRLazy Load node accessor
namespaces()NamespaceCollectionRNamespace URI ↔ Index conversion
EventseventsArc<Mutex<OpcUaEvents>>RWUnified channel for session-level events; access using .lock()
Configurationkeep_alive_interval_ms()u32RKeepAlive heartbeat interval (default 10000 ms, 0 = disabled)
set_keep_alive_interval_ms(ms)()WChange the heartbeat interval (&mut self)
auto_publish()boolRWhether the background publish thread is enabled
set_auto_publish(b)()WEnable or disable the background publish thread (&mut self)

Public Methods​

Structure​

Connection Management​

  • connect(&mut self) — Establish a connection (Hello → SecureChannel → Session → Activate)
  • disconnect(&mut self) — Connection terminated voluntarily; the instance can reconnect
  • Drop (Implicit) — Automatically disconnects and closes when leaving the scope

Heartbeat and Release​

  • publish(timeout_ms: u32) -> StatusCode — Manually trigger a Publish once (usually not necessary)

Read / Write​

Browse​

Subscribe​

Method Call​

History​

Control​

  • cancel(&self, request_handle: u32) — Cancel Unfinished Requests

Complete Example​

use darra_opcua::{Session, Variant};

fn main() -> Result<(), Box<dyn std::error::Error>> {
darra_opcua::initialize()?;

let mut s = Session::new("opc.tcp://localhost:4840")?;
s.events.lock().unwrap().on_connected.push(Box::new(|e| {
println!("Connected to {}", e.endpoint_url);
}));
s.events.lock().unwrap().on_disconnected.push(Box::new(|e| {
println!("Disconnected: {}", e.status_code);
}));
s.connect()?;

// Read
let dv = s.read("ns=2;s=Temperature")?;
println!("T = {} °C", dv.variant().try_get_f64().unwrap_or(0.0));

// Write
let mut v = Variant::new();
v.set_f64(42.0);
let st = s.write("ns=2;s=Setpoint", &v)?;
println!("Write status = {}", st);

// Subscribe
let sub = s.create_subscription(500.0)?;
sub.add_node("ns=2;s=Counter", |args| {
println!("{:?} = {:?} ({})",
args.node_id, args.value_string, args.status);
})?;

// Methods
let mut a = Variant::new(); a.set_i32(3);
let mut b = Variant::new(); b.set_i32(4);
let outputs = s.call("ns=2;s=Calc", "ns=2;s=Calc.Add", &[a, b])?;
for v in &outputs {
println!("sum = {}", v.try_get_i32().unwrap_or(0));
}

std::thread::sleep(std::time::Duration::from_secs(60));
Ok(()) // s / sub: Automatically release when dropped
}

Next Step​