What is @nestjs/event-emitter?
@nestjs/event-emitter is a module for NestJS that provides an event-driven architecture. It allows you to emit and listen to events within your NestJS application, making it easier to decouple different parts of your application and improve maintainability.
What are @nestjs/event-emitter's main functionalities?
Event Emission
This feature allows you to emit events from any part of your application. In this example, the `MyService` class emits an event named 'my.event' with some data.
const { Injectable } = require('@nestjs/common');
const { EventEmitter2 } = require('@nestjs/event-emitter');
@Injectable()
class MyService {
constructor(private eventEmitter: EventEmitter2) {}
triggerEvent() {
this.eventEmitter.emit('my.event', { data: 'some data' });
}
}
Event Listener
This feature allows you to listen to events emitted within your application. In this example, the `MyListener` class listens for the 'my.event' event and handles it by logging the payload.
const { Injectable } = require('@nestjs/common');
const { OnEvent } = require('@nestjs/event-emitter');
@Injectable()
class MyListener {
@OnEvent('my.event')
handleMyEvent(payload) {
console.log('Event received:', payload);
}
}
Event Payload Validation
This feature allows you to validate the payload of events. In this example, the `MyEventPayload` class uses class-validator to ensure that the `data` property is a string. The `MyListener` class listens for the 'my.event' event and handles it asynchronously.
const { Injectable } = require('@nestjs/common');
const { OnEvent } = require('@nestjs/event-emitter');
const { IsString } = require('class-validator');
class MyEventPayload {
@IsString()
data;
}
@Injectable()
class MyListener {
@OnEvent('my.event', { async: true })
async handleMyEvent(payload: MyEventPayload) {
console.log('Validated event received:', payload);
}
}
Other packages similar to @nestjs/event-emitter
eventemitter2
EventEmitter2 is a Node.js module that extends the EventEmitter class to provide additional features such as namespaced events, wildcards, and more. It is more general-purpose compared to @nestjs/event-emitter, which is specifically designed for NestJS applications.
rxjs
RxJS is a library for reactive programming using Observables. It provides powerful operators for event handling, transformation, and composition. While RxJS is more complex and feature-rich, @nestjs/event-emitter offers a simpler and more straightforward API for event-driven architecture in NestJS.
mitt
Mitt is a tiny (~200 bytes) functional event emitter. It is extremely lightweight and easy to use, but lacks some of the advanced features provided by @nestjs/event-emitter, such as integration with NestJS's dependency injection system.
A progressive Node.js framework for building efficient and scalable server-side applications.
Description
Events module for Nest built on top of the eventemitter2 package.
Installation
$ npm i --save @nestjs/event-emitter
Quick Start
Overview & Tutorial
Support
Nest is an MIT-licensed open source project. It can grow thanks to the sponsors and support by the amazing backers. If you'd like to join them, please read more here.
Stay in touch
License
Nest is MIT licensed.