My App
RoomKitBackend

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
ParameterTypeDescription
eventRoomKitEventThe 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
ParameterTypeDescription
typeRoomKitEventTypeThe event type to listen for
handler(event) => voidCallback 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
ParameterTypeDescription
typeRoomKitEventTypeThe event type to unsubscribe from
handler(event) => voidThe handler to remove

removeAllListeners

Remove all registered event handlers.

removeAllListeners(): void

Event Types

All events include metadata: { triggeredBy: string; timestamp: Date }.

EventPayloadEmitted By
BookingRequestedBookingBookingService.create
BookingConfirmedBookingBookingService.confirm
BookingStartedBookingBookingService.checkIn
BookingCompletedBookingBookingService.complete
BookingCancelledBookingBookingService.cancel
BookingModifiedBookingBookingService.modify, BookingService.delegate
ConflictDetectedConflictRecordConflictService
BlackoutCreatedBlackoutWindowBlackoutService.create
BlackoutImpactDetected{ blackout: BlackoutWindow; impactedBookings: Booking[] }BlackoutService.analyzeImpact
ExamSessionCreatedExamSessionExamService.createExamSession
BulkOperationProgressBulkOperationBulkOperationService
BulkOperationCompletedBulkOperationBulkOperationService

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
    }
}

On this page