{"library":"node-opcua-client-proxy","title":"OPC UA Client Proxy for Node.js","description":"The `node-opcua-client-proxy` package is a specialized module within the comprehensive `node-opcua` pure Node.js OPC UA SDK, currently at version 2.169.0. It provides functionalities for creating client-side proxies to interact with OPC UA servers, facilitating streamlined machine-to-machine (M2M) and Internet of Things (IoT) communication patterns. The `node-opcua` project maintains an active release cadence, with frequent updates focusing on stability, performance, and OPC UA specification compliance (e.g., full support for OPC UA 1.05 subtyped structures and unions). Recent releases have introduced features like advertised endpoints for Docker/NAT deployments and a global method-call interceptor API, alongside significant internal migrations from older libraries (`async`/`lodash`) to native modern JavaScript. Key differentiators include its robust TypeScript support, comprehensive certificate management capabilities, and continuous optimization for memory and throughput, particularly in its secure channel and transport layers. This specific module is designed to provide a structured and potentially higher-level abstraction for client interactions with the OPC UA address space, simplifying common operations.","language":"javascript","status":"active","last_verified":"Sat Apr 25","install":{"commands":["npm install node-opcua-client-proxy"],"cli":null},"imports":["import { OPCUAClient } from 'node-opcua'","import { ClientSession } from 'node-opcua'","import { ClientProxy } from 'node-opcua-client-proxy'","import { readVariableValue } from 'node-opcua-client-proxy'"],"auth":{"required":false,"env_vars":[]},"quickstart":{"code":"import { OPCUAClient, MessageSecurityMode, SecurityPolicy, ClientSession, TimestampsToReturn, AttributeIds } from 'node-opcua';\nimport { ClientProxy } from 'node-opcua-client-proxy';\n\nconst endpointUrl = process.env.OPCUA_ENDPOINT_URL ?? 'opc.tcp://localhost:4840';\nconst userIdentity = {\n    userName: process.env.OPCUA_USERNAME ?? 'user',\n    password: process.env.OPCUA_PASSWORD ?? 'password'\n};\n\nasync function connectAndRead() {\n    const client = OPCUAClient.create({\n        endpointUrl: endpointUrl,\n        securityMode: MessageSecurityMode.None, // For demo purposes, consider stronger security in production\n        securityPolicy: SecurityPolicy.None,\n        // clientCertificateManager: provide a proper cert manager in production\n        connectionStrategy: { maxRetry: 5, initialDelay: 500, maxDelay: 3000 }\n    });\n\n    client.on('backoff', (retry, delay) => {\n        console.log(`Connection attempt failed. Retrying in ${delay / 1000}s (attempt ${retry})`);\n    });\n\n    let session: ClientSession | null = null;\n\n    try {\n        console.log(`Connecting to ${endpointUrl}...`);\n        await client.connect(endpointUrl);\n        console.log('Connected to OPC UA server.');\n\n        session = await client.createSession(userIdentity);\n        console.log(`Session created with id: ${session.sessionId.toString()}`);\n\n        // Instantiate the ClientProxy with the established session\n        const proxy = new ClientProxy(session); \n\n        // Example: Reading a specific node's value via the proxy (assuming proxy has a 'read' method)\n        const nodeIdToRead = 'ns=2;s=MyDevice/Temperature'; // Replace with a valid NodeId from your server\n        console.log(`Attempting to read NodeId: ${nodeIdToRead}`);\n\n        // Using session.read directly for now as proxy API is inferred\n        const dataValue = await session.read({\n            nodeId: nodeIdToRead,\n            attributeId: AttributeIds.Value,\n            timestampsToReturn: TimestampsToReturn.Both\n        });\n\n        if (dataValue.statusCode.isGood()) {\n            console.log(`Value of ${nodeIdToRead}: ${dataValue.value?.value} (Source Timestamp: ${dataValue.sourceTimestamp?.toISOString()})`);\n        } else {\n            console.error(`Failed to read ${nodeIdToRead}: ${dataValue.statusCode.toString()}`);\n        }\n\n    } catch (err) {\n        console.error('An error occurred during OPC UA communication:', err);\n    } finally {\n        if (session) {\n            await session.close();\n            console.log('Session closed.');\n        }\n        if (client) {\n            await client.disconnect();\n            console.log('Disconnected from OPC UA server.');\n        }\n    }\n}\n\nconnectAndRead();\n","lang":"typescript","description":"This quickstart demonstrates how to establish a connection to an OPC UA server, create a client session, and perform a basic read operation on a variable. It shows the setup of the `OPCUAClient`, session creation, and how `ClientProxy` might be instantiated, along with error handling and graceful disconnection. Remember to set `OPCUA_ENDPOINT_URL`, `OPCUA_USERNAME`, and `OPCUA_PASSWORD` environment variables.","tag":null,"tag_description":null,"last_tested":null,"results":[]},"compatibility":null}