Introducing Socket Firewall: Free, Proactive Protection for Your Software Supply Chain.Learn More
Socket
Book a DemoInstallSign in
Socket

@nestjs/event-emitter

Package Overview
Dependencies
Maintainers
2
Versions
22
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@nestjs/event-emitter

Nest - modern, fast, powerful node.js web framework (@event-emitter)

latest
Source
npmnpm
Version
3.0.1
Version published
Weekly downloads
668K
-2.07%
Maintainers
2
Weekly downloads
 
Created

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

FAQs

Package last updated on 24 Feb 2025

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts