Connections
Getting Started
Introduction InstallationUsage
Usage Connections Session-manager Logging-caching SecurityReference
Testing Examples Auto-publish EventsConnections
Named Connections
Define connections in config/opcua.php:
'connections' => [
'default' => [
'endpoint' => env('OPCUA_ENDPOINT', 'opc.tcp://localhost:4840'),
],
'plc-line-1' => [
'endpoint' => 'opc.tcp://10.0.0.10:4840',
'username' => 'operator',
'password' => 'pass123',
],
],Connecting
// Default connection
$client = Opcua::connect();
// Named connection
$client = Opcua::connect('plc-line-1');Getting a Connection Without Connecting (Managed Mode Only)
v4.0 breaking change: In direct mode,
Opcua::connection()now returns an already-connectedClient. TheOpcUaClientInterfaceno longer exposes setter methods (setTimeout(), etc.), so there is no pre-connect configuration step. If you need to customise timeout or other settings, set them inconfig/opcua.phpor pass inline config toconnectTo().In managed mode (session manager running),
connection()returns aManagedClientthat still supports setter methods before the first operation.
// Direct mode — returns a connected Client (same as connect())
$client = Opcua::connection('plc-line-1');
// Managed mode — setter methods are still available
// $client = Opcua::connection('plc-line-1');
// $client->setTimeout(15.0);Switching the Default
OPCUA_CONNECTION=plc-line-1
Ad-hoc Connections
Connect to endpoints not defined in config:
// Minimal
$client = Opcua::connectTo('opc.tcp://10.0.0.50:4840');
// With inline config
$client = Opcua::connectTo('opc.tcp://10.0.0.99:4840', [
'username' => 'operator',
'password' => 'secret',
'security_policy' => 'Basic256Sha256',
'security_mode' => 'SignAndEncrypt',
]);
// With a name for later retrieval
$client = Opcua::connectTo('opc.tcp://10.0.0.99:4840', as: 'temp-plc');
$same = Opcua::connection('temp-plc');Disconnecting
// Single connection
Opcua::disconnect('plc-line-1');
// Default connection
Opcua::disconnect();
// All connections (including ad-hoc)
Opcua::disconnectAll();After disconnecting, the next call to connection() or connect() creates a fresh client.
Connection Caching
OpcuaManager caches client instances by name. Repeated calls to connection('plc-1') return the same instance:
$a = Opcua::connection('plc-1');
$b = Opcua::connection('plc-1');
assert($a === $b); // trueCalling disconnect() removes the cached instance.
Dependency Injection
The OpcuaManager is registered as a singleton and can be injected anywhere:
use PhpOpcua\LaravelOpcua\OpcuaManager;
class PlcController extends Controller
{
public function read(OpcuaManager $opcua)
{
$client = $opcua->connect();
$value = $client->read('ns=2;i=1001');
$client->disconnect();
return response()->json(['value' => $value->getValue()]);
}
}You can also resolve it from the container:
$manager = app(OpcuaManager::class);
// or
$manager = app('opcua');Session Manager Auto-Detection
When creating a client, OpcuaManager checks for the daemon's Unix socket:
session_manager.enabledistrue(default)session_manager.socket_pathfile exists
If both conditions are met, a ManagedClient (daemon-proxied) is created. Otherwise, a direct Client is created. This is transparent — your application code doesn't change.
if (Opcua::isSessionManagerRunning()) {
// ManagedClient — session persists across requests
} else {
// Direct Client — new TCP connection per request
}How Connection Configuration Works
When a connection is created, OpcuaManager applies configuration differently depending on the mode:
Direct Mode (configureBuilder)
A ClientBuilder is configured and then connect() is called, returning a fully connected Client. All settings are immutable after connection.
- Security policy and mode
- User credentials (username/password)
- Client certificate and CA
- User certificate
- Timeout
- Auto-retry
- Batch size
- Browse max depth
- Trust store path and trust policy
- Auto-accept and auto-accept-force
- Auto-detect write type
- Read metadata cache
- Logger (explicit config or Laravel default)
- Cache (explicit config or Laravel default)
Managed Mode (configureManagedClient)
A ManagedClient is created and setter methods are called. Settings can be adjusted after creation.
- Security policy and mode
- User credentials (username/password)
- Client certificate and CA
- User certificate
- Timeout
- Auto-retry
- Batch size
- Browse max depth
- Logger (explicit config or Laravel default)
- Cache (explicit config or Laravel default)