Skip to main content

Multi-Source Subscription Collection

Simultaneously monitor multiple data points—including temperature, pressure, flow rate, and counters—and push data only when there are changes on the server, so as not to waste bandwidth. Use C# Example.

Sample Configuration

Complete Code​

using DarraOpcUa_Client;
using System;
using System.Threading;

class Program
{
static void Main()
{
// Replace with an on-site PLC industrial PC IP
const string endpoint = "opc.tcp://192.168.1.100:4840";

// Nodes are formatted as ID ns=2;s=<DBor GVLpath>.<variable name>; the actual names are subject to the identifiers used in the project PLC .
var nodes = new[]
{
"ns=2;s=Boiler1.Temperature",
"ns=2;s=Boiler1.Pressure",
"ns=2;s=Boiler1.FlowRate",
"ns=2;s=Line1.Counter",
};

// 1. Connection
using var ua = new DarraOpcUa(endpoint);
ua.Connect();
Console.WriteLine($"[OK] 已连接 {endpoint}");

// 2. Create a subscription + set up a callback
using var sub = ua.CreateSubscription(publishingIntervalMs: 500);
sub.DataChanged += (s, e) =>
Console.WriteLine($"{DateTime.Now:HH:mm:ss.fff} {e.NodeId,-32} = {e.ValueString,-12} [{e.Status}]");

// 3. Add Monitoring Items in Bulk
var results = sub.AddMany(nodes);
for (int i = 0; i < nodes.Length; i++)
Console.WriteLine($" add {nodes[i]} -> {results[i].status}");

// 4. Continue receiving push notifications until you press Ctrl+C
Console.WriteLine("\n等待数据推送, 按 Ctrl+C 停止...\n");
Thread.Sleep(Timeout.Infinite);
}
}

Code Highlights​

  • publishingIntervalMs This refers to the interval between server-side push batches, not the sampling interval; if there are no changes, nothing is sent. NotificationMessage
  • DataChanged Triggered on the Publish thread; WinForms uses Control.BeginInvoke / WPF uses Dispatcher.BeginInvoke to switch back to UI the thread
  • AddMany is 1 RPC; the loop's per-node Add calls are 1 RPC each.
  • sub This is required using; otherwise, the server will retain the subscription until it times sessionTimeoutMs out.

Important Notes​

  • The "Subscribe Publish" model uses server-side push; as long as the client's session remains active, it continues to receive updates without needing to send its own heartbeats.
  • sub This is required using; otherwise, the server will retain the subscription until it sessionTimeoutMs times out.
  • For large-volume (1000+) monitoring, please see Examples of Mass Surveillance.