{"id":14739,"library":"node-opcua-server-discovery","title":"Node-OPCUA Server Discovery","description":"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.","status":"active","version":"2.169.0","language":"javascript","source_language":"en","source_url":"git://github.com/node-opcua/node-opcua","tags":["javascript","OPCUA","opcua","m2m","iot","opc ua","internet of things","typescript"],"install":[{"cmd":"npm install node-opcua-server-discovery","lang":"bash","label":"npm"},{"cmd":"yarn add node-opcua-server-discovery","lang":"bash","label":"yarn"},{"cmd":"pnpm add node-opcua-server-discovery","lang":"bash","label":"pnpm"}],"dependencies":[],"imports":[{"note":"The `node-opcua` ecosystem primarily uses ES Modules and TypeScript. While CommonJS might work in some older Node.js contexts, ESM is the recommended and best-supported import method.","wrong":"const OPCUADiscoveryServer = require('node-opcua-server-discovery');","symbol":"OPCUADiscoveryServer","correct":"import { OPCUADiscoveryServer } from 'node-opcua-server-discovery';"},{"note":"Import types separately using `import type` for clarity and to ensure type-only imports are correctly handled by TypeScript compilers and bundlers.","symbol":"DiscoveryServerOptions","correct":"import type { DiscoveryServerOptions } from 'node-opcua-server-discovery';"},{"note":"While not directly from `server-discovery`, certificate management is crucial for OPC UA security. `node-opcua-pki` is a closely related and often required dependency for configuring secure discovery services.","symbol":"CertificateManager","correct":"import { CertificateManager } from 'node-opcua-pki';"}],"quickstart":{"code":"import { OPCUADiscoveryServer } from 'node-opcua-server-discovery';\nimport { CertificateManager } from 'node-opcua-pki';\nimport path from 'path';\nimport os from 'os';\n\nasync function startDiscoveryServer() {\n  const pkiFolder = path.join(os.tmpdir(), \"pki_for_discovery\");\n  const certificateManager = new CertificateManager({ pki: pkiFolder });\n  await certificateManager.initialize();\n\n  // Ensure a self-signed certificate is available for the discovery server\n  await certificateManager.createSelfSignedCertificate({\n    applicationUri: 'urn:MyNodeOPCUADiscoveryServer',\n    applicationName: { text: 'My Node-OPCUA Discovery Server', locale: 'en-US' },\n    dns: [os.hostname(), 'localhost'],\n    ip: [], // Add your server's IP if necessary\n    startDate: new Date(),\n    endDate: new Date(Date.now() + 365 * 24 * 60 * 60 * 1000) // 1 year validity\n  });\n\n  const discoveryServer = new OPCUADiscoveryServer({\n    port: 4840,\n    serverInfo: {\n      applicationUri: 'urn:MyNodeOPCUADiscoveryServer',\n      applicationName: { text: 'My Node-OPCUA Discovery Server', locale: 'en-US' },\n      productUri: 'https://github.com/node-opcua/node-opcua',\n    },\n    certificateManager: certificateManager,\n    // Optional: Specify server endpoints for the discovery server itself\n    // For example, if running behind NAT/Docker with advertised endpoints\n    // advertisedEndpoints: ['opc.tcp://my-public-host:4840']\n  });\n\n  await discoveryServer.start();\n  console.log(`OPC UA Discovery Server started on port ${discoveryServer.port}.`);\n  console.log(`PKI folder: ${pkiFolder}`);\n\n  process.on('SIGINT', async () => {\n    console.log('Stopping discovery server...');\n    await discoveryServer.shutdown();\n    console.log('Discovery server stopped.');\n    process.exit(0);\n  });\n}\n\nstartDiscoveryServer().catch(err => {\n  console.error('Failed to start discovery server:', err);\n  process.exit(1);\n});","lang":"typescript","description":"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."},"warnings":[{"fix":"Review and update any custom event handlers or modules that might be interacting with the internal event system or relying on `async`/`lodash` utilities directly. Consult `node-opcua`'s main documentation for updated event patterns.","message":"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.","severity":"breaking","affected_versions":">=2.168.0"},{"fix":"For servers running behind network address translation (NAT), Docker port-mapping, or reverse proxies, ensure the `advertisedEndpoints` option is correctly configured with the publicly accessible endpoint URLs. Validate these URLs from the client's perspective.","message":"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.","severity":"gotcha","affected_versions":">=2.165.0"},{"fix":"If your application uses custom OPC UA DataTypes or relies on specific encoding/decoding behaviors, thoroughly test against `v2.163.0` and later versions. Ensure your information models strictly adhere to OPC UA 1.05 specifications. Re-evaluate any custom extension object or variant handling.","message":"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.","severity":"gotcha","affected_versions":">=2.163.0"},{"fix":"Always ensure your `OPCUADiscoveryServer` instance has a valid `CertificateManager` configured with appropriate certificates. For client-server communication via discovery, ensure that both the discovery server and the registered OPC UA servers have trusted certificates configured on both sides.","message":"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.","severity":"gotcha","affected_versions":">=2.0.0"}],"env_vars":null,"last_verified":"2026-04-19T00:00:00.000Z","next_check":"2026-07-18T00:00:00.000Z","problems":[{"fix":"Ensure 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.","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.","error":"Error: Certificate validation failed for endpoint 'opc.tcp://localhost:4840'"},{"fix":"Check 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.","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.","error":"Error: read ECONNRESET"},{"fix":"Ensure 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.","cause":"The `OPCUADiscoveryServer` instance was not properly initialized before attempting to call its `start()` method, likely due to missing or incorrect constructor arguments.","error":"TypeError: Cannot read properties of undefined (reading 'start')"}],"ecosystem":"npm"}