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.
What a change contains
Section titled “What a change contains”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 join removes children
Section titled “When a join removes children”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.
Watch a read model
Section titled “Watch a read model”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}"); });import io.cratis.chronicle.IEventStoreimport io.cratis.chronicle.readModels.ReadModelimport kotlinx.coroutines.flow.collect
@ReadModeldata class WatchingBasicOrder(val status: String = "", val totalAmount: Double = 0.0)
suspend fun watchOrders(store: IEventStore) { store.readModels.watch(WatchingBasicOrder::class).collect { changeset -> if (changeset.removed || changeset.readModel == null) return@collect
println("${changeset.modelKey}: ${changeset.readModel!!.status}") }}import io.cratis.chronicle.EventStore;import io.cratis.chronicle.readModels.ReadModel;
import io.cratis.chronicle.java.ReadModelsJavaBridge;import kotlinx.coroutines.Job;
@ReadModelclass WatchingBasicOrder { private String status = ""; private double totalAmount = 0;
public String getStatus() { return status; } public void setStatus(String status) { this.status = status; }
public double getTotalAmount() { return totalAmount; } public void setTotalAmount(double totalAmount) { this.totalAmount = totalAmount; }}
class ReadModelsWatchingReadModelsBasic { Job watchOrders(EventStore store) { return ReadModelsJavaBridge.watch(store.getReadModels(), WatchingBasicOrder.class, changeset -> { if (changeset.getRemoved() || changeset.getReadModel() == null) { return; }
System.out.println(changeset.getModelKey() + ": " + changeset.getReadModel().getStatus()); }); }}defmodule MyApp.ReadModels.WatchingBasicOrder do use Cratis.Chronicle.ReadModels.ReadModel
defstruct id: "", status: ""end
defmodule MyApp.WatchingReadModelsBasicMonitor do alias MyApp.ReadModels.WatchingBasicOrder
def start_watching do {:ok, watcher} = Cratis.Chronicle.ReadModels.watch(WatchingBasicOrder) watcher end
def handle_next_change do receive do {:chronicle_read_model_changed, WatchingBasicOrder, changeset} -> if changeset.removed or is_nil(changeset.read_model) do :ok else IO.puts("#{changeset.model_key}: #{changeset.read_model.status}") end end end
def stop_watching(watcher), do: Cratis.Chronicle.ReadModels.unwatch(watcher)endimport { ReadModelChangeType } from '@cratis/chronicle';
const watcher = store.readModels.createWatcher(Order, { signal: AbortSignal.timeout(60_000) });const stopRefreshing = watcher.onResubscribed(async () => { // Requery the entire collection, including additions and removals missed during the outage. const orders = await store.readModels.getInstances(Order); console.table(orders);});try { await watcher.subscribed; // The kernel is now subscribed; events appended after this point can be observed. for await (const changeset of watcher) { console.log(changeset.changeType === ReadModelChangeType.Added ? 'Added' : 'Changed', changeset.key, changeset.changeContext?.correlationId); if (!changeset.removed) { console.log(changeset.readModel.status); } }} finally { stopRefreshing(); watcher.dispose();}createWatcher() starts immediately. Await subscribed before producing events when you need to observe their changes. The example stops after one minute; a service can pass its shutdown signal instead. watch() keeps its original async-iterable return type and lazy, pull-based behavior: the stream starts on the first next(), and each change waits for consumer demand. It has no client-side buffer or overflow limit. Use createWatcher() for readiness and lifetime controls.
By default, transport errors reject iteration with the original error and stream completion ends iteration. Registering the onResubscribed callback opts this example into resumption: transport failures (gRPC 1/4/13/14) and stream completion resume on the same watcher. You can also opt in explicitly with createWatcher(Order, { resume: true }), but must still account for missed changes. onResubscribed fires after the next subscription acknowledgment, not the first, so you can refresh even if no further changes arrive. Register before awaiting readiness; late registrations do not replay earlier notifications. Async callbacks are awaited before reading further changes on the resumed stream; a thrown error or rejected promise fails iteration. The returned function unregisters the callback, as does disposal. Removing the last callback disables resumption for subsequent failures unless resume: true was passed.
After a disconnect, watcher.subscribed waits for the next acknowledgment; previously resolved promises remain resolved. Changes received before the outage are still delivered, but changes during the outage are not reported. The callback refreshes the collection while the loop logs received changes; if you maintain a displayed collection, treat those changes as invalidations and requery rather than overwriting refreshed state with older buffered payloads. Non-transport stream errors, conversion/compliance failures, and callback failures reject iteration and require a new watcher. A terminal connection failure requires a new client after correcting its cause.
For createWatcher(), stream reads pause after acknowledgment at 1,024 buffered changes until the consumer pulls. Before each acknowledgment, the watcher must keep reading to reach the marker; exceeding 1,024 buffered changes then fails iteration and readiness. Use createWatcher(Order, { maxBuffered: 256 }) only if you want overflow failure instead of backpressure; the positive-integer limit applies before and after acknowledgment. Both APIs yield optional changeType and changeContext fields.
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.
Filter changes
Section titled “Filter changes”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}"); });import io.cratis.chronicle.IEventStoreimport io.cratis.chronicle.readModels.ReadModelimport kotlinx.coroutines.flow.collectimport kotlinx.coroutines.flow.filter
@ReadModeldata class WatchingFilteringOrder(val status: String = "", val totalAmount: Double = 0.0)
/** * Filtering happens client-side - the server still sends every change for the read model type. */suspend fun watchHighValueOrders(store: IEventStore, threshold: Double) { store.readModels.watch(WatchingFilteringOrder::class) .filter { (it.readModel?.totalAmount ?: 0.0) > threshold } .collect { changeset -> println("${changeset.modelKey}: ${changeset.readModel?.totalAmount}") }}import io.cratis.chronicle.EventStore;import io.cratis.chronicle.readModels.ReadModel;
import io.cratis.chronicle.java.ReadModelsJavaBridge;import kotlinx.coroutines.Job;
@ReadModelclass WatchingFilteringOrder { private String status = ""; private double totalAmount = 0;
public String getStatus() { return status; } public void setStatus(String status) { this.status = status; }
public double getTotalAmount() { return totalAmount; } public void setTotalAmount(double totalAmount) { this.totalAmount = totalAmount; }}
class ReadModelsWatchingReadModelsFiltering { // Filtering happens client-side, inside the callback - the server still sends every change // for the read model type. Job watchHighValueOrders(EventStore store, double threshold) { return ReadModelsJavaBridge.watch(store.getReadModels(), WatchingFilteringOrder.class, changeset -> { WatchingFilteringOrder order = changeset.getReadModel(); if (order == null || order.getTotalAmount() <= threshold) { return; }
System.out.println(changeset.getModelKey() + ": " + order.getTotalAmount()); }); }}defmodule MyApp.ReadModels.WatchingFilteringOrder do use Cratis.Chronicle.ReadModels.ReadModel
defstruct id: "", total_amount: 0.0end
defmodule MyApp.WatchingReadModelsFilteringMonitor do alias MyApp.ReadModels.WatchingFilteringOrder
def start_watching do {:ok, watcher} = Cratis.Chronicle.ReadModels.watch(WatchingFilteringOrder) watcher end
def handle_next_change(threshold) do # Filtering happens client-side, in the process receiving the changes. receive do {:chronicle_read_model_changed, WatchingFilteringOrder, changeset} -> if changeset.read_model && changeset.read_model.total_amount > threshold do IO.puts("#{changeset.model_key}: #{changeset.read_model.total_amount}") end end endendfor await (const changeset of store.readModels.watch(Order)) { if (changeset.readModel.totalAmount <= threshold) { continue; }
console.log(`${changeset.key}: ${changeset.readModel.totalAmount}`);}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.
When to use this
Section titled “When to use this”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.
Related topics
Section titled “Related topics”- Getting a Single Instance - Read current state on demand
- Getting a Collection of Instances - Read all current instances
- Getting Snapshots - Inspect historical state
- Reactors - React to events directly