Swiftpack.co - gerardogrisolini/ZenOPCUA as Swift Package

Swiftpack.co is a collection of thousands of indexed Swift packages. Search packages.
OPCUA Client built on top of Apple's SwiftNIO
.package(url: "https://github.com/gerardogrisolini/ZenOPCUA.git", from: "2.0.11")


Getting Started

Adding a dependencies clause to your Package.swift

dependencies: [
    .package(url: "https://github.com/gerardogrisolini/ZenOPCUA.git", from: "1.0.0")

Make client

import NIO
import ZenOPCUA

let eventLoopGroup = MultiThreadedEventLoopGroup(numberOfThreads: System.coreCount)
defer { try! eventLoopGroup.syncShutdownGracefully() }

let opcua = ZenOPCUA(
    endpoint: "opc.tcp://MacBook-Pro-di-Gerardo.local:53530/OPCUA/SimulationServer",
    reconnect: false,
    eventLoopGroup: eventLoopGroup

opcua.onHandlerRemoved = {
    print("OPCUA Client disconnected")
opcua.onErrorCaught = { error in

Connect to server

try opcua.connect().wait()


let items = try opcua.browse().wait()
for item in items {
    item.references.forEach { ref in
        print("\(ref.displayName.text): \(ref.nodeId)")

Read value

let reads = [ReadValue(nodeId: NodeIdNumeric(nameSpace: 0, identifier: 2258))]
let readed = try opcua.read(nodes: reads).wait()
print(readed.first?.variant.value ?? "nil")

Write value

let writes: [WriteValue] = [
        nodeId: NodeIdString(nameSpace: 5, identifier: "MyLevel"),
        value: DataValue(variant: Variant(value: Double(21.0)))
let writed = try opcua.write(nodes: writes).wait()

Subscribe and MonitoredItems

let items: [ReadValue] = [
    ReadValue(nodeId: NodeIdNumeric(nameSpace: 0, identifier: 2258), monitoredId: 1),
    ReadValue(nodeId: NodeIdString(nameSpace: 3, identifier: "Counter"), monitoredId: 2),
    ReadValue(nodeId: NodeIdString(nameSpace: 5, identifier: "MyLevel"), monitoredId: 3)

opcua.onDataChanged = { data in
    data.forEach { dataChange in
        dataChange.dataChangeNotification.monitoredItems.forEach { item in
            if let node = items.first(where: { $0.monitoredId == item.monitoredId }) {
                print("\(node.nodeId): \(item.value.variant.value)")

let subId = try opcua.createSubscription(requestedPubliscingInterval: 500).wait()
let results = try opcua.createMonitoredItems(subscriptionId: subId, itemsToCreate: items).wait()
results.forEach { result in
    print("createMonitoredItem: \(result.monitoredItemId) = \(result.statusCode)")

let deleted = try opcua.deleteSubscriptions(subscriptionIds: [subId]).wait()
deleted.forEach { result in
    print("deleteSubscription: \(result)")

Disconnect client

try OPCUA.disconnect().wait()


Stars: 2
Last commit: 2 weeks ago

Ad: Job Offers

iOS Software Engineer @ Perry Street Software
Perry Street Software is Jack’d and SCRUFF. We are two of the world’s largest gay, bi, trans and queer social dating apps on iOS and Android. Our brands reach more than 20 million members worldwide so members can connect, meet and express themselves on a platform that prioritizes privacy and security. We invest heavily into SwiftUI and using Swift Packages to modularize the codebase.

Submit a free job ad (while I'm testing this). The analytics numbers for this website are here.

Swiftpack is being maintained by Petr Pavlik | @ptrpavlik | @swiftpackco | API | Analytics