# Live queries

Subscribe to real-time data changes using live queries and AsyncStream in the SurrealDB Swift SDK.

Live queries let you subscribe to changes on a table and receive events in real time. They require the [WebSocket client](/docs/reference/swift/concepts/connecting.md#websocket-client) and are delivered as an `AsyncStream<LiveEvent<T>>`.

## Starting a live query

```swift
let client = try SurrealWebSocketClient(endpoint: "ws://localhost:8000")
try await client.connect()
_ = try await client.signin(.root(username: "root", password: "secret"))
try await client.use(namespace: "myapp", database: "mydb")

let stream = try await client.live(SurrealDSL.live(Person.self))
```

## Consuming events

Iterate the stream with `for await` and switch on the event's `action`:

```swift
for await event in stream {
    switch event.action {
    case .create:
        print("Created:", event.decoded as Any)
    case .update:
        print("Updated:", event.decoded as Any)
    case .delete:
        print("Deleted record:", event.recordID)
    case .killed:
        print("Live query was killed")
    }
}
```

## Killing a live query

To stop receiving events, kill the live query using its id:

```swift
try await client.kill(liveQueryID: event.queryID)
```

See the [`live`](/docs/reference/swift/methods/live.md) and [`kill`](/docs/reference/swift/methods/kill.md) method references for more detail.
