Skip to content

Keep a Chronicle Read Model List Up to Date

IReadModels.Observe<T>() keeps a complete Chronicle read model list current by sending its initial contents and replacement collection snapshots as stored data changes. Use it for a small list or dashboard; Watch sends individual changesets instead.

The signatures and subscription behavior described here are the .NET API. Choose your client tab for its examples.

The read model must already be registered and maintained by an active projection or reducer. Subscribe to the returned subject, which shares the latest collection with its subscribers, and keep the subscription while the screen or service needs updates:

using System.Reactive.Linq;
using Cratis.Chronicle.ReadModels;
public static class OrderListSubscription
{
// The caller disposes this subscription when the list is no longer needed.
public static IDisposable Subscribe(IReadModels readModels) => readModels
.Observe<Order>()
.Subscribe(
orders => Console.WriteLine($"Current orders: {orders.Count()}"),
error => Console.WriteLine(error.Message));
}

View C# snippet source on GitHub

Replace your displayed list with each emission rather than appending items to it. The first emission includes every stored instance, or an empty set when there are none. There is no default 50-instance window. For a collection that could grow too large to keep in memory, use a paginated materialized observation.

Chronicle uses the registered container name and converts stored keys to your read model’s key property; a property named Id is not required. This works with MongoDB, SQL, and the in-memory sink without opening a collection yourself. The kernel releases personally identifiable information (PII) and encrypted values before sending the collection.

The namespace is the event store’s namespace at the time you call Observe. Keep observations within the lifetime of the tenant scope that created them.

Pass an expression when the consumer only needs a subset of each snapshot:

using System.Reactive.Linq;
using Cratis.Chronicle.ReadModels;
public static class ConfirmedOrderSubscription
{
public static IDisposable Subscribe(IReadModels readModels) => readModels
.Observe<Order>(order => order.Status == OrderStatus.Confirmed)
.Subscribe(
orders => Console.WriteLine($"Confirmed orders: {orders.Count()}"),
error => Console.WriteLine(error.Message));
}

View C# snippet source on GitHub

The expression is compiled once and evaluated client-side after release. It narrows the list the consumer receives, not what the server reads or sends. If you need database filtering, sorting, or paging, query the sink directly rather than loading the full collection first.

Return the subject directly from an observable query when the caller should receive the same replacement collections:

using Cratis.Chronicle.ReadModels;
using Reactive = System.Reactive.Subjects;
// Add these query methods to a read model maintained by a projection or reducer.
public record LiveOrder(string OrderId, OrderStatus Status)
{
public static Reactive.ISubject<IEnumerable<LiveOrder>> All(IReadModels readModels) =>
readModels.Observe<LiveOrder>();
public static Reactive.ISubject<IEnumerable<LiveOrder>> ByStatus(
OrderStatus status, IReadModels readModels) =>
readModels.Observe<LiveOrder>(order => order.Status == status);
}

View C# snippet source on GitHub

The query’s consumer owns the subscription lifetime. Use read-model watching instead when it needs the key, removal flag, or event context of an individual change.

Each call creates its own observation, independent of Watch and other calls to Observe. The server stream starts with the first subscriber. Additional subscribers share that stream and immediately receive its latest collection.

Disposing the last subscription closes the stream. Calling OnCompleted on the subject or disposing the subject also closes it. A stopped observation does not reopen: call Observe again to start a new one.

Stream, deserialization, and filter failures reach subscribers through OnError. A SQL sink retries failed polls internally, but once the observation itself errors, Chronicle does not automatically resubscribe. The consumer must call Observe again if it wants a new stream.

Materialized reads are eventually consistent with the event log. SQL polls for changes, so several writes between polls can appear as one snapshot. Use Watch when you need projection changesets rather than replacement collections.

The .NET API rejects read models it cannot observe:

Condition Result
A custom IReadModels implementation does not override or forward Observe ReadModelCannotBeObserved; its existing APIs remain usable
A passive read model has no materialized sink ReadModelCannotBeObserved
An event-target type publishes events rather than storing a collection ReadModelCannotBeObserved
No known projection or reducer maintains the type UnknownReadModel

The testing event store rebuilds collections from its event log. Use it to test the list and filter behavior, not to verify server streams or database notifications.