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.
db.ListenLive<T>(queryUuid)Arguments
Arguments | Description |
|---|---|
queryUuid | The UUID of the live query to consume. |
Example usage
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.
Using an IAsyncEnumerable
:::note
NOTE: This will block the current thread until the query is killed.
:::
await 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...
}
} Using an Observable
liveQuery
.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.
await db.LiveQuery<T>(sql)Arguments
Arguments | Description |
|---|---|
sql | Specifies the SurrealQL statements. |
cancellationToken | The cancellationToken enables graceful cancellation of asynchronous operations. |
Example usage
const string table = "person";
await using var liveQuery = await db.LiveQuery<Person>($"LIVE SELECT * FROM type::table({table});");
// Consume the live query... 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
await using var liveQuery = await db.LiveRawQuery<Person>("LIVE SELECT * FROM person;");
// highlight-next-line
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
Arguments | Description |
|---|---|
cancellationToken | The cancellationToken enables graceful cancellation of asynchronous operations. |
Example usage
await using var liveQuery = await db.LiveRawQuery<Person>("LIVE SELECT * FROM person;");
// highlight-next-line
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
Arguments | Description |
|---|---|
cancellationToken | The cancellationToken enables graceful cancellation of asynchronous operations. |
Example usage
await using var liveQuery = await db.LiveRawQuery<Person>("LIVE SELECT * FROM person;");
// highlight-next-line
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
Arguments | Description |
|---|---|
cancellationToken | The cancellationToken enables graceful cancellation of asynchronous operations. |
Example usage
await using var liveQuery = await db.LiveRawQuery<Person>("LIVE SELECT * FROM person;");
// highlight-next-line
await foreach (var record in liveQuery.GetDeletedRecords())
{
// Use the deleted record
} KillAsync()
Kills the underlying live query.
await liveQuery.KillAsync(cancellationToken)Arguments
Arguments | Description |
|---|---|
cancellationToken | The cancellationToken enables graceful cancellation of asynchronous operations. |
Example usage
var liveQuery = await db.LiveRawQuery<Person>("LIVE SELECT * FROM person;");
// Consume the live query...
// Manually kill the live query
// highlight-next-line
await liveQuery.KillAsync(); .LiveRawQuery<T>()
Initiate a live query from a SurrealQL statement, based on a raw SurrealQL query.
await db.LiveRawQuery<T>(sql, params)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
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.
await db.LiveTable<T>(table, diff)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
await using var liveQuery = await db.LiveTable<Person>("person");
// Consume the live query... .Kill()
Kills a running live query by it's UUID.
await db.Kill(queryUuid)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
await db.Kill(queryUuid);Live actions
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 |