Node-OPCUA Server Discovery

2.169.0 · active · verified Sun Apr 19

This module, `node-opcua-server-discovery`, is a core component of the `node-opcua` SDK, a pure Node.js implementation of the OPC UA specification. It provides the functionality for OPC UA servers to register themselves with a Local Discovery Server (LDS) or for clients to discover available OPC UA servers on a network. The package is currently at version 2.169.0 and maintains a highly active release cadence, often publishing updates multiple times a month, focusing on stability, performance, OPC UA 1.05 compliance, and enhanced security features like certificate management. Its key differentiators include being a complete, native JavaScript/TypeScript implementation, offering strong support for modern Node.js environments, and a consistent focus on optimizing transport, memory, and protocol robustness.

Common errors

Warnings

Install

Imports

Quickstart

This code snippet demonstrates how to initialize and start an OPC UA Local Discovery Server (LDS) using `node-opcua-server-discovery`. It includes essential steps for certificate management, defining server information, and graceful shutdown. This LDS can then be used by other OPC UA servers to register themselves, and by clients to discover available services.

import { OPCUADiscoveryServer } from 'node-opcua-server-discovery';
import { CertificateManager } from 'node-opcua-pki';
import path from 'path';
import os from 'os';

async function startDiscoveryServer() {
  const pkiFolder = path.join(os.tmpdir(), "pki_for_discovery");
  const certificateManager = new CertificateManager({ pki: pkiFolder });
  await certificateManager.initialize();

  // Ensure a self-signed certificate is available for the discovery server
  await certificateManager.createSelfSignedCertificate({
    applicationUri: 'urn:MyNodeOPCUADiscoveryServer',
    applicationName: { text: 'My Node-OPCUA Discovery Server', locale: 'en-US' },
    dns: [os.hostname(), 'localhost'],
    ip: [], // Add your server's IP if necessary
    startDate: new Date(),
    endDate: new Date(Date.now() + 365 * 24 * 60 * 60 * 1000) // 1 year validity
  });

  const discoveryServer = new OPCUADiscoveryServer({
    port: 4840,
    serverInfo: {
      applicationUri: 'urn:MyNodeOPCUADiscoveryServer',
      applicationName: { text: 'My Node-OPCUA Discovery Server', locale: 'en-US' },
      productUri: 'https://github.com/node-opcua/node-opcua',
    },
    certificateManager: certificateManager,
    // Optional: Specify server endpoints for the discovery server itself
    // For example, if running behind NAT/Docker with advertised endpoints
    // advertisedEndpoints: ['opc.tcp://my-public-host:4840']
  });

  await discoveryServer.start();
  console.log(`OPC UA Discovery Server started on port ${discoveryServer.port}.`);
  console.log(`PKI folder: ${pkiFolder}`);

  process.on('SIGINT', async () => {
    console.log('Stopping discovery server...');
    await discoveryServer.shutdown();
    console.log('Discovery server stopped.');
    process.exit(0);
  });
}

startDiscoveryServer().catch(err => {
  console.error('Failed to start discovery server:', err);
  process.exit(1);
});

view raw JSON →