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.
Method Syntaxdb.ListenLive<T>(queryUuid)
Arguments | Description | ||
---|---|---|---|
queryUuid required | The UUID of the live query to consume. |
await using var liveQuery = db.ListenLive<Person>(queryUuid); // Consume the live query...
You can then consume the live query using either an IAsyncEnumerable
or an Observable
.
IAsyncEnumerable
:::note NOTE: This will block the current thread until the query is killed. :::
Option 1: Consume the live query via an IAsyncEnumerableawait foreach (var response in liveQuery) { // Either an Open, Create, Update, Delete or Close notification... if (response is SurrealDbLiveQueryOpenResponse) { // Do something... } if (response is SurrealDbLiveQueryCreateResponse<Person> create) { // Use the `Result` record } if (response is SurrealDbLiveQueryUpdateResponse<Person> update) { // Use the `Result` record } if (response is SurrealDbLiveQueryDeleteResponse<Person> delete) { // Use the `Result` record } if (response is SurrealDbLiveQueryCloseResponse) { // Do something... } }
Observable
Option 2: Consume the live query via an ObservableliveQuery .ToObservable() .Subscribe((response) => { // Either an Open, Create, Update, Delete or Close notification... if (response is SurrealDbLiveQueryOpenResponse) { // Do something... } if (response is SurrealDbLiveQueryCreateResponse<Person> create) { // Use the `Result` record } if (response is SurrealDbLiveQueryUpdateResponse<Person> update) { // Use the `Result` record } if (response is SurrealDbLiveQueryDeleteResponse<Person> delete) { // Use the `Result` record } if (response is SurrealDbLiveQueryCloseResponse) { // Do something... } });
You can also use the OfType
operator to filter the responses.
liveQuery .ToObservable() .OfType<SurrealDbLiveQueryCreateResponse<Person>>() .Select(response => response.Result) .Subscribe((record) => { // Use the created record });
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.
Method Syntaxawait db.LiveQuery<T>(sql)
Arguments | Description | ||
---|---|---|---|
sql required | Specifies the SurrealQL statements. | ||
cancellationToken optional | The cancellationToken enables graceful cancellation of asynchronous operations. |
const string table = "person"; await using var liveQuery = await db.LiveQuery<Person>($"LIVE SELECT * FROM type::table({table});"); // Consume the live query...
SurrealDbLiveQuery
methodsThe 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 | Description | ||
---|---|---|---|
cancellationToken optional | The cancellationToken enables graceful cancellation of asynchronous operations. |
await using var liveQuery = await db.LiveRawQuery<Person>("LIVE SELECT * FROM person;");await foreach (var response in liveQuery.GetResults()){ // Either a Create, Update or Delete notification... if (response is SurrealDbLiveQueryCreateResponse<Person> create) { // Use the `Result` record } if (response is SurrealDbLiveQueryUpdateResponse<Person> update) { // Use the `Result` record } if (response is SurrealDbLiveQueryDeleteResponse<Person> delete) { // Use the `Result` record } }
GetCreatedRecords()
Returns an enumerator that iterates asynchronously through the collection of created records.
Arguments | Description | ||
---|---|---|---|
cancellationToken optional | The cancellationToken enables graceful cancellation of asynchronous operations. |
await using var liveQuery = await db.LiveRawQuery<Person>("LIVE SELECT * FROM person;");await foreach (var record in liveQuery.GetCreatedRecords()){ // Use the created record }
GetUpdatedRecords()
Returns an enumerator that iterates asynchronously through the collection of updated records.
Arguments | Description | ||
---|---|---|---|
cancellationToken optional | The cancellationToken enables graceful cancellation of asynchronous operations. |
await using var liveQuery = await db.LiveRawQuery<Person>("LIVE SELECT * FROM person;");await foreach (var record in liveQuery.GetUpdatedRecords()){ // Use the updated record }
GetDeletedRecords()
Returns an enumerator that iterates asynchronously through the collection of deleted records.
Arguments | Description | ||
---|---|---|---|
cancellationToken optional | The cancellationToken enables graceful cancellation of asynchronous operations. |
await using var liveQuery = await db.LiveRawQuery<Person>("LIVE SELECT * FROM person;");await foreach (var record in liveQuery.GetDeletedRecords()){ // Use the deleted record }
KillAsync()
Kills the underlying live query.
Method Syntaxawait liveQuery.KillAsync(cancellationToken)
Arguments | Description | ||
---|---|---|---|
cancellationToken optional | The cancellationToken enables graceful cancellation of asynchronous operations. |
var liveQuery = await db.LiveRawQuery<Person>("LIVE SELECT * FROM person;"); // Consume the live query... // Manually kill the live queryawait liveQuery.KillAsync();
.LiveRawQuery<T>()
Initiate a live query from a SurrealQL statement, based on a raw SurrealQL query.
Method Syntaxawait db.LiveRawQuery<T>(sql, params)
Arguments | Description | ||
---|---|---|---|
sql required | Specifies the SurrealQL statements. | ||
params optional | Assigns variables which can be used in the query. | ||
cancellationToken optional | The cancellationToken enables graceful cancellation of asynchronous operations. |
await using var liveQuery = await db.LiveRawQuery<Person>("LIVE SELECT * FROM person;"); // Consume the live query...
.LiveTable<T>()
Initiate a live query from a table.
Method Syntaxawait db.LiveTable<T>(table, diff)
Arguments | Description | ||
---|---|---|---|
table required | The table name to listen for changes for. | ||
diff optional | If set to true, live notifications will include an array of JSON Patch objects, rather than the entire record for each notification. | ||
cancellationToken optional | The cancellationToken enables graceful cancellation of asynchronous operations. |
await using var liveQuery = await db.LiveTable<Person>("person"); // Consume the live query...
.Kill()
Kills a running live query by it’s UUID.
Method Syntaxawait db.Kill(queryUuid)
Arguments | Description | ||
---|---|---|---|
queryUuid required | The UUID of the live query you wish to kill. | ||
cancellationToken optional | The cancellationToken enables graceful cancellation of asynchronous operations. |
await db.Kill(queryUuid);
A live query event can be one of the following:
Action | Result | Description | |
---|---|---|---|
| N/A | Emitted when the live query is opened in the server. | |
|
| Emitted when the live query is closed due to it either being killed or the connection being disconnected. | |
|
| Emitted when a record within your subscription gets created | |
|
| Emitted when a record within your subscription gets updated | |
|
| Emitted when a record within your subscription gets deleted |