OPC UA Client for Node.js

2.169.0 · active · verified Tue Apr 21

node-opcua-client is the client-side module of a comprehensive OPC UA SDK written entirely in JavaScript and Node.js, providing robust connectivity to OPC UA servers. Currently at version 2.169.0, the library exhibits a rapid and consistent release cadence, often featuring multiple patch and minor releases per month to introduce new features, performance enhancements, and bug fixes. Key differentiators include its pure Node.js implementation, broad support for various OPC UA specifications (including OPC UA 1.05 compliance), and a strong emphasis on performance through optimizations in data type handling, transport, and secure channel operations. The project maintains an open-source MIT license but offers commercial support, advanced documentation, and value-added extensions through Sterfive SAS, catering to professional and industrial applications.

Common errors

Warnings

Install

Imports

Quickstart

Demonstrates how to connect to an OPC UA server, create a session, and read a value from a well-known NodeId (Server_ServerStatus_CurrentTime).

import {
    OPCUAClient,
    MessageSecurityMode,
    SecurityPolicy,
    AttributeIds
} from "node-opcua-client";

async function main() {
    // Use process.env.OPCUA_SERVER_ENDPOINT_URL for flexibility, fallback to localhost
    const endpointUrl = process.env.OPCUA_SERVER_ENDPOINT_URL ?? "opc.tcp://localhost:4840";
    
    // Create an OPC UA client instance
    const client = OPCUAClient.create({
        endpointUrl: endpointUrl,
        securityMode: MessageSecurityMode.None, // No security for simplicity (dev/test)
        securityPolicy: SecurityPolicy.None, // No security policy
        connectionStrategy: {
            maxRetry: 10,
            initialDelay: 2000,
            maxDelay: 10 * 1000
        }
    });

    try {
        // Connect the client to the OPC UA server
        await client.connect(endpointUrl);
        console.log("Client connected to endpoint:", endpointUrl);

        // Create a session with the server
        const session = await client.createSession();
        console.log("Session created with ID:", session.sessionId.toString());

        // Define the NodeId to read (e.g., Server_ServerStatus_CurrentTime)
        const nodeIdToRead = "ns=0;i=2258"; 
        
        // Read the value of the specified node
        const dataValue = await session.read({
            nodeId: nodeIdToRead,
            attributeId: AttributeIds.Value
        });
        console.log(`Value of ${nodeIdToRead}: ${dataValue.value.value}`);

        // Close the session and disconnect the client
        await session.close();
        console.log("Session closed.");

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

    } catch (err) {
        console.error("An error occurred during OPC UA operations:", err);
        process.exit(1);
    }
}

main();

view raw JSON →