0
0
NestJSframework~20 mins

Message patterns (request-response) in NestJS - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
NestJS Message Patterns Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
What is the output of this NestJS microservice handler?

Consider this NestJS microservice handler using a message pattern. What will be the response when the client sends a message with pattern 'sum' and data {a: 3, b: 4}?

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

@Controller()
export class MathController {
  @MessagePattern('sum')
  sum(data: { a: number; b: number }) {
    return data.a + data.b;
  }
}
A7
Bundefined
CThrows an error
DNaN
Attempts:
2 left
💡 Hint

Think about what the handler returns when it receives the data.

📝 Syntax
intermediate
2:00remaining
Which option correctly defines a message pattern handler in NestJS?

Choose the correct syntax to define a message pattern handler that listens for the pattern 'get_user'.

A
@MessagePattern('get_user')
getUser(data) => {
  return data;
}
B
@MessagePattern get_user
getUser(data) {
  return data;
}
C
@MessagePattern('get_user')
getUser {
  return data;
}
D
@MessagePattern('get_user')
getUser(data) {
  return data;
}
Attempts:
2 left
💡 Hint

Remember the decorator syntax requires parentheses and a string argument.

🔧 Debug
advanced
2:00remaining
Why does this NestJS microservice handler not respond to messages?

Given this code, why does the microservice never respond to messages with pattern 'ping'?

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

@Controller()
export class PingController {
  @MessagePattern('ping')
  ping() {
    console.log('Ping received');
  }
}
AThe pattern string 'ping' is invalid and does not match any messages.
BThe handler does not return any value, so the client waits indefinitely.
CThe @Controller decorator is missing required parameters.
DThe method name 'ping' conflicts with reserved keywords.
Attempts:
2 left
💡 Hint

Think about what happens if a handler does not return a response.

state_output
advanced
2:00remaining
What is the value of 'count' after these messages are processed?

Consider this NestJS microservice controller that counts how many times it received a 'count' message. What is the value of count after processing three messages with pattern 'count'?

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

@Controller()
export class CounterController {
  private count = 0;

  @MessagePattern('count')
  increment() {
    this.count++;
    return this.count;
  }
}
A3
B0
C1
DUndefined
Attempts:
2 left
💡 Hint

Each message triggers the increment method once.

🧠 Conceptual
expert
2:00remaining
Which statement about NestJS message patterns and request-response is true?

Choose the correct statement about how NestJS handles request-response message patterns in microservices.

AMessage patterns only support fire-and-forget messages and cannot return responses.
BNestJS automatically sends a default response if the handler method returns void or undefined.
CThe handler method must always return a value or a Promise to send a response back to the client.
DThe client must manually listen for responses; the handler does not send them automatically.
Attempts:
2 left
💡 Hint

Think about how request-response communication works in microservices.