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
Steps
- Server Boot:
app.tscallsstartConsumer() - Registration:
@repo/crm-eventsregisters local process or connects to message broker - Graceful Shutdown: Cleanup function stops listeners before process exits
Observability
Every event is logged with metadata:
| Field | Description |
|---|---|
eventName | e.g., Organization.Created |
payloadId | Event identifier |
status | SUCCESS or ERROR |
duration | Processing time |
Error Handling
If a subscriber throws, the consumer triggers the retry mechanism of the underlying event bus.