Consider a NestJS application where an event is emitted using this.eventEmitter.emit('user.created', user). What is the expected behavior if there is a listener subscribed to the 'user.created' event?
import { Injectable } from '@nestjs/common'; import { EventEmitter2 } from '@nestjs/event-emitter'; @Injectable() export class UserService { constructor(private eventEmitter: EventEmitter2) {} createUser(user) { // user creation logic this.eventEmitter.emit('user.created', user); } } // Listener example import { Injectable } from '@nestjs/common'; import { OnEvent } from '@nestjs/event-emitter'; @Injectable() export class UserListener { @OnEvent('user.created') handleUserCreatedEvent(payload) { console.log('User created:', payload); } }
Think about how event emitters work in NestJS and whether events are synchronous or asynchronous.
In NestJS, when an event is emitted, all listeners subscribed to that event are immediately called with the payload. If no listener exists, the event is simply ignored without error.
Which of the following code snippets correctly listens to an event named 'order.completed' using NestJS event emitter?
Recall the decorator provided by NestJS for event listening.
The correct decorator to listen for events in NestJS is @OnEvent(). Other decorators like @EventListener or @ListenEvent do not exist in NestJS.
Given the following NestJS code, why does the listener not log anything when createUser is called?
import { Injectable } from '@nestjs/common';
import { EventEmitter2, OnEvent } from '@nestjs/event-emitter';
@Injectable()
export class UserService {
constructor(private eventEmitter: EventEmitter2) {}
createUser(user) {
this.eventEmitter.emit('user.created', user);
}
}
@Injectable()
export class UserListener {
@OnEvent('user.created')
handleUserCreated(payload) {
console.log('User created:', payload);
}
}Think about how NestJS creates instances of classes and when decorators like @OnEvent are activated.
In NestJS, event listeners decorated with @OnEvent only work if the class is instantiated by the framework. If UserListener is not injected or used anywhere, it is never created, so the listener method never runs.
Consider two listeners subscribed to the 'task.finished' event. When the event is emitted with payload { id: 42 }, what will be the console output?
import { Injectable } from '@nestjs/common';
import { OnEvent } from '@nestjs/event-emitter';
@Injectable()
export class ListenerOne {
@OnEvent('task.finished')
handleTask(payload) {
console.log('ListenerOne:', payload.id);
}
}
@Injectable()
export class ListenerTwo {
@OnEvent('task.finished')
handleTask(payload) {
console.log('ListenerTwo:', payload.id);
}
}
// Emitting event
this.eventEmitter.emit('task.finished', { id: 42 });Think about how event emitters handle multiple listeners for the same event.
When an event is emitted, all listeners subscribed to that event are called in order. Both ListenerOne and ListenerTwo will log their messages with the payload.
In a NestJS module, if you try to inject EventEmitter2 without importing EventEmitterModule, what error will you encounter at runtime?
Think about how NestJS dependency injection works and what happens if a provider is missing.
If EventEmitterModule is not imported, NestJS does not know how to provide EventEmitter2. This causes a dependency resolution error stating no provider found.