Node-OPCUA Server

2.169.0 · active · verified Sun Apr 19

The `node-opcua-server` package provides a pure JavaScript and TypeScript implementation of an OPC UA (Open Platform Communications Unified Architecture) server stack, designed for Node.js environments. It facilitates secure and reliable data exchange in industrial automation, M2M, and IoT applications. The project is under active development, with frequent minor and patch releases, currently stable at version 2.169.0. Key differentiators include its complete native Node.js implementation, comprehensive support for OPC UA standards including full OPC UA 1.05 compliance (featuring subtyped structures, unions, and optimized data type handling), and a robust, typed event system. It ships with full TypeScript type definitions, enhancing developer experience. While the core server is open-source (MIT licensed), Sterfive SAS offers commercial support and value-added companion modules for advanced features like PubSub, web proxying, optimized clients, and Global Discovery Services.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart code demonstrates how to create a basic OPC UA server, define a custom address space with a dynamic temperature variable, and handle graceful shutdown. It uses `node-opcua` for all core functionalities, including server instantiation, node creation, and data type handling.

import { OPCUAServer, DataType, Variant, StatusCodes, NodeId } from 'node-opcua';

const server = new OPCUAServer({
    port: 4840, // default OPC UA port
    resourcePath: '/UA/MyAwesomeServer',
    buildInfo: {
        productName: 'My Awesome OPCUA Server',
        buildNumber: '7658',
        buildDate: new Date(2026, 3, 19)
    }
});

async function startServer() {
    await server.start();
    console.log('Server started and listening on', server.endpoints[0].endpointUrl);
    console.log('Press Ctrl+C to stop the server.');

    server.on('post_initialize', () => {
        // Define the address space
        const addressSpace = server.engine.addressSpace;
        if (!addressSpace) {
            console.error('Address space not available.');
            return;
        }

        const namespace = addressSpace.get = addressSpace.registerNamespace('http://mynamespace.com/UA/MyAwesomeServer/');
        const device = namespace.addObject({
            organizedBy: addressSpace.rootFolder.objects,
            browseName: 'MyDevice'
        });

        let temperature = 25.0;
        namespace.addVariable({
            componentOf: device,
            nodeId: 's=Temperature',
            browseName: 'Temperature',
            dataType: DataType.Double,
            value: {
                get: () => new Variant({
                    dataType: DataType.Double,
                    value: temperature
                })
            }
        });

        // Simulate temperature changes
        setInterval(() => {
            temperature = 20 + 10 * Math.sin(Date.now() / 10000); // Oscillation between 10 and 30
        }, 1000);

        console.log('Address space initialized with a Temperature variable.');
    });

    process.on('SIGINT', async () => {
        console.log('Caught interrupt signal, shutting down server...');
        await server.shutdown();
        console.log('Server shut down.');
        process.exit(0);
    });
}

startServer().catch(console.error);

view raw JSON →