Skip to content

Watch Chronicle Read Model Changes

Watching a Chronicle read model sends individual changesets containing the changed key, resulting state, and removal flag. Use it to refresh a cache entry or update one item on screen without replacing the complete collection.

Watch does not send an initial collection. If the screen needs its current contents followed by replacement snapshots, use Observing Read Models. If application behavior should run for additions, modifications, or removals, a read model reactor can own the dispatch and subscription.

Watch support and subscription syntax differ by client. Choose your client tab in the examples; the field names below describe the .NET changeset.

A .NET ReadModelChangeset<TReadModel> contains:

Field Meaning
Namespace The Chronicle namespace where the change happened
ModelKey The read model key that changed
ReadModel The resulting state, which can be absent for a removal
Removed Whether the instance was removed
ChangeType The reported change type; reducer watches report non-removals as Modified
ChangeContext Causing event context for a projection watch; reducer watches supply only the model key

The changeset does not contain the previous state. Check Removed and whether ReadModel is present before reading properties. To compare two observed states, keep the last value for each key. If a reaction depends on the original business event, use an event reactor instead.

When a projection removes a child from every parent with RemovedWithJoin, a watcher receives one change for each parent that held the child. Each change carries the full state of that parent after the removal, and Removed is false. There is no per-child delta, and nothing is sent for the key of the removal event itself. A parent that is removed too only produces its own removal change.

These changes are produced after the event is handled, so they can arrive after later changes. They are produced only while the projection is being watched.

Register the read model type before subscribing. The .NET example ignores removals and absent instances before reading Status:

using var subscription = eventStore.ReadModels
.Watch<Order>()
.Subscribe(changeset =>
{
if (changeset.Removed || changeset.ReadModel is null)
{
return;
}
Console.WriteLine($"{changeset.ModelKey}: {changeset.ReadModel.Status}");
});

View C# snippet source on GitHub

Dispose the .NET subscription when its consumer closes. Other clients use their own cancellation or iterator lifetime mechanisms. Keep a long-lived watch in a service with an explicit shutdown path so it does not outlive the work that needs it.

Apply a client-side filter when the consumer only needs some changes. This example selects instances whose current amount exceeds a threshold:

using var subscription = eventStore.ReadModels
.Watch<Order>()
.Where(changeset => changeset.ReadModel?.TotalAmount > threshold)
.Subscribe(changeset =>
{
Console.WriteLine($"{changeset.ModelKey}: {changeset.ReadModel!.TotalAmount:C}");
});

View C# snippet source on GitHub

The filter does not reduce the server’s notifications for that read model type. A predicate on ReadModel can also exclude removals, because a removed instance may have no state. If you maintain a local list, handle removals by key separately or use a collection observation.

Watch when the consumer can own a long-running subscription and needs to act on one changed key at a time. It suits cache invalidation or an interface that already has state and can apply incoming changes.

For a value needed only once, read the instance on demand. To inspect how a projection reached a value, get its snapshots. Neither task needs a permanent subscription.

A watch follows derived state; it is not a substitute for event-reactor processing. Use event reactors when the behavior depends on the domain event and its ordered processing rather than a read model notification.