A LIVE SELECT statement creates a session that keeps track of changes to a table in real time. Inside the Rust SDK, this is accomplished by appending .live() to the end of a .select() query.
Getting started
Start a running database using the following command:
surreal start --user root --pass secret
To follow along interactively, connect using Surrealist or the following command to open up the CLI:
Then use the cargo add command to add three crates: surrealdb, tokio, and the futures crate for the StreamExt trait needed to use the async stream.
The example code below shows a live select that keeps track of changes made to a sample of records from the account table. By adding the .range() method, it can be restricted to certain record IDs, in this case any record IDs in between the letters a and g.
The stream from the .live() method returns a stream of Notifications. These contain an action (an Action::Create, Action::Update, or Action::Delete), and a field called data that contains anything that can be deserialized. In this case, the data field will deserialize into a struct that we create called Account. It also contains a query_id field that contains the ID of the live query itself, for record keeping or to cancel using the KILL statement.
Once the following code is run, the Rust client will continue to listen for changes to the account table indefinitely.
whileletSome(result)=sample_accounts.next().await{ matchresult{ Ok(notification)=>{ letaction=notification.action; letaccount: Account=notification.data; letid=notification.query_id; println!("{action:?} from live ID {id}:\n {account:#?}\n"); } Err(error)=>eprintln!("{error}"), } }
Ok(()) }
We can now move to Surrealist or the CLI to execute a few queries and see what happens. The following queries create, update, and delete ten account records.
The Rust client will keep track for any with a record ID in between a and g, returning an output similar to the following.
Create from live ID aad570d6-1d3f-4dd6-81f5-582b92a6d8a4: Account { id: RecordId { table: Table( "account", ), key: String( "cr4obgozhu4oe2fo3vra", ), }, balance: 10.0, }
Create from live ID aad570d6-1d3f-4dd6-81f5-582b92a6d8a4: Account { id: RecordId { table: Table( "account", ), key: String( "do3us1twpyaxm20mp2qt", ), }, balance: 10.0, }
Update from live ID aad570d6-1d3f-4dd6-81f5-582b92a6d8a4: Account { id: RecordId { table: Table( "account", ), key: String( "cr4obgozhu4oe2fo3vra", ), }, balance: 1010.0, }
Update from live ID aad570d6-1d3f-4dd6-81f5-582b92a6d8a4: Account { id: RecordId { table: Table( "account", ), key: String( "do3us1twpyaxm20mp2qt", ), }, balance: 1010.0, }
Delete from live ID aad570d6-1d3f-4dd6-81f5-582b92a6d8a4: Account { id: RecordId { table: Table( "account", ), key: String( "cr4obgozhu4oe2fo3vra", ), }, balance: 1010.0, }
Delete from live ID aad570d6-1d3f-4dd6-81f5-582b92a6d8a4: Account { id: RecordId { table: Table( "account", ), key: String( "do3us1twpyaxm20mp2qt", ), }, balance: 1010.0, }
A LIVE SELECT statement creates a session that keeps track of changes to a table in real time. Inside the Rust SDK, this is accomplished by appending .live() to the end of a .select() query.
Getting started
Start a running database using the following command:
surreal start --user root --pass secret
To follow along interactively, connect using Surrealist or the following command to open up the CLI:
Then use the cargo add command to add four crates: surrealdb and tokio, the futures crate for the StreamExt trait needed to use the async stream, as well as with serde with the "serde_derive" feature (cargo add serde --features serde_derive). The dependencies inside Cargo.toml should look something like this:
cargo add serde --features serde_derive
[dependencies] futures = "0.3.31" serde = { version = "1.0.228", features = ["serde_derive"] } surrealdb = "2.4.1" tokio = "1.49.0"
The example code below shows a live select that keeps track of changes made to a sample of records from the account table. By adding the .range() method, it can be restricted to certain record IDs, in this case any record IDs in between the letters a and g.
The stream from the .live() method returns a stream of Notifications. These contain an action (an Action::Create, Action::Update, or Action::Delete), and a field called data that contains anything that can be deserialized. In this case, the data field will deserialize into a struct that we create called Account. It also contains a query_id field that contains the ID of the live query itself, for record keeping or to cancel using the KILL statement.
Once the following code is run, the Rust client will continue to listen for changes to the account table indefinitely.
whileletSome(result)=sample_accounts.next().await{ matchresult{ Ok(notification)=>{ letaction=notification.action; letaccount: Account=notification.data; letid=notification.query_id; println!("{action:?} from live ID {id}:\n {account:#?}\n"); } Err(error)=>eprintln!("{error}"), } }
Ok(()) }
We can now move to Surrealist or the CLI to execute a few queries and see what happens. The following queries create, update, and delete ten account records.