# Changes in depth

Code on this page comes in TypeScript, Rust, and Python. The same page with one language only: [Python](https://docs.laserdata.com/md/python/laser-sdk/advanced/change-feed.md), [TypeScript](https://docs.laserdata.com/md/typescript/laser-sdk/advanced/change-feed.md), [Rust](https://docs.laserdata.com/md/rust/laser-sdk/advanced/change-feed.md).

This is the full reference for [Changes](/laser-sdk/change-feed). Start there for the short version.

The change feed tells you when an operational view advanced. Each change record says which index moved, over which source offsets, and how many rows landed. It does not carry the rows. Read the feed, then query the index only when it moved. The feed is an ordinary topic read by offset over the connection you already have, so it adds no new connection or push channel.

## Turn it on

Call `notify()` on the [projection binding](/laser-sdk/advanced/views#bind-a-topic). After each committed projector batch for that binding, the managed backend publishes one change record. A binding without `notify()` serves queries only. Watch the same index name that you query.

The feed needs the `watch` capability, which Laser Stack and LaserData Cloud advertise. Reading the rows afterward also needs the `query` capability. In Rust, the reader needs the `watch` feature, which the `managed` feature includes.

## Where records go

Change records go to the `changes` topic on the ops stream, `_agdx`, the stream the managed services use for their own records. On a deployment that reports `stream_tenancy`, each stream has its own change topic, `stream:<stream>/_agdx/changes`. A client with a default stream reads its stream's topic, and each record names its `stream`. A client with a default stream also scopes the index name it filters on, the same way a query scopes it.

## Open a reader

| Language | Open a reader for one index | Read every index | Returns |
| --- | --- | --- | --- |
| Rust | `laser.watch().index(name).records().await?` | `laser.watch().records().await?` | `Result<WatchReader, LaserError>` |
| TypeScript | `await laser.watch().index(name).records()` | `await laser.watch().records()` | `Promise<WatchReader>` |
| Python | `await laser.watch(index=name)` | `await laser.watch()` | `WatchReader` |

When the deployment does not publish the feed, opening the reader fails with an unsupported error. Opening a reader checks the capabilities again, so a deployment that starts publishing the feed after the client connected is seen. The SDK never waits on a topic that nothing writes to.

A new reader starts at the first change the feed still retains. Add `.from_end()` in Rust, `.fromEnd()` in TypeScript, or `from_end=True` in Python to start at the current end of every partition, so the reader receives only changes published after it opened. Open the reader before the write you want to await.

`.index(name)` keeps one index. The filter runs in the client, so the reader still reads every record on the topic. The reader skips any record that does not decode as a change record and counts it in `skipped`.

## Read records

Your application owns the loop. The SDK tracks the position between polls and has no push callbacks.

1. Call `poll()`. It returns every matching change that arrived since the last poll, or an empty list when the reader is caught up. It never waits.
2. Or call `poll_within(timeout)` (Rust takes a `Duration`, Python `timeout_ms`, TypeScript `pollWithin(timeoutMs, { signal })`). It reads again every 100 ms until a change lands or the timeout passes, and returns an empty list when none landed in time.
3. Handle the changes. Query the index if it moved.

Each item is a watched change: the `change` record, its `message_id` (TypeScript `messageId`), and its `position` on the feed, the partition and offset.

To read one record at a time, Rust's `stream()` returns a `futures::Stream`, TypeScript's `stream()` returns an async generator, and the Python reader is itself an async iterator. All three end when the reader is caught up. In TypeScript and Python a later loop on the same reader resumes where the last one stopped. Rust's `stream()` takes ownership of the reader, so save its offsets first if you need them.

**TypeScript**

```ts
const feed = await laser.watch().index("readings_v1").records()
for await (const { change } of feed.stream()) {
  console.log(`${change.index} advanced ${change.rows} row(s) on partition ${change.partitionId}`)
}
```

**Rust**

```rust
use futures::StreamExt;

let feed = laser.watch().index("readings_v1").records().await?;
let mut changes = std::pin::pin!(feed.stream());
while let Some(watched) = changes.next().await {
    let change = watched?.change;
    println!(
        "{} advanced {} row(s) on partition {}",
        change.index, change.rows, change.partition_id
    );
}
```

**Python**

```python
feed = await laser.watch(index="readings_v1")
async for watched in feed:
    change = watched.change
    print(f"{change.index} advanced {change.rows} row(s) on partition {change.partition_id}")
```

## Change record fields

| Field | TypeScript | Meaning |
| --- | --- | --- |
| `index` | `index` | The index that advanced |
| `partition_id` | `partitionId` | The source partition |
| `from_offset` | `fromOffset` | First source offset in the batch, inclusive |
| `to_offset` | `toOffset` | Last source offset in the batch, inclusive |
| `rows` | `rows` | Rows the batch wrote |
| `stream` | `stream` | The source stream, set only when the deployment keeps one feed per stream |
| `v` | `v` | The change record version, currently 1 |

A record whose `v` is not the version the client reads stops its partition at that record. The changes before it are returned, and a poll with nothing else to return fails with a command error whose code is `VersionSkew`, so an older client never misreads a newer record. Upgrade the client to continue.

In TypeScript, `fromOffset` and `toOffset` are `bigint` values and `stream` is optional. In Rust, `stream` is an `Option<String>`. In Python, it is `None` when absent.

## Resume after a restart

Save the reader's offsets and restore them in a new reader. Where you store them is up to you.

| Language | Read the position | Restore it |
| --- | --- | --- |
| Rust | `feed.offsets()`, a slice with one `u64` per partition | `.from_offsets(saved)` on a new reader, with a `Vec<u64>` |
| TypeScript | The `feed.offsets` property, a `ReadonlyMap<number, bigint>` from partition to offset | `.fromOffsets(saved)` on a new reader |
| Python | The `feed.offsets` property, a list of integers | `.from_offsets(saved)`, or `await laser.watch(index=name, from_offsets=saved)` |

Each offset is the next one to read on that partition. A new reader without restored offsets starts at the first retained change. In Rust, a saved list shorter than the partition count reads the missing partitions from the start.

After a resume the reader counts the feed records retention removed before it reached them, in `missed`. A count above zero means changes were lost: query the view directly instead of trusting the feed.

**TypeScript**

```ts
const saved = feed.offsets

// After a restart
const resumed = (await laser.watch().index("readings_v1").records()).fromOffsets(saved)
await resumed.poll()
if (resumed.missed > 0n) await laser.query("readings_v1").fetch()
```

**Rust**

```rust
let mut feed = laser.watch().index("readings_v1").records().await?;
feed.poll().await?;
let saved = feed.offsets().to_vec();

// After a restart
let mut resumed = laser
    .watch()
    .index("readings_v1")
    .records()
    .await?
    .from_offsets(saved);
resumed.poll().await?;
if resumed.missed() > 0 {
    laser.query("readings_v1").fetch().await?;
}
```

**Python**

```python
saved = feed.offsets

# After a restart
resumed = await laser.watch(index="readings_v1", from_offsets=saved)
await resumed.poll()
if resumed.missed:
    await laser.query("readings_v1").fetch()
```

## Delivery guarantees

* Notifications are best effort after commit. A lost notification does not lose projected data, because the rows are already in the view.
* The same change always carries the same message id, derived from its content. A retried batch can publish a change twice, so drop a change whose `message_id` you already handled.
* A reader that falls behind the feed's retention window misses the records that expired, and its `missed` count says how many. Query the view directly to catch up.
* The feed reports progress. It does not prove that a specific write is in the view. To make sure a query includes your own write, use a read-your-writes query. See [Consistency](/laser-sdk/advanced/views#consistency).
* The feed reports operational view changes only. It does not report key-value or session changes. Sessions have their own change feed on the session reads.

## Key operations

| Verb | What it does |
| --- | --- |
| `notify()` on a binding | Turn on change records for that index |
| `watch().index(name).records()` | Open a reader for one index (Rust, TypeScript) |
| `watch(index=name)` | Open a reader for one index (Python) |
| `watch().records()` / `watch()` | Read every index on one feed |
| `from_end()` / `fromEnd()` / `from_end=True` | Start at the current end of the feed |
| `poll()` | Read the changes that arrived since the last poll |
| `poll_within(timeout)` / `pollWithin(timeoutMs)` | Wait for the first change, up to a timeout |
| `missed` / `skipped` | Records retention removed before the reader reached them, records that were not change records |
| `stream()` | Read records one at a time until caught up (Rust, TypeScript. Python: `async for`) |
| `offsets` / `from_offsets(saved)` | Save and restore the position across restarts (TypeScript: `fromOffsets`) |

- [Changes](/laser-sdk/change-feed): The short guide
- [Queries and views in depth](/laser-sdk/advanced/views): Bindings, queries, and consistency
- [Laser Stack](/laser-sdk/laser-stack): Run the change feed locally

Source: https://docs.laserdata.com/laser-sdk/advanced/change-feed
