node-opcua - Pure Node.js OPC UA SDK

2.169.0 · active · verified Sun Apr 19

node-opcua is a comprehensive, pure JavaScript/TypeScript implementation of the OPC Unified Architecture (OPC UA) stack, designed for Node.js environments. It enables the creation of OPC UA clients, servers, and related tools, facilitating machine-to-machine (M2M) and Industrial IoT (IIoT) communication. The package is actively maintained, with frequent minor and patch releases, currently stable at version 2.169.0, focusing on performance, memory optimization, and OPC UA 1.05 compliance. Key differentiators include its full adherence to the OPC UA specification, a robust architecture for handling large information models, and enterprise-grade support and value-added extensions provided by Sterfive SAS, which also sponsors its long-term development. It supports modern Node.js versions (>=18) and ships with TypeScript types for improved developer experience.

Common errors

Warnings

Install

Imports

Quickstart

This example demonstrates how to set up a basic OPC UA server, expose a dynamically updating integer variable, and then connect to it with an OPC UA client to browse its address space and read the variable's value. It uses anonymous authentication and no message security for simplicity. For production use, secure communication with certificates and user authentication is highly recommended.

import { OPCUAServer, OPCUAClient, MessageSecurityMode, SecurityPolicy, DataType, Variant, AttributeIds } from "node-opcua";
import { resolve } from "path";

// --- Server Setup ---
async function startServer() {
    // Ensure a 'certificates' directory exists in your project root
    // For production, manage certificates securely (e.g., via GDS)
    // This example uses anonymous authentication for simplicity.
    const server = new OPCUAServer({
        port: 4334,
        resourcePath: "/UA/MySampleServer",
        buildInfo: {
            productName: "MySampleServer",
            buildNumber: "1234",
            buildDate: new Date()
        },
        serverInfo: {
            applicationUri: "urn:MySampleServer",
            productUri: "urn:MySampleServer",
            applicationName: { text: "MySampleServer", locale: "en" }
        },
        securityModes: [MessageSecurityMode.None],
        securityPolicies: [SecurityPolicy.None],
        allowAnonymous: true,
        // Certificate files are needed even with securityMode.None in many setups
        // The toolkit generates default ones in C:\Users\user\AppData\Roaming\node-opcua-default-nodejs
        // if not provided, but explicitly setting them is good practice for production.
        // For simplicity in this quickstart, we assume default generation or manual placement.
    });

    await server.start();
    console.log(`Server started on port ${server.port}`);
    console.log(`Server endpointUrl: ${server.endpointUrl}`);

    server.engine.addressSpace?.getFolder("ObjectsFolder")?.addVariable({
        nodeId: "ns=1;s=MyDynamicVariable",
        browseName: "MyDynamicVariable",
        dataType: DataType.Int32,
        value: {
            get: () => new Variant({ dataType: DataType.Int32, value: Math.floor(Math.random() * 100) })
        }
    });

    return server;
}

// --- Client Setup ---
async function startClient(endpointUrl: string) {
    const client = OPCUAClient.create({
        endpointUrl,
        securityMode: MessageSecurityMode.None,
        securityPolicy: SecurityPolicy.None,
        connectionStrategy: {
            maxRetry: 0, // No retries for quickstart simplicity
            initialDelay: 100
        }
    });

    try {
        await client.connect(endpointUrl);
        console.log("Client connected to server.");

        const session = await client.createSession();
        console.log("Client session created.");

        // Browse the root folder
        const browseResult = await session.browse("RootFolder");
        console.log("RootFolder children:");
        for (const reference of browseResult.references || []) {
            console.log(`  - ${reference.browseName.toString()}`);
        }

        // Read the value of MyDynamicVariable
        const readValue = await session.read({
            nodeId: "ns=1;s=MyDynamicVariable",
            attributeId: AttributeIds.Value
        });
        console.log(`Value of MyDynamicVariable: ${readValue.value?.value}`);

        await session.close();
        await client.disconnect();
        console.log("Client disconnected.");

    } catch (err) {
        console.error("Client error:", err);
    }
}

// --- Main Execution ---
(async () => {
    let server: OPCUAServer | null = null;
    try {
        server = await startServer();
        // Give server a moment to fully initialize
        await new Promise(resolve => setTimeout(resolve, 1000));
        await startClient(server.endpointUrl!); // Use the server's dynamically assigned endpoint
    } catch (error) {
        console.error("Error during quickstart execution:", error);
    } finally {
        if (server) {
            console.log("Shutting down server...");
            await server.shutdown();
            console.log("Server shut down.");
        }
    }
})();

view raw JSON →