0
0
NestJSframework~10 mins

Event patterns (event-based) in NestJS - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to create an event handler using the correct decorator.

NestJS
import { Controller } from '@nestjs/common';
import { EventPattern } from '@nestjs/microservices';

@Controller()
export class AppController {
  @[1]('order_created')
  handleOrderCreated(data: any) {
    console.log('Order created event received:', data);
  }
}
Drag options to blanks, or click blank then click option'
ASubscribeMessage
BEventPattern
CMessagePattern
DOnEvent
Attempts:
3 left
💡 Hint
Common Mistakes
Using @MessagePattern instead of @EventPattern
Using @SubscribeMessage which is for WebSocket events
Using @OnEvent which is for internal event emitter
2fill in blank
medium

Complete the code to emit an event named 'user_registered' with user data.

NestJS
import { ClientProxy } from '@nestjs/microservices';

export class UserService {
  constructor(private client: ClientProxy) {}

  registerUser(user: any) {
    // user registration logic
    this.client.[1]('user_registered', user);
  }
}
Drag options to blanks, or click blank then click option'
Asend
Bpublish
Cdispatch
Demit
Attempts:
3 left
💡 Hint
Common Mistakes
Using send which expects a response
Using publish which is not a ClientProxy method
Using dispatch which does not exist
3fill in blank
hard

Fix the error in the event handler method signature to properly receive event data.

NestJS
import { Controller } from '@nestjs/common';
import { EventPattern } from '@nestjs/microservices';

@Controller()
export class PaymentController {
  @EventPattern('payment_processed')
  handlePayment([1]) {
    console.log('Payment processed:', data);
  }
}
Drag options to blanks, or click blank then click option'
Adata
Bevent
Cpayload
Dmessage
Attempts:
3 left
💡 Hint
Common Mistakes
Using a parameter name different from what is used inside the method
Not defining any parameter to receive event data
4fill in blank
hard

Fill both blanks to create a service that listens to 'inventory_updated' events and logs the update.

NestJS
import { Injectable } from '@nestjs/common';
import { [1] } from '@nestjs/microservices';

@Injectable()
export class InventoryService {
  @[2]('inventory_updated')
  handleInventoryUpdate(update: any) {
    console.log('Inventory updated:', update);
  }
}
Drag options to blanks, or click blank then click option'
AEventPattern
BMessagePattern
CSubscribeMessage
DOnEvent
Attempts:
3 left
💡 Hint
Common Mistakes
Importing and using different decorators
Using MessagePattern which is for request-response
5fill in blank
hard

Fill all three blanks to create a client service that emits 'notification_sent' events with a message payload.

NestJS
import { Injectable } from '@nestjs/common';
import { [1] } from '@nestjs/microservices';

@Injectable()
export class NotificationService {
  constructor(private client: [2]) {}

  sendNotification(message: string) {
    this.client.[3]('notification_sent', { message });
  }
}
Drag options to blanks, or click blank then click option'
AClientProxy
Cemit
Dsend
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'send' instead of 'emit' for events
Mismatching injected client type and import