# select_live

The .select().live() methods for the SurrealDB Rust SDK initiate live queries for a live stream of notifications.

**3.x**

Initiate live queries for a live stream of notifications.

You can achieve the same live subscription by running `LIVE SELECT` inside [`query()`](/docs/reference/rust/methods/query.md#live-select-stream) and consuming results with [`IndexedResults::stream`](https://docs.rs/surrealdb/latest/surrealdb/struct.IndexedResults.html#method.stream). This can be useful when one or more live statements are part of a larger batch or if you prefer to use raw SurrealQL.

```rust title="Method Syntax"
db.select(resource).live()
```

## Arguments
<table>
    <thead>
        <tr>
            <th colspan="2" scope="col">Argument</th>
            <th colspan="2" scope="col">Description</th>
        </tr>
    </thead>
    <tbody>
        <tr>
            <td colspan="2" scope="row" data-label="Argument">
                <code>resource</code>
            </td>
            <td colspan="2" scope="row" data-label="Description">
                The table name or a record ID to select.
            </td>
        </tr>
    </tbody>
</table>

## Example usage: Listen for live updates

The following example requires adding the `futures` crate with `cargo add futures` in order to work with the results of the async stream. Once run, the program will continue to wait and listen for events for the `person` table to happen.

```rust
use futures::StreamExt;
use surrealdb::engine::remote::ws::Ws;
use surrealdb::opt::auth::Root;
use surrealdb::{Notification, Surreal};
use surrealdb::types::{RecordId, SurrealValue};

#[derive(Debug, SurrealValue)]
struct Person {
    id: RecordId,
}

// Handle the result of the live query notification
fn handle(result: Result<Notification<Person>, surrealdb::Error>) {
    println!("Received notification: {:?}", result);
}

#[tokio::main]
async fn main() -> surrealdb::Result<()> {
    let db = Surreal::new::<Ws>("127.0.0.1:8000").await?;

    db.signin(Root {
        username: "root".into(),
        password: "secret".into(),
    })
    .await?;

    db.use_ns("main").use_db("main").await?;

    db.query("DEFINE TABLE person").await?;

    // Select the "person" table and listen for live updates.
    let mut stream = db.select("person").live().await?;

    // Process updates as they come in.
    while let Some(result) = stream.next().await {
        // Do something with the notification
        handle(result);
    }
    Ok(())
}
```

Then connect to it using SurrealDB Studio or open a new terminal window with the following command.

```bash
surreal sql --user root --pass secret --pretty
```

You can then use queries like the following to work with some `person` records.

```surql
CREATE person;
UPDATE person SET is_nice_person = true;
DELETE person;
```

The following output will then show up in the terminal window running the Rust example.

```text
Received notification: Ok(Notification { query_id: Uuid(ab57b7f9-00b1-47e8-8f29-9d11d0572264), action: Create, data: Person { id: RecordId { table: Table("person"), key: String("pbpm2xhmaofyne383455") } } })
Received notification: Ok(Notification { query_id: Uuid(ab57b7f9-00b1-47e8-8f29-9d11d0572264), action: Update, data: Person { id: RecordId { table: Table("person"), key: String("pbpm2xhmaofyne383455") } } })
Received notification: Ok(Notification { query_id: Uuid(ab57b7f9-00b1-47e8-8f29-9d11d0572264), action: Delete, data: Person { id: RecordId { table: Table("person"), key: String("pbpm2xhmaofyne383455") } } })
```

## See also

* [.live() method on Docs.rs](https://docs.rs/surrealdb/latest/surrealdb/method/struct.Select.html#method.live)
* [`query()` with streaming `LIVE SELECT`](/docs/reference/rust/methods/query.md#live-select-stream)

**2.x**

Initiate live queries for a live stream of notifications.

```rust title="Method Syntax"
db.select(resource).live()
```

## Arguments
<table>
    <thead>
        <tr>
            <th colspan="2" scope="col">Argument</th>
            <th colspan="2" scope="col">Description</th>
        </tr>
    </thead>
    <tbody>
        <tr>
            <td colspan="2" scope="row" data-label="Argument">
                <code>resource</code>
            </td>
            <td colspan="2" scope="row" data-label="Description">
                The table name or a record ID to select.
            </td>
        </tr>
    </tbody>
</table>

## Example usage: Listen for live updates

The following example requires adding the `futures` crate with `cargo add futures` in order to work with the results of the async stream. Once run, the program will continue to wait and listen for events for the `person` table to happen.

```rust
use futures::StreamExt;
use serde::Deserialize;
use surrealdb::engine::remote::ws::Ws;
use surrealdb::opt::auth::Root;
use surrealdb::{Notification, Surreal};
use surrealdb::RecordId;

#[derive(Debug, Deserialize)]
struct Person {
    id: RecordId,
}

// Handle the result of the live query notification
fn handle(result: Result<Notification<Person>, surrealdb::Error>) {
    println!("Received notification: {:?}", result);
}

#[tokio::main]
async fn main() -> surrealdb::Result<()> {
    let db = Surreal::new::<Ws>("127.0.0.1:8000").await?;

    db.signin(Root {
        username: "root",
        password: "secret",
    })
    .await?;

    db.use_ns("ns").use_db("db").await?;

    // Select the "person" table and listen for live updates.
    let mut stream = db.select("person").live().await?;

    // Process updates as they come in.
    while let Some(result) = stream.next().await {
        // Do something with the notification
        handle(result);
    }
    Ok(())
}
```

Then connect to it using SurrealDB Studio or open a new terminal window with the following command.

```bash
surreal sql --namespace ns --database db --user root --pass secret --pretty
```

You can then use queries like the following to work with some `person` records.

```surql
CREATE person;
UPDATE person SET is_nice_person = true;
DELETE person;
```

The following output will then show up in the terminal window running the Rust example.

```text
Received notification: Ok(Notification { query_id: b55d31dc-e657-4a6b-a32b-f5abed4ef459, action: Create, data: Person { id: RecordId { table: "person", key: String("334mabva9ibitsypabm5") } } })
Received notification: Ok(Notification { query_id: b55d31dc-e657-4a6b-a32b-f5abed4ef459, action: Update, data: Person { id: RecordId { table: "person", key: String("334mabva9ibitsypabm5") } } })
Received notification: Ok(Notification { query_id: b55d31dc-e657-4a6b-a32b-f5abed4ef459, action: Delete, data: Person { id: RecordId { table: "person", key: String("334mabva9ibitsypabm5") } } })
```

## See also

* [.live() method on Docs.rs](https://docs.rs/surrealdb/latest/surrealdb/method/struct.Select.html#method.live)
