Live queries notify your application whenever records that match a query are created, updated, or deleted. You start a LIVE SELECT statement to get a query ID, then iterate the messages the server pushes for that ID.
Note
Starting a live query
Run a LIVE SELECT statement with run(). The result is the live query ID.
Receiving messages
Pass the ID to live(). It returns an iterable of LiveMessage objects, one per change. Each message carries the action, the affected record ID, and the new value.
Running without blocking the application
A foreach over live() blocks the current PHP process while it waits for the next message. The standard PHP runtime (PHP-FPM or the CLI) handles one task per worker, so a process parked on a live query cannot serve anything else.
Warning
To run live queries alongside the rest of your application, choose an asynchronous runtime or run dedicated workers. See Runtimes and workers for configuring PHP-FPM with dedicated workers, OpenSwoole, and FrankenPHP.
Live actions
Every message has an action from the LiveAction enum:
| Action | Description |
|---|---|
LiveAction::Create | A new record matched the query |
LiveAction::Update | A matching record was modified |
LiveAction::Delete | A matching record was removed |
LiveAction::Killed | The live query was stopped on the server |
Stopping a live query
Stop a live query by running a KILL statement with its ID. The loop receives a final LiveAction::Killed message and ends.
Learn more
Surreal API reference for the
live()signatureLIVE SELECT for the SurrealQL syntax
Connecting to SurrealDB for WebSocket setup