Selegic CRM Docs
ServerPub-Sub

Consumer Bootstrap

How the server initializes event listeners on startup.

startConsumer Function

Located in features/pub-sub/setup/subscriber.ts, this function registers event listeners:

export function startConsumer() {
  const orgCreated = onEvent("Organization.Created", async (evt) => {
    await organizationCreatedSubscriber(evt as OrganizationCreatedEvent);
  });

  const seedOrg = onEvent("Organization.Seed", async (evt) => {
    await seedOrganizationSubscriber(evt as SeedOrganizationEvent);
  });

  // Returns cleanup function
  return () => {
    orgCreated();
    seedOrg();
  };
}

Lifecycle

Server Boot app.ts init phase Register event listeners Connect to event broker Consumer ready Shutdown Stop listeners Process events

Steps

  1. Server Boot: app.ts calls startConsumer()
  2. Registration: @repo/crm-events registers local process or connects to message broker
  3. Graceful Shutdown: Cleanup function stops listeners before process exits

Observability

Every event is logged with metadata:

FieldDescription
eventNamee.g., Organization.Created
payloadIdEvent identifier
statusSUCCESS or ERROR
durationProcessing time

Error Handling

If a subscriber throws, the consumer triggers the retry mechanism of the underlying event bus.

On this page