Event-Driven Architecture
Relevant source files
The following files were used as context for generating this wiki page:
- packages/core/src/lib/event-bus/event-bus.module.ts
- packages/core/src/lib/event-bus/event-bus.ts
- packages/core/src/lib/time-tracking/timer/events/timer-started.event.ts
- packages/core/src/lib/time-tracking/timer/handlers/timer-started.handler.ts
- packages/plugins/integration-upwork/src/lib/upwork.service.ts
- README.md
The Gauzy platform utilizes an event-driven architecture to facilitate communication between different modules and services in a loosely coupled manner. This approach is built on the Command Query Responsibility Segregation (CQRS) pattern provided by the NestJS framework (@nestjs/cqrs). It allows various parts of the application to react to significant occurrences (events) without being directly dependent on the components that trigger them.
This system is composed of three primary components:
- Events: Simple classes that represent a specific action or state change that has occurred.
- Event Publishers: Services that create and dispatch events to a central event bus.
- Event Handlers: Classes that listen for specific events and execute business logic in response.
Core Components
The foundation of the event system is provided by a custom EventBus and its corresponding NestJS module.
EventBusModule
The EventBusModule is responsible for making the eventing capabilities available throughout the application for dependency injection. It achieves this by importing and re-exporting the CqrsModule from NestJS.
// packages/core/src/lib/event-bus/event-bus.module.ts:1-11
import { Module } from '@nestjs/common';
import { CqrsModule } from '@nestjs/cqrs';
import { EventBus } from './event-bus';
@Module({
imports: [CqrsModule],
providers: [EventBus],
exports: [EventBus]
})
export class EventBusModule {}
Sources: packages/core/src/lib/event-bus/event-bus.module.ts
EventBus
The EventBus class is a project-specific abstraction that extends the EventBus from @nestjs/cqrs. While it does not add new functionality, it provides a consistent, named provider for publishing events within the Gauzy ecosystem. It exposes publish() and publishAll() methods for dispatching single or multiple events.
// packages/core/src/lib/event-bus/event-bus.ts:1-8
import { Injectable } from '@nestjs/common';
import { EventBus as NestEventBus } from '@nestjs/cqrs';
@Injectable()
export class EventBus extends NestEventBus {
constructor() {
super();
}
}
Sources: packages/core/src/lib/event-bus/event-bus.ts
The diagram below illustrates the simple inheritance structure of the EventBus.
Event Lifecycle
The event lifecycle consists of three stages: definition, publishing, and handling.
1. Defining an Event
Events are plain classes that encapsulate data related to a specific occurrence. For instance, the TimerStartedEvent is created whenever a user starts a time-tracking timer. Its purpose is to carry the newly created ITimeLog object to any interested listeners.
// packages/core/src/lib/time-tracking/timer/events/timer-started.event.ts:1-8
import { ITimeLog } from '@gauzy/contracts';
export class TimerStartedEvent {
constructor(
public readonly timeLog: ITimeLog
) {}
}
Sources: packages/core/src/lib/time-tracking/timer/events/timer-started.event.ts
The class diagram for this event is straightforward, showing its data payload.
2. Publishing an Event
Events are published by services after a relevant action is completed. The publisher injects the EventBus and uses the publish() method to dispatch the event.
For example, the UpworkService publishes a TimerStartedEvent after creating a TimeLog record. This decouples the timer creation logic from any subsequent actions that need to happen, such as sending notifications or updating other parts of the system.
// packages/plugins/integration-upwork/src/lib/upwork.service.ts:114-116
this.eventBus.publish(
new TimerStartedEvent(timeLog)
);
Sources: packages/plugins/integration-upwork/src/lib/upwork.service.ts:115-117
The following sequence diagram shows how a service publishes an event.
3. Handling an Event
Event Handlers are responsible for listening to specific events and executing business logic. A handler is a class decorated with @EventsHandler(...) and must implement the IEventHandler<T> interface, where T is the event type it handles.
The TimerStartedHandler listens for the TimerStartedEvent. Its handle() method is automatically invoked by the event bus when the event is published. The example below shows a simple handler that logs a message to the console, but in a real-world scenario, it could trigger more complex workflows.
// packages/core/src/lib/time-tracking/timer/handlers/timer-started.handler.ts:5-15
import { EventsHandler, IEventHandler } from '@nestjs/cqrs';
import { TimerStartedEvent } from '../events/timer-started.event';
@EventsHandler(TimerStartedEvent)
export class TimerStartedHandler implements IEventHandler<TimerStartedEvent> {
constructor() {}
async handle(event: TimerStartedEvent) {
const { timeLog } = event;
console.log('Timer started for employee %s', timeLog.employeeId);
}
}
Sources: packages/core/src/lib/time-tracking/timer/handlers/timer-started.handler.ts
The relationship between the handler, the event, and the interface is shown below.
End-to-End Flow
The following diagram illustrates the complete, end-to-end flow of an event from publication to handling, using the timer example.
Summary
Gauzy's event-driven architecture, powered by NestJS CQRS, provides a robust mechanism for creating scalable and maintainable applications. By decoupling components, it allows developers to add or modify functionality in response to application events without altering the source of those events. This separation of concerns is key to managing complexity as the platform grows.