Skip to main content

Namespaces — s.NamespacesRef()

OPC UA A node consists NamespaceIndex + Identifier of NodeId, but NamespaceIndex Unstable — A server restart may cause changes. Production code must use NamespaceUri reverse lookup indexes; do not hard-code them ns=2.

Prerequisite Reading
  • Standard = ns0; please refer to the node constants WellKnownNodes.
  • For information on node and Browse path resolution, please refer to Nodes.

Namespaces Public Members​

Session::NamespacesRef() returns Namespaces&; at Connect() time it is automatically loaded from the server's NamespaceArray (i=2255) (if loading fails, only a warning is logged; the connection is not blocked).

The Namespaces value retrieval interface: all are member functions, no fields:

CategoryPropertyTypeAccessDescription
MethodsCount()intRNumber of namespaces
operator[](int i)std::stringRIndex-based access; returns the URI corresponding to the given Index; returns an empty string if out of bounds
IndexOf(uri)intRLookup URI → Index; returns -1 if not found
Reload()StatusRProactively re-read the server's NamespaceArray (when the Server changes dynamically)

Common Uses​

Reverse Lookup: Index Spelling NodeId​

#include <darra/opcua/client.hpp>
#include <stdexcept>
#include <string>

s.Connect();
int idx = s.NamespacesRef().IndexOf("urn:my-company:plc1");
if (idx < 0) {
throw std::runtime_error("Server does not have my namespace");
}

std::string node_id = "ns=" + std::to_string(idx) + ";s=Temperature";
auto dv = s.Read(node_id);

List all namespaces (debug)​

#include <darra/opcua/client.hpp>
#include <iostream>

auto& nss = s.NamespacesRef();
for (int i = 0; i < nss.Count(); ++i) {
std::cout << " [" << i << "] " << nss[i] << "\n";
}

Output example:

[0] http://opcfoundation.org/UA/
[1] urn:server-host
[2] urn:my-company:plc1
[3] http://my-company.com/UA/

Dynamic Namespace Changes​

Some servers register their namespaces only after they start up (plug-in style), so Connect does not receive the complete NamespaceArray set. In this case, call the re-read Reload() method:

s.NamespacesRef().Reload();
int idx = s.NamespacesRef().IndexOf("urn:plugin:loaded-after-startup");

Standard namespace (ns=0)​

OPC Foundation It is specified that Index 0 is always http://opcfoundation.org/UA/, and all standard nodes have ns=0:

NodeNodeIdNote
Rooti=84Root
Objectsi=85Business Data Root
Typesi=86Type Definition Root
Serveri=2253The Server Object Itself
ServerStatusi=2256Server Status Structure
ServerStatus.CurrentTimei=2258Current server time
NamespaceArrayi=2255Namespace Array
ServerArrayi=2254Server-side array

For more information, see WellKnownNodes.

Next Step​