Messaging & Events

PubSubService, domain events, @JobEvent, event dispatch, deduplication, job queues, and @JobDefinition executors.

Overview

  1. Domain Events — dispatched from services after writes, trigger job enqueuing and pub-sub publishing.
  2. PubSubService — real-time publish/subscribe for inter-process notification and GraphQL subscriptions.
  3. Job Queues — durable, at-least-once background job processing.

Defining Events

Events are @Serializable classes annotated with @JobEvent:

kotlin
@JobEvent ParameterPurpose
jobsJob classes to enqueue when dispatched
pubsubChannelChannel to publish to (empty = no publish)

Dispatching Events

KSP generates a dispatch() extension. Both job enqueuing and pub-sub publishing are deferred until after the transaction commits.

kotlin

Event Deduplication

The deferredEvents {} scope deduplicates events by (eventClass, identityKey()):

kotlin

Job Executors

A job executor extends AbstractJobExecutor<T> and is annotated with @JobDefinition:

kotlin

Job payload

kotlin

Full Event Flow

The complete lifecycle:

Event flow