What is an event pattern in NestJS event-based communication?
An event pattern is a unique identifier string used to listen for or emit specific events between microservices or modules in NestJS. It helps match events with their handlers.
Click to reveal answer
beginner
How do you define an event handler for a specific event pattern in NestJS?
You use the @EventPattern('patternName') decorator on a method inside a controller or service. This method will run when an event with that pattern is received.
Click to reveal answer
intermediate
What is the difference between @EventPattern and @MessagePattern in NestJS?
@EventPattern listens for events that do not expect a response (fire-and-forget). @MessagePattern listens for messages that expect a response (request-response).
Click to reveal answer
beginner
How do you emit an event with a pattern in NestJS?
You use the ClientProxy's emit method with the event pattern string and the data payload. For example: client.emit('order_created', { id: 1 }).
Click to reveal answer
intermediate
Why are event patterns useful in microservice architectures?
They allow services to communicate asynchronously and loosely coupled by sending and listening to events identified by patterns, improving scalability and flexibility.
Click to reveal answer
Which decorator do you use to listen for an event pattern in NestJS?
A@SubscribeEvent
B@MessagePattern
C@EventPattern
D@ListenEvent
✗ Incorrect
The @EventPattern decorator is used to listen for events matching a specific pattern.
What method do you call to emit an event from a NestJS client proxy?
Aemit()
Bdispatch()
Csend()
Dtrigger()
✗ Incorrect
The emit() method sends an event without expecting a response.
What is the main difference between @EventPattern and @MessagePattern?
A@EventPattern expects a response, @MessagePattern does not
B@EventPattern is fire-and-forget, @MessagePattern expects a response
C@EventPattern is for HTTP, @MessagePattern is for TCP
The @Payload decorator requires parentheses even if no arguments are passed, so it should be @Payload().
Step 2: Verify other parts
Event name format is correct, method name can be anything, and parameter order is flexible.
Final Answer:
Missing parentheses in @Payload decorator -> Option A
Quick Check:
@Payload() needs parentheses [OK]
Hint: Always write @Payload() with parentheses, even if empty [OK]
Common Mistakes:
Omitting parentheses in decorators
Thinking method names must follow event names
Assuming parameter order matters for decorators
5. You want to create a NestJS service that listens to two different events: 'payment_success' and 'payment_failed'. Which of the following is the best way to implement this using event patterns?
hard
A. Use @Controller with two routes named after events
B. Create two methods, each decorated with @EventPattern for each event name
C. Create one method with @EventPattern('payment') and check event type inside
D. Use @Payload() decorator twice in one method for both events
Solution
Step 1: Understand event pattern usage
Each event should have its own handler method decorated with @EventPattern specifying the event name.
Step 2: Evaluate other options
Create one method with @EventPattern('payment') and check event type inside is less clear and not idiomatic; Use @Controller with two routes named after events is for HTTP routes, not events; Use @Payload() decorator twice in one method for both events is invalid syntax.
Final Answer:
Create two methods, each decorated with @EventPattern for each event name -> Option B
Quick Check:
Separate methods for separate events [OK]
Hint: Use one @EventPattern per event with separate methods [OK]
Common Mistakes:
Trying to handle multiple events in one method without checking
Confusing event handlers with HTTP controllers
Using multiple @Payload decorators in one method
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.