opcua
The opcua module provides an OPC UA client API for JSH applications.
Client
OPC UA client object.
Creation
new Client(options)- Returns:
Client - Throws
missing client optionsifoptionsis omitted.
Options
| Option | Type | Default | Description |
|---|---|---|---|
| endpoint | string | "" | OPC UA endpoint (opc.tcp://host:port) |
| readRetryInterval | number | 100 (clamped to 100 if lower) | Retry interval for read() in milliseconds |
| messageSecurityMode | number | MessageSecurityMode.None | Security mode. See MessageSecurityMode |
Usage example
| |
close()
Closes the client connection.
Syntax
close()- Returns:
nullon success
read()
Reads values from the given node list.
Syntax
read(readRequest)Parameters
readRequest(object): ReadRequest
Return value
object[]: array of ReadResult
Error behavior:
- Throws an error when
nodesis missing or empty.
write()
Writes one or more node values.
Syntax
write(...writeRequest)Parameters
writeRequest(object, variadic): WriteRequest
Return value
object: WriteResult
Error behavior:
- Throws
missing argumentwhen no argument is provided.
Usage example
| |
browse()
Browses references for one or more nodes.
Syntax
browse(browseRequest)Parameters
browseRequest(object): BrowseRequest
Return value
object[]: array of BrowseResult
Error behavior:
- Throws an error when
nodesis missing or empty.
Usage example
| |
browseNext()
Continues a paginated browse request using continuation points returned by browse() or browseNext().
Syntax
browseNext(browseNextRequest)Parameters
browseNextRequest(object): BrowseNextRequest
Return value
object[]: array of BrowseResult
Error behavior:
- Throws an error when
continuationPointsis missing or empty.
Usage example
| |
children()
Returns direct child references of a node.
Syntax
children(childrenRequest)Parameters
childrenRequest(object): ChildrenRequest
Return value
object[]: array of ChildrenResult
Error behavior:
- Throws an error when
nodeis missing or empty.
Usage example
| |
ReadRequest
| Property | Type | Default | Description |
|---|---|---|---|
| nodes | string[] | List of OPC UA node IDs to read | |
| maxAge | number | 0 | Maximum acceptable cache age in milliseconds |
| timestampsToReturn | number | TimestampsToReturn.Neither | Timestamp return policy |
ReadResult
| Property | Type | Description |
|---|---|---|
| status | number | OPC UA status code (uint32) |
| statusText | string | Status text |
| statusCode | string | Status code name (for example, StatusGood) |
| value | any | Read value |
| type | string | Value type name (for example, Boolean, Int32, Double) |
| sourceTimestamp | number | Unix epoch timestamp in milliseconds |
| serverTimestamp | number | Unix epoch timestamp in milliseconds |
WriteRequest
| Property | Type | Description |
|---|---|---|
| node | string | Target node ID |
| value | any | Value to write |
WriteResult
| Property | Type | Description |
|---|---|---|
| error | Error|null | Write error |
| timestamp | number | Response timestamp (Unix epoch milliseconds) |
| requestHandle | number | OPC UA request handle |
| serviceResult | number | OPC UA service result code |
| stringTable | string[] | OPC UA string table |
| results | number[] | Per-node status code array |
BrowseRequest
| Property | Type | Default | Description |
|---|---|---|---|
| nodes | string[] | List of OPC UA node IDs to browse | |
| browseDirection | number | BrowseDirection.Forward | Browse direction |
| referenceTypeId | string | all references | Reference type node ID to follow |
| includeSubtypes | boolean | true | Whether to include subtypes of referenceTypeId |
| nodeClassMask | number | 0 | Bitmask of node classes to include |
| resultMask | number | BrowseResultMask.All | Bitmask of fields to return |
| requestedMaxReferencesPerNode | number | 0 | Server hint for maximum references returned per node before pagination |
BrowseNextRequest
| Property | Type | Default | Description |
|---|---|---|---|
| continuationPoints | string[] | Base64-encoded continuation points returned from browse() or browseNext() | |
| releaseContinuationPoints | boolean | false | Releases continuation points on the server without requesting more references |
BrowseResult
| Property | Type | Description |
|---|---|---|
| status | number | OPC UA status code (uint32) |
| statusText | string | Status text |
| continuationPoint | string | Base64-encoded continuation point. Empty string when there is no next page |
| references | object[] | Array of BrowseReference |
BrowseReference
| Property | Type | Description |
|---|---|---|
| referenceTypeId | string | Reference type node ID |
| isForward | boolean | Whether the reference direction is forward |
| nodeId | string | Target node ID |
| browseName | string | Browse name |
| displayName | string | Display name |
| nodeClass | number | OPC UA node class value |
| typeDefinition | string | Type definition node ID |
ChildrenRequest
| Property | Type | Description |
|---|---|---|
| node | string | Parent node ID |
| nodeClassMask | number | Bitmask of node classes to include |
ChildrenResult
| Property | Type | Description |
|---|---|---|
| referenceTypeId | string | Reference type node ID |
| isForward | boolean | Whether the reference direction is forward |
| nodeId | string | Child node ID |
| browseName | string | Browse name |
| displayName | string | Display name |
| nodeClass | number | OPC UA node class value |
| typeDefinition | string | Type definition node ID |
BrowseDirection
BrowseDirection.ForwardBrowseDirection.InverseBrowseDirection.BothBrowseDirection.Invalid
NodeClass
NodeClass.UnspecifiedNodeClass.ObjectNodeClass.VariableNodeClass.MethodNodeClass.ObjectTypeNodeClass.VariableTypeNodeClass.ReferenceTypeNodeClass.DataTypeNodeClass.View
BrowseResultMask
BrowseResultMask.NoneBrowseResultMask.ReferenceTypeIdBrowseResultMask.IsForwardBrowseResultMask.NodeClassBrowseResultMask.BrowseNameBrowseResultMask.DisplayNameBrowseResultMask.TypeDefinitionBrowseResultMask.AllBrowseResultMask.ReferenceTypeInfoBrowseResultMask.TargetInfo
MessageSecurityMode
MessageSecurityMode.NoneMessageSecurityMode.SignMessageSecurityMode.SignAndEncryptMessageSecurityMode.Invalid
TimestampsToReturn
TimestampsToReturn.SourceTimestampsToReturn.ServerTimestampsToReturn.BothTimestampsToReturn.NeitherTimestampsToReturn.Invalid
OPCUA Client
This example implements a collector that connects to an OPC UA server, reads system metrics, and stores them in the database.
Flow
- OPC UA integration: use the
opcuamodule to connect toopc.tcp://localhost:4840and read node values such assys_cpu,sys_mem, andload1. - Periodic collection: use
setInterval()to read data every 10 seconds. - Data ingestion: store collected values in the
EXAMPLEtable (name,time,value).
Data collector
Save the script as opcua-client.js, then run it in the background from the JSH terminal.
jsh / > opcua-client
jsh / > ps
┌──────┬──────┬──────┬──────────────────┬────────┐
│ PID │ PPID │ USER │ NAME │ UPTIME │
├──────┼──────┼──────┼──────────────────┼────────┤
│ 1044 │ 1 │ sys │ /opcua-client.js │ 13s │
│ 1045 │ 1025 │ sys │ ps │ 0s │
└──────┴──────┴──────┴──────────────────┴────────┘ opcua-client.js
| |
Simulator server
To test opcua-client.js, you need an OPC UA server that provides system metric nodes.
If you do not have a real environment, use the simulator from the repository below.
It provides sample data such as sys_cpu, sys_mem, load1, load5, and load15
to validate collection and visualization flows.
Follow the instructions in the repository to set it up.
https://github.com/machbase/neo-server/tree/main/jsh/native/opcua/test_server
After starting the simulator, run opcua-client.js and the OPC UA client will connect
and collect data.