OPC UA Client
Learn how to connect to an OPC UA server using the Heisenware OPC UA integration.
The OpcuaClient
class provides a high-level interface for communicating with OPC UA servers. It simplifies complex operations like establishing secure connections, Browse the server's address space, reading and writing variables, calling methods, monitoring data for changes, and handling file transfers.
An instance of the client must be created to manage the connection state and session with a server. For secure connections, certificates must be generated first using the createCertificates
function.
Video tutorial
Watch the video to learn how to connect to an OPC UA Server and read, record and visualize OPC UA data from that server within Heisenware.
createCertificates
This is a static utility function that should be run once to generate the necessary client and user certificates for establishing secure connections. It creates a local Public Key Infrastructure (PKI) folder containing the self-signed certificates and private keys required for authentication and encryption.
Parameters
None.
Example
# (No arguments needed)
Output
Returns true
upon successful creation of the certificates.
create
This function creates an instance of the OPC UA client. It can be configured for either an unsecured connection or a secure connection using various security policies.
Parameters
options: An object to configure the client's security settings. For an unsecured connection, leave this argument empty.
securityMode: The security mode, can be
None
,Sign
, orSignAndEncrypt
.securityPolicy: The security policy, such as
Basic256Sha256
,Aes128_Sha256_RsaOaep
, etc.
Example: Unsecured Connection
To create a client for an unsecured connection, call create
with no arguments.
# (No arguments needed)
Example: Secure Connection
This creates a client configured for a secure connection using the certificates generated by createCertificates
.
# options
securityMode: SignAndEncrypt
securityPolicy: Basic256Sha256
connect
Establishes a connection to an OPC UA server endpoint and creates a session. You can connect anonymously or provide user credentials.
Parameters
endpointUrl: The full endpoint URL of the OPC UA server (e.g.,
opc.tcp://server.com:4840
).userIdentity: An object containing user credentials.
username: The user's name for password-based authentication.
password: The user's password.
useDefaultUserCertificate: Set to
true
to authenticate using the default user certificate generated bycreateCertificates
.
Example: Anonymous Login
# endpointUrl
opc.tcp://milo.digitalpetri.com:62541/milo
Example: Username and Password Login
# endpointUrl
opc.tcp://my-secure-server.com:4840
# userIdentity
username: myuser
password: mysecretpassword
Example: Certificate-Based Login
This authenticates using the default user certificate.
# endpointUrl
opc.tcp://my-secure-server.com:4840
# userIdentity
useDefaultUserCertificate: true
Output
Returns true
if the connection and session creation are successful.
disconnect
Closes the active session and disconnects from the OPC UA server.
Parameters
None.
Output
Returns true
on a successful disconnection.
isConnected
Checks if the client has a valid and active channel with the server.
Parameters
None.
Output
Returns true
if the client is connected, otherwise false
.
browse
This is the generic, non-recursive browse function. It can start from any node address on the server. The more specific functions like browseObjects
are convenience wrappers around this one.
Parameters
address: The
nodeId
or a full browse path (e.g.,/0:Objects/2:Demo
) from which to start Browse.
Example
# address
/0:Objects/2:Demo
Output
An array of objects, where each object represents a found node and contains its browseName
, nodeId
, and nodeClass
.
browseObjects
Performs a non-recursive browse of the server's Objects
folder. You can specify a deeper path to start from.
Parameters
path: The browse path, starting from within the
Objects
folder. Path items are separated by/
, and each item usesnamespaceIndex:BrowseName
format (e.g.,2:Demo/2:Dynamic
).
Example
# path
2:Demo/2:Dynamic/2:Scalar
Output
An array of objects, where each object represents a found node and contains its browseName
, nodeId
, and nodeClass
.
browseTypes
Performs a non-recursive browse of the server's Types
folder. This is useful for exploring the server's data type hierarchy.
Parameters
path: An optional browse path to start from within the
Types
folder. If omitted, it browses from the root of theTypes
folder.
Example
# path
0:BaseObjectType/0:FolderType
Output
An array of objects, where each object represents a found data type node.
browseViews
Performs a non-recursive browse of the server's Views
folder. Views are predefined, filtered collections of nodes.
Parameters
path: An optional browse path to start from within the
Views
folder. If omitted, it browses from the root of theViews
folder.
Example
# path
0:Server
Output
An array of objects, where each object represents a found node within the specified view.
readNode
Reads all attributes of a specific OPC UA node, returning a detailed data structure. This is more comprehensive than readVariable
, which only fetches the node's value.
Parameters
address: The
nodeId
or a valid browse path of the node to read.
Example: Read using nodeId
# address
ns=2;s=Demo.Dynamic.Int32
Example: Read using Browse Path
# address
/0:Objects/2:Demo/2:Dynamic/2:Int32
Output
A JSON object representing the node's DataValue
. This includes the value, status code, and server/source timestamps. For example:
JSON
{
"value": {
"dataType": "Int32",
"value": 12345
},
"statusCode": {
"name": "Good",
"value": 0
},
"serverTimestamp": "2025-07-11T08:52:34.000Z",
"sourceTimestamp": "2025-07-11T08:52:34.000Z"
}
readVariable
Reads the value of a single variable from the server. This is a convenience function that extracts just the value from the node's data.
Parameters
address: The address of the variable, which can be its
nodeId
(recommended) or a full browse path.
Example: Read using nodeId
# address
ns=2;s=Demo.Dynamic.Int32
Example: Read using Browse Path
# address
/0:Objects/2:Demo/2:Dynamic/2:Int32
Output
The raw value of the variable (e.g., a number, a string, a boolean, or an array).
writeVariable
Writes a new value to a variable on the server. The function automatically handles the conversion from a standard JavaScript type to the required OPC UA data type.
Parameters
address: The
nodeId
or browse path of the variable to write to.value: The new value to set.
Example
# address
ns=2;s=Demo.Dynamic.Int32
# value
42
Output
The function returns null
on a successful write or throws an error on failure.
callMethod
Invokes a method on an OPC UA object on the server. Input arguments are automatically converted to the correct data types as defined by the method on the server.
Parameters
methodAddress: The
nodeId
or browse path of the method to call.inputValues: An array of values to pass as input arguments to the method.
Example
This example calls a method that takes two input arguments.
# methodAddress
ns=2;s=Demo.Methods.Multiply
# inputValues
[ 5, 10 ]
Output
An array containing the output arguments returned by the method call.
monitorNode
Subscribes to changes for an entire OPC UA node. The provided callback function is triggered with the full node data object (including value, status, and timestamps) whenever a change is detected by the server.
Parameters
address: The
nodeId
or browse path of the node to monitor.listener: The callback function that will receive the full data object on change.
options: An object to configure the subscription.
samplingInterval: How often (in milliseconds) the server should check for changes. Default is
1000
.
Example
# address
ns=2;s=Demo.Dynamic.UInt16
# listener
<callback>
# options
samplingInterval: 5000
Output
Returns a unique nodeId
string that acts as an identifier for this monitored item, which is needed for stopMonitor
.
monitorVariable
Subscribes to value changes for a specific variable. A callback function is triggered every time the server reports a new value.
Parameters
address: The
nodeId
or browse path of the variable to monitor.listener: The callback function that will receive the new value.
options: An object to configure the subscription.
samplingInterval: How often (in milliseconds) the server should check the variable for changes. Default is
1000
.
Example
# address
ns=2;s=Demo.Dynamic.UInt16
# listener
<callback>
# options
samplingInterval: 5000
Output
Returns a unique nodeId
string that acts as an identifier for this monitored item. This ID is used to stop the monitoring later.
stopMonitor
Terminates an active subscription for a monitored item.
Parameters
nodeId: The identifier string that was returned by a previous call to
monitorNode
ormonitorVariable
.
Example
# nodeId
ns=2;s=Demo.Dynamic.UInt16
Output
Returns true
if the subscription was terminated successfully.
browseDirectory
Recursively browses a file-system-like directory structure exposed by an OPC UA server.
Parameters
address: The
nodeId
or browse path of the starting directory node.
Output
A nested array of objects representing the directory structure.
readFile
Reads the entire content of a file exposed by the OPC UA server's file transfer feature.
Parameters
address: The
nodeId
or browse path of the file node on the server.
Example
# address
ns=2;s=Demo.Files.TextFile
Output
The complete content of the file, encoded as a base64 string.
writeFile
Creates a new file in a specified folder on the server and uploads content to it.
Parameters
folderAddress: The
nodeId
or browse path of the folder where the file will be created.newFileName: The name for the new file.
pathOrBase64: The content to upload, either as a base64 string or a local file system path.
Example
# folderAddress
ns=2;s=Demo.Files
# newFileName
report.txt
# pathOrBase64
SGVsbG8sIFdvcmxkIQ==
Output
The nodeId
of the newly created file on the server.
deleteFile
Deletes a file from a folder on the OPC UA server.
Parameters
folderAddress: The
nodeId
or browse path of the folder containing the file.fileName: The name of the file to delete.
Example
# folderAddress
ns=2;s=Demo.Files
# fileName
report.txt
Output
Returns true
if the file was deleted successfully.
listenToEvents
Registers a callback function to receive important lifecycle events from the client, such as Connected
, Connection Lost
, Session Closed
, etc. This is useful for monitoring the health of the connection.
Parameters
listener: The callback function to receive event strings.
Output
Returns true
.
Last updated