EventBus
Typed domain event bus for reactive integrations
Overview
EventBus is an in-process, synchronous event bus for RoomKit domain events. All handlers are invoked synchronously when an event is emitted. If any handler throws, errors are aggregated into a single error.
import { EventBus, type RoomKitEvent, type RoomKitEventType } from '@roomkit/nestjs';Methods
emit
Dispatch an event to all registered handlers for that event type. If one or more handlers throw, their errors are aggregated and re-thrown as a single error.
emit(event: RoomKitEvent): void| Parameter | Type | Description |
|---|---|---|
event | RoomKitEvent | The domain event to dispatch |
eventBus.emit({
type: 'BookingRequested',
payload: booking,
metadata: { triggeredBy: 'user-42', timestamp: new Date() },
});on
Subscribe to a specific event type. Returns an unsubscribe function.
on<T extends RoomKitEventType>(type: T, handler: (event: RoomKitEvent<T>) => void): () => void| Parameter | Type | Description |
|---|---|---|
type | RoomKitEventType | The event type to listen for |
handler | (event) => void | Callback invoked when the event is emitted |
Returns () => void -- call this function to unsubscribe.
const unsubscribe = eventBus.on('BookingConfirmed', (event) => {
console.log('Booking confirmed:', event.payload.id);
});
// Later, unsubscribe
unsubscribe();off
Unsubscribe a specific handler from an event type.
off<T extends RoomKitEventType>(type: T, handler: (event: RoomKitEvent<T>) => void): void| Parameter | Type | Description |
|---|---|---|
type | RoomKitEventType | The event type to unsubscribe from |
handler | (event) => void | The handler to remove |
removeAllListeners
Remove all registered event handlers.
removeAllListeners(): voidEvent Types
All events include metadata: { triggeredBy: string; timestamp: Date }.
| Event | Payload | Emitted By |
|---|---|---|
BookingRequested | Booking | BookingService.create |
BookingConfirmed | Booking | BookingService.confirm |
BookingStarted | Booking | BookingService.checkIn |
BookingCompleted | Booking | BookingService.complete |
BookingCancelled | Booking | BookingService.cancel |
BookingModified | Booking | BookingService.modify, BookingService.delegate |
ConflictDetected | ConflictRecord | ConflictService |
BlackoutCreated | BlackoutWindow | BlackoutService.create |
BlackoutImpactDetected | { blackout: BlackoutWindow; impactedBookings: Booking[] } | BlackoutService.analyzeImpact |
ExamSessionCreated | ExamSession | ExamService.createExamSession |
BulkOperationProgress | BulkOperation | BulkOperationService |
BulkOperationCompleted | BulkOperation | BulkOperationService |
Registering Handlers
Via Module Options
Wire handlers at registration time. These are bound during module bootstrap.
import { Module } from '@nestjs/common';
import { RoomKitModule, PrismaRoomKitAdapter } from '@roomkit/nestjs';
@Module({
imports: [
RoomKitModule.register({
storage: new PrismaRoomKitAdapter(prisma),
priorities: [
{ name: 'academic', weight: 80 },
{ name: 'standard', weight: 40 },
],
events: {
BookingRequested: (event) => {
console.log('New booking request:', event.payload.id);
},
BookingCancelled: (event) => {
notificationService.notifyCancellation(event.payload);
},
ConflictDetected: (event) => {
alertService.sendConflictAlert(event.payload);
},
},
}),
],
})
export class AppModule {}Via Direct EventBus Injection
Inject the EventBus service and subscribe to events programmatically.
import { Injectable, OnModuleInit, OnModuleDestroy } from '@nestjs/common';
import { EventBus } from '@roomkit/nestjs';
@Injectable()
export class BookingNotificationService implements OnModuleInit, OnModuleDestroy {
private unsubscribers: (() => void)[] = [];
constructor(private readonly eventBus: EventBus) {}
onModuleInit() {
this.unsubscribers.push(
this.eventBus.on('BookingConfirmed', (event) => {
this.sendConfirmationEmail(event.payload);
}),
);
this.unsubscribers.push(
this.eventBus.on('BookingCancelled', (event) => {
this.sendCancellationEmail(event.payload);
}),
);
}
onModuleDestroy() {
this.unsubscribers.forEach((unsub) => unsub());
}
private sendConfirmationEmail(booking: any) {
// Send email logic
}
private sendCancellationEmail(booking: any) {
// Send email logic
}
}