Skip to content

Cratis Chronicle reactors: create your first handler

A Cratis Chronicle reactor connects a recorded event to application work, such as sending an order notification. Define the event it handles, add the handler, then register the reactor with your client so Chronicle can deliver events to it.

Start with a running Chronicle kernel and a connected client. If you still need those, follow Get started with Cratis Chronicle first. The examples below show reactor declarations and registration, not a complete application.

Use a projection or reducer instead when you need queryable state rather than a side effect.

Choose the fact that should trigger the work. The order-placed event below carries the information the notification handler needs. It is a normal Chronicle event with its own event type identity, not a special message format for reactors.

[EventType]
public record ReactorOrderPlaced(string CustomerEmail, decimal TotalAmount);

View C# snippet source on GitHub

Add the handler to a reactor type or module. The client determines how it finds that handler: .NET uses the first parameter’s event type, Kotlin and Java use event-typed methods, TypeScript supports event-name conventions, and Elixir uses @handles with handle/2. Use the event context when the work needs metadata such as the event source ID or sequence number.

public interface ReactorEmailGateway
{
Task SendOrderPlaced(string email, decimal amount, DateTimeOffset occurred);
}
public class OrderNotificationsReactor(ReactorEmailGateway emailGateway) : IReactor
{
public Task Placed(ReactorOrderPlaced @event, EventContext context) =>
emailGateway.SendOrderPlaced(
@event.CustomerEmail,
@event.TotalAmount,
context.Occurred);
}

View C# snippet source on GitHub

Register the reactor in the application that will run its handler. The .NET host can discover reactors at startup; the examples also show explicit registration. Kotlin and Java register an instance, TypeScript registers discovered reactor classes, and Elixir includes the module in client startup options.

public class ReactorRegistration
{
public Task Register(IEventStore eventStore) => eventStore.Reactors.Register();
}

View C# snippet source on GitHub

Give the reactor an explicit ID before deploying it. Clients otherwise derive the identifier from the type or module name, so a rename can make Chronicle see a different observer. Keeping the ID stable preserves the connection to its stored observation position.

[Reactor(id: "order-notifications")]
public class NamedOrderNotificationsReactor : IReactor
{
public Task Placed(ReactorOrderPlaced @event) => Task.CompletedTask;
}

View C# snippet source on GitHub

A newly registered reactor normally catches up on existing history, so a notification handler can run for old orders as well as new ones. If it should handle only future events, read Start at the Tail before deploying it. Excluding replay alone does not exclude initial catch-up.

The same event can also arrive again during recovery or retry. Make the effect idempotent: repeating the operation must not repeat its business outcome. For an external API, pass an idempotency key that the receiving system enforces; Delivery Identity explains the identity Chronicle supplies.

With the reactor registered, append an event of the type it handles and check the handler’s effect. If it does not run, inspect the reactor’s state and failed partitions before appending the event again.

Read Event Processing when your handler needs context or dependencies, or Filtering when only some events of a handled type should reach it. Tagging reactors labels observers for organization; it does not filter delivery.

For the data-building alternative, compare reducers and projections.