Node-OPCUA Server Discovery
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
-
Error: Certificate validation failed for endpoint 'opc.tcp://localhost:4840'
cause The client (or discovery server) does not trust the server's (or discovery server's) certificate, or the certificate's application URI/DNS name does not match the endpoint URL.fixEnsure the server's certificate is in the client's trusted certificates store, and vice versa. Verify that the `applicationUri` in the certificate matches the server's configured URI and that DNS names/IPs in the certificate match the endpoint URL used for connection. Regenerate certificates if necessary, ensuring proper hostnames/IPs. -
Error: read ECONNRESET
cause This typically indicates a network connectivity issue or a premature termination of the connection, often due to firewalls, incorrect endpoint URLs, or server-side certificate validation failures leading to an immediate disconnect.fixCheck network connectivity between client and server, including firewall rules on both ends (port 4840 for LDS, default 4840 or custom for OPC UA servers). Verify the endpoint URL is correct and accessible. Review server logs for errors related to connection establishment or certificate rejection. -
TypeError: Cannot read properties of undefined (reading 'start')
cause The `OPCUADiscoveryServer` instance was not properly initialized before attempting to call its `start()` method, likely due to missing or incorrect constructor arguments.fixEnsure the `OPCUADiscoveryServer` constructor is called with all required options, including `port`, `serverInfo`, and `certificateManager`. Double-check the types and availability of these properties before passing them to the constructor.
Warnings
- breaking Version `2.168.0` significantly refactored core packages, migrating from `async` and `lodash` to native modern JavaScript patterns. While primarily internal, changes to the 'more robust typed event system' may introduce breaking changes to existing event listeners or custom extensions relying on previous internal event structures.
- gotcha Beginning with `v2.165.0`, `node-opcua` introduced enhanced 'Advertised Endpoints' support for Docker, NAT, or proxy deployments. Incorrectly configuring advertised endpoints can lead to discovery failures where clients cannot connect to the server's actual network location.
- gotcha Changes in OPC UA 1.05 compliance and DataType handling (v2.163.0) can impact applications dealing with complex or custom information models. While providing performance improvements, previous non-compliant type handling might now be stricter.
- gotcha The `node-opcua` ecosystem relies heavily on X.509 certificates for secure communication. Misconfiguration or missing certificates (e.g., self-signed certificates for the discovery server itself, or trusted certificates for registering servers) are common sources of connection errors.
Install
-
npm install node-opcua-server-discovery -
yarn add node-opcua-server-discovery -
pnpm add node-opcua-server-discovery
Imports
- OPCUADiscoveryServer
const OPCUADiscoveryServer = require('node-opcua-server-discovery');import { OPCUADiscoveryServer } from 'node-opcua-server-discovery'; - DiscoveryServerOptions
import type { DiscoveryServerOptions } from 'node-opcua-server-discovery'; - CertificateManager
import { CertificateManager } from 'node-opcua-pki';
Quickstart
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);
});