The Surreal class is the entry point for version 2 of the SDK. It manages the connection, the session, authentication, and query execution.
Namespace: SurrealDB\SDK\Surreal
Source: src/Surreal.php
Constructor
DriverOptions customises driver-wide behaviour such as the codec, HTTP client, scheduler, and middleware. Pass null for the defaults.
Connection methods
connect()
Connect to a SurrealDB endpoint.
| Parameter | Type | Description |
|---|---|---|
url | string | Endpoint | The endpoint to connect to, such as ws://127.0.0.1:8000/rpc. |
options | ConnectOptions | Namespace, database, authentication, and reconnection settings. |
close()
Close the connection and release its resources.
status()
Return the current ConnectionStatus.
isConnected()
Return whether the connection is established. Equivalent to status() === ConnectionStatus::Connected.
health()
Throw if the instance is unreachable, otherwise return nothing.
version()
Return the server version string, for example surrealdb-2.1.0.
isFeatureSupported()
Return whether a feature is available on the current connection and server.
subscribe()
Subscribe to a lifecycle event: connecting, connected, reconnecting, disconnected, error, auth, or using. Returns a closure that removes the listener.
connection()
Return the underlying ConnectionController for advanced operations such as transactions, sessions, and import/export.
Session methods
use()
Select a namespace and an optional database.
let()
Define a session parameter, available in later queries as $name.
unset()
Remove a session parameter.
Authentication methods
signin()
Sign in with a credential object or an array. Returns a Tokens object.
signup()
Sign up a new record user. Returns a Tokens object.
authenticate()
Authenticate the session with an existing token.
invalidate()
Clear the session's authentication.
Query methods
query()
Execute a BoundQuery, returning one result per statement.
run()
Execute raw SurrealQL with optional bindings, returning one result per statement.
Statement builders
These methods start a fluent query builder. Call execute() to run it.
select($what),create($what),update($what),upsert($what),delete($what)insert($tableOrData, $data = null),relate($from, $edge, $to, $data = null)call($name, $version = null, $args = []),auth()
live()
Subscribe to a live query by its ID. Returns an iterable of LiveMessage objects.
Iterating the result blocks the process while it waits for messages. See running without blocking the application for consuming live queries in a worker.
Supporting types
ConnectOptions
Per-connection settings passed to connect().
Namespace: SurrealDB\SDK\Connection\ConnectOptions
ConnectionStatus
A string-backed enum with the connection lifecycle states.
Namespace: SurrealDB\SDK\Connection\ConnectionStatus
Values: Disconnected, Connecting, Reconnecting, Connected
Tokens
The result of signin() and signup(): an access token and an optional refresh token.
Namespace: SurrealDB\SDK\Auth\Tokens
LiveMessage
A single live query notification.
Namespace: SurrealDB\SDK\Live\LiveMessage
ConnectionController
The controller orchestrates the connection. Access it with $db->connection() for operations that are not on the Surreal facade.
Namespace: SurrealDB\SDK\Connection\ConnectionController
Transactions
Import and export
Token renewal
Sessions
See also
Query Builders for the fluent statement API
Data types for the value classes
Utilities for
BoundQuery, enums, and driver optionsConnecting to SurrealDB for the connection guide