OPC UA client and server: discovery, sessions and subscriptions
Part of the OPC UA guide
An OPC UA client is the application that opens the connection and sends requests; an OPC UA server is the application that owns the address space and answers them. The client discovers the server’s endpoints, opens a secure channel, creates a session, and then reads, writes or subscribes to nodes. The server never initiates anything except the notifications a client has asked for through a subscription.
This page is part of the OPC UA guide. It follows a connection from the endpoint URL to the first value, using the service definitions in Part 4 of the OPC UA specification. Node identifiers are covered on the address space page.
Which side is the server?
The server is wherever the data originates or is collected:
- A PLC or motion controller with an OPC UA server in its firmware. The server usually has to be enabled in the controller’s project, and tags have to be marked as accessible before they appear.
- A software server on a PC or industrial computer, such as Kepware or Ignition, which polls devices over their native protocols and exposes the results as nodes.
- A machine’s own controller or HMI, exposing a companion specification model for that machine type.
Clients are the consumers: SCADA and HMI software, MES, historians, analytics tools and edge data collectors. Engineering tools with a node browser are clients as well, and they are the quickest way to confirm a NodeId before it goes into a configuration.
The endpoint URL
A client needs one thing to start: the endpoint URL.
opc.tcp://192.168.1.100:4840/path
| | | |
| | | optional path, used by some software servers
| | port, 4840 by default
| host name or IP address
transport: OPC UA TCP with binary encodingA server normally offers several endpoints at the same address, one for each combination of security mode and security policy it supports. They share the URL and differ in their security settings.
Discovery: GetEndpoints
Before opening a real session, a client calls GetEndpoints on the URL it was given. This call needs no security and no session. The server returns a list of endpoint descriptions, each with:
- the endpoint URL to use for the session,
- the message security mode (None, Sign, or SignAndEncrypt) and the security policy,
- the server’s application instance certificate,
- the user identity token types it accepts (anonymous, user name, certificate),
- a relative security level, so a client can pick the strongest endpoint it supports.
The first item on that list causes a common failure. The URL in the endpoint description is whatever the server is configured to report, and it is often a host name, such as opc.tcp://PLC-LINE3:4840. A client that connected by IP address and has no DNS entry for PLC-LINE3 gets the endpoint list and then fails to connect to the URL it was told to use. The fixes are to add the name to DNS or a hosts file, to configure the server to report its IP address, or to use a client that keeps the original host and takes only the security settings from the description.
A network can also run a Local Discovery Server, a separate service that servers register with and that answers the FindServers call with a list of servers on that machine. It listens on port 4840 too, which is one reason an application server on the same PC may be found on another port.
From TCP connection to session
| Step | Messages | What is agreed |
|---|---|---|
| 1. Transport | Hello, Acknowledge | Protocol version, buffer sizes, maximum message size |
| 2. Secure channel | OpenSecureChannel request and response | Security mode and policy, certificates, nonces for key derivation, channel lifetime |
| 3. Session | CreateSession request and response | Application description, session timeout, a server signature proving it holds its private key |
| 4. Activation | ActivateSession request and response | The user identity, and a client signature proving it holds its private key |
| 5. Work | Browse, Read, Write, CreateSubscription and others | |
| 6. Teardown | CloseSession, CloseSecureChannel |
The secure channel and the session are separate layers. The channel protects the bytes between two applications and is renewed periodically with fresh keys. The session represents one user’s context on the server and holds its subscriptions. If the network drops, a client can open a new secure channel and reactivate the existing session, provided it does so within the session timeout, and its subscriptions continue. A client that instead creates a new session every time leaves the old ones to expire, and on a small server those orphaned sessions can use up the session limit.
The certificate checks in steps 2 to 4 are described on the OPC UA security page.
The services a client uses
Part 4 groups the services into sets. These are the ones a data-collection client calls:
| Service set | Services | Purpose |
|---|---|---|
| Discovery | FindServers, GetEndpoints | Locate servers and their endpoints |
| SecureChannel | OpenSecureChannel, CloseSecureChannel | Set up and renew message protection |
| Session | CreateSession, ActivateSession, CloseSession | Establish the application and user context |
| View | Browse, BrowseNext, TranslateBrowsePathsToNodeIds | Explore the address space |
| Attribute | Read, Write, HistoryRead, HistoryUpdate | Read or write attributes of nodes |
| Method | Call | Run a method node with arguments |
| MonitoredItem | CreateMonitoredItems, ModifyMonitoredItems, DeleteMonitoredItems | Choose what a subscription watches |
| Subscription | CreateSubscription, Publish, Republish, DeleteSubscriptions | Receive changes and events |
Every service takes a list. A single Read request can name many nodes, each with its own attribute ID, and the response carries one result per node with its own status code. One bad NodeId in a batch does not fail the others: the service call returns Good, and the entry for that node carries BadNodeIdUnknown. Clients that check only the overall result miss this.
Read, or subscribe?
A Read returns the current DataValue for each requested attribute. A maxAge parameter tells the server how old a cached value may be; 0 asks it to fetch from the device.
A subscription moves the sampling to the server:
| Parameter | Set on | Meaning |
|---|---|---|
| Publishing interval | Subscription | How often the server sends a notification message |
| Keep-alive count | Subscription | Number of empty publishing intervals before the server sends a keep-alive |
| Lifetime count | Subscription | Number of intervals without a Publish request before the server deletes the subscription |
| Sampling interval | Monitored item | How often the server checks the node |
| Queue size | Monitored item | How many changes are kept between publishes |
| Data change filter | Monitored item | Report on status, value or timestamp change, with an optional absolute or percent deadband |
Notifications are delivered as responses to Publish requests. The client keeps a few Publish requests outstanding, and the server answers one whenever it has something to send. All traffic is therefore initiated by the client over the connection it opened, and no inbound port on the client is needed.
The server may revise every one of these values. A client that asks for a 10 ms sampling interval from a PLC whose fastest rate is 100 ms receives the revised figure in the response and should read it. A percent deadband is calculated against the node’s EURange property, so it applies only to analog items that have one.
What a small server limits
A full server on a PC accepts hundreds of sessions and very large subscriptions. An embedded server in a PLC shares a processor with the control program and is deliberately limited. The limits that are reached first:
- Sessions. Some controllers accept only a handful. An engineering laptop, an HMI and a SCADA system can fill them, and the next client receives
BadTooManySessions. - Monitored items and subscriptions per session.
- Nodes per Read, Write or Browse call. Servers publish these under
Server > ServerCapabilities > OperationLimits, and a well-behaved client splits its requests to fit. - Minimum sampling interval, often tied to the PLC scan or a communication cycle.
Part 7 of the specification defines server profiles in increasing size, from embedded device profiles with a single session and no subscriptions up to the standard server profile. The controller’s manual states which it implements and the actual figures.
A minimal client
Open-source stacks exist for most languages. With the Python asyncua library, connecting and reading one node is a few lines:
import asyncio
from asyncua import Client
async def main():
async with Client("opc.tcp://192.168.1.100:4840") as client:
node = client.get_node("ns=2;s=Temperature")
value = await node.read_value()
print(value)
asyncio.run(main())This connects with no security and an anonymous user, which many servers refuse outside a test bench.
Common connection problems
| Symptom | Likely cause |
|---|---|
| TCP connection refused | Server not enabled in the controller, wrong port, or a firewall on 4840 |
| Endpoints are listed, session fails | Host name in the returned endpoint URL does not resolve from the client |
BadSecurityChecksFailed, BadCertificateUntrusted | The server has not been told to trust the client’s certificate |
BadIdentityTokenRejected, BadUserAccessDenied | Anonymous access disabled, or wrong user name and password |
BadTooManySessions | Session limit reached, often by orphaned sessions from a reconnecting client |
BadNodeIdUnknown for some items only | Per-node error inside a successful Read; check the namespace index |
| Subscription stops delivering after a network drop | Session or subscription lifetime expired before the client reconnected |
The OPC UA client in the ControlCom Edge Server
In a ControlCom deployment the client is the ControlCom Edge Server, which runs on site; ControlCom Connect, the hosted platform, receives the values from it. In the Edge Server configuration editor, OPC UA is one of the types in the Add New Client dialog. The client’s Connection tab takes a Client Name, the OPC Endpoint URL (for example 192.168.1.100:4840 or opc.tcp://192.168.1.100:4840) and an Enable Client switch; new clients start disabled. The Items tab holds one entry per node, and the Settings tab exports and imports the item list as CSV. A saved configuration takes effect when the Edge Server restarts, which can be triggered with Restart in the gateway’s Send command panel.
The OPC UA client documentation walks through the screens. The platform overview shows where the values go once they reach ControlCom Connect.
One Edge Server can hold an OPC UA client for the PLC’s server next to Modbus and BACnet clients for the rest of the site, all feeding the same ControlCom Connect account.