Realtime data streaming
You can use the SurrealDB SDK to create live queries that listen for changes in the database and automatically update your application when changes occur. This feature is useful for building real-time applications that need to respond to changes in the database.
| Method | Description |
|---|---|
db.ListenLive(queryUuid) | Listen responses from an existing live query |
db.LiveQuery(sql) | Initiate a live query from a SurrealQL statement |
db.LiveRawQuery(sql, params) | Initiate a live query from a SurrealQL statement, based on a raw SurrealQL query |
db.LiveTable(table, diff) | Initiate a live query from a table |
db.Kill(queryUuid) | Kills a running live query by it's UUID |
.ListenLive<T>()
Listen responses from an existing live query.
Arguments
| Arguments | Description |
|---|---|
queryUuid
| The UUID of the live query to consume. |
Example usage
You can then consume the live query using either an IAsyncEnumerable or an Observable.
Using an IAsyncEnumerable
:::note NOTE: This will block the current thread until the query is killed. :::
Using an Observable
You can also use the OfType operator to filter the responses.
Note that this pattern is already simplified via methods available on the SurrealDbLiveQuery object.
You can learn more about these methods in the LiveQuery methods section.
.LiveQuery<T>()
Initiate a live query from a SurrealQL statement.
Arguments
| Arguments | Description |
|---|---|
sql
| Specifies the SurrealQL statements. |
cancellationToken
| The cancellationToken enables graceful cancellation of asynchronous operations. |
Example usage
SurrealDbLiveQuery methods
The SurrealDbLiveQuery object provides the following methods:
GetResults()
Returns an enumerator that iterates asynchronously through the collection of results
(all actions CREATE, UPDATE and DELETE, except OPEN and CLOSE).
Arguments
| Arguments | Description |
|---|---|
cancellationToken
| The cancellationToken enables graceful cancellation of asynchronous operations. |
Example usage
GetCreatedRecords()
Returns an enumerator that iterates asynchronously through the collection of created records.
Arguments
| Arguments | Description |
|---|---|
cancellationToken
| The cancellationToken enables graceful cancellation of asynchronous operations. |
Example usage
GetUpdatedRecords()
Returns an enumerator that iterates asynchronously through the collection of updated records.
Arguments
| Arguments | Description |
|---|---|
cancellationToken
| The cancellationToken enables graceful cancellation of asynchronous operations. |
Example usage
GetDeletedRecords()
Returns an enumerator that iterates asynchronously through the collection of deleted records.
Arguments
| Arguments | Description |
|---|---|
cancellationToken
| The cancellationToken enables graceful cancellation of asynchronous operations. |
Example usage
KillAsync()
Kills the underlying live query.
Arguments
| Arguments | Description |
|---|---|
cancellationToken
| The cancellationToken enables graceful cancellation of asynchronous operations. |
Example usage
.LiveRawQuery<T>()
Initiate a live query from a SurrealQL statement, based on a raw SurrealQL query.
Arguments
| Arguments | Description |
|---|---|
sql
| Specifies the SurrealQL statements. |
params
| Assigns variables which can be used in the query. |
cancellationToken
| The cancellationToken enables graceful cancellation of asynchronous operations. |
Example usage
.LiveTable<T>()
Initiate a live query from a table.
Arguments
| Arguments | Description |
|---|---|
table
| The table name to listen for changes for. |
diff
| If set to true, live notifications will include an array of JSON Patch objects, rather than the entire record for each notification. |
cancellationToken
| The cancellationToken enables graceful cancellation of asynchronous operations. |
Example usage
.Kill()
Kills a running live query by it's UUID.
Arguments
| Arguments | Description |
|---|---|
queryUuid
| The UUID of the live query you wish to kill. |
cancellationToken
| The cancellationToken enables graceful cancellation of asynchronous operations. |
Example usage
Live Actions
A live query event can be one of the following:
| Action | Result | Description |
|---|---|---|
OPEN
| N/A | Emitted when the live query is opened in the server. |
CLOSE
|
SocketClosed or QueryKilled
| Emitted when the live query is closed due to it either being killed or the connection being disconnected. |
CREATE
|
Result
| Emitted when a record within your subscription gets created |
UPDATE
|
Result
| Emitted when a record within your subscription gets updated |
CREATE
|
Result
| Emitted when a record within your subscription gets deleted |