• Start

Languages

/

PHP

/

v2 (alpha)

/

Concepts

Events

Observe connection lifecycle and RPC traffic in version 2 of the PHP SDK with the high-level subscribe() API and the lower-level PSR-14 event dispatcher.

Version 2 of the PHP SDK exposes two ways to observe what it is doing. The high-level subscribe() method covers connection lifecycle changes with a simple callback. The lower-level PSR-14 event dispatcher emits a richer stream that includes per-request events, and integrates with framework dispatchers.

Use subscribe() for quick hooks into the connection lifecycle. Pass an event name and a listener; the method returns a closure that removes the listener when called.

$unsubscribe = $db->subscribe('connected', function (string $version): void {
echo "Connected to SurrealDB {$version}";
});

// later
$unsubscribe();

The available event names are connecting, connected, reconnecting, disconnected, error, auth, and using. This is the simplest option when you only need to react to the connection coming up, dropping, or re-authenticating. See Connecting to SurrealDB and Authentication for lifecycle examples.

For full observation, including every RPC request and response, the SDK dispatches typed event objects through a PSR-14 event dispatcher. Unlike subscribe(), this stream reaches RPC-level events and lets you reuse a framework's dispatcher.

By default the SDK creates its own dispatcher. To receive events, either register listeners on a dispatcher you control and pass it through DriverOptions, or pass your framework's PSR-14 dispatcher.

use SurrealDB\SDK\Surreal;
use SurrealDB\SDK\Connection\DriverOptions;
use SurrealDB\SDK\Events\EventDispatcher;
use SurrealDB\SDK\Events\ListenerProvider;
use SurrealDB\SDK\Events\RpcResponseReceived;

$provider = new ListenerProvider();
$provider->on(RpcResponseReceived::class, function (RpcResponseReceived $event): void {
error_log($event->request->method);
});

$db = new Surreal(new DriverOptions(
events: new EventDispatcher($provider),
));

The bundled ListenerProvider matches a listener against the event class and any of its parents or interfaces, so you can listen to a single event type or a shared base.

Because the SDK depends only on the PSR-14 EventDispatcherInterface, you can pass the dispatcher from Laravel, Symfony, or any PSR-14 bridge. The SDK then dispatches its events into your application's existing listener setup.

$db = new Surreal(new DriverOptions(
events: $container->get(\Psr\EventDispatcher\EventDispatcherInterface::class),
));

All events live in the SurrealDB\SDK\Events namespace and are readonly value objects.

EventDispatched whenPayload
ConnectingA connection attempt startsnone
ConnectedThe connection is established and readyversion
DisconnectedThe connection closesnone
ReconnectingA dropped connection is being re-establishednone
ConnectionErrorA connection-level error occurserror
AuthChangedA session's authentication changes or clearstokens, session
NamespaceDatabaseSelectedThe namespace or database changesthe selection, session
RpcRequestSentJust before a request is handed to the transportrequest
RpcResponseReceivedAfter a response is received (success or error)request, response
LiveMessageReceivedA live query notification arrivesmessage
use SurrealDB\SDK\Events\RpcRequestSent;
use SurrealDB\SDK\Events\AuthChanged;

$provider->on(RpcRequestSent::class, function (RpcRequestSent $event): void {
// $event->request->method, $event->request->params
});

$provider->on(AuthChanged::class, function (AuthChanged $event): void {
// $event->tokens is null when the session is signed out
});
  • Use subscribe() for connection lifecycle hooks with the least setup.

  • Use the PSR-14 dispatcher when you need per-request events, want to fan events into a framework's listeners, or are building observability tooling.

Was this page helpful?