Large-Scale Monitoring (1000+ Nodes)
On factory floors, it is often necessary to collect data from tens of thousands of tags simultaneously. The key issue isn't SDK performance; it'sHow to Use — Multiple subscriptions / Batch AddMany / Asynchronous consumption. Use C# Example.
Sample Configuration
- Starter Subscription → Multi-Source Subscription Collection
- Complete Data Flow → End-to-end data collection to CSV
Complete Code
using DarraOpcUa_Client;
using System;
using System.Collections.Concurrent;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using System.Threading.Channels;
class Program
{
static async Task Main()
{
// Replace with an on-site PLC industrial PC IP
const string endpoint = "opc.tcp://192.168.1.100:4840";
// 1500 tags, divided into three groups based on update frequency (in actual projects, the format NodeId is determined by the identifiers exported by the project PLC )
var fastTags = Enumerable.Range(1, 100 ).Select(i => $"ns=2;s=Fast.T{i}").ToList(); // 100ms
var mediumTags = Enumerable.Range(1, 800 ).Select(i => $"ns=2;s=Medium.T{i}").ToList(); // 1s
var slowTags = Enumerable.Range(1, 600 ).Select(i => $"ns=2;s=Slow.T{i}").ToList(); // 30s
using var ua = new DarraOpcUa(endpoint);
ua.Connect();
Console.WriteLine($"[OK] 已连接 {endpoint}");
// 1. Asynchronous Consumption Queue
var channel = Channel.CreateUnbounded<DataChangeEventArgs>();
long counter = 0;
_ = Task.Run(async () =>
{
await foreach (var e in channel.Reader.ReadAllAsync())
{
Interlocked.Increment(ref counter);
// Enter database / file / forwarding here
}
});
// 2. Three subscriptions, each different publishingInterval
using var fast = ua.CreateSubscription(100);
using var medium = ua.CreateSubscription(1_000);
using var slow = ua.CreateSubscription(30_000);
EventHandler<DataChangeEventArgs> handler = (s, e) => channel.Writer.TryWrite(e);
fast.DataChanged += handler;
medium.DataChanged += handler;
slow.DataChanged += handler;
// 3. RPC Add N monitoring items at once AddMany
fast .AddMany(fastTags);
medium.AddMany(mediumTags);
slow .AddMany(slowTags);
Console.WriteLine($"已订阅 {fastTags.Count + mediumTags.Count + slowTags.Count} 个节点 (3 个 Subscription)");
// 4. Reported throughput per minute
var timer = new Timer(_ =>
{
var n = Interlocked.Exchange(ref counter, 0);
Console.WriteLine($"{DateTime.Now:HH:mm:ss} 上一分钟 DataChanges = {n} ({n / 60.0:F0}/s)");
}, null, 60_000, 60_000);
Console.WriteLine("\n运行中, Ctrl+C 退出...");
await Task.Delay(Timeout.Infinite);
}
}
Code Highlights
- Multiple subscriptions based on update frequency: High-frequency, slow-rate subscriptions = batching causes delays and loss of real-time performance; low-frequency, fast-rate subscriptions = server resource waste CPU
- When there are a large number of monitoring items, split them across multiple subscriptions and sessions; overloading a single subscription can slow down push notifications.
- Callbacks are
channel.Writer.TryWritequeued in a unified manner; any blocking (DBe.g., file operationsHTTP) slows down publishing, and messages are dropped when the NotificationQueue is full. Interlocked.IncrementCount-Through-Rate Timing Pull ServerStatus Direct
Important Notes
- For long, high-latency pipelines (high bandwidth, high latency)
maxKeepAliveCount,Modify(...)increaselifetimeCount/ to avoid false positives caused by publish jitter. - DataChange Monitoring throughput using a
Interlocked.Incrementcounter is ServerStatus more direct than pulling it at regular intervals.