0
0
NestJSframework~20 mins

Queue producers in NestJS - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Queue Producer Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
What happens when a message is sent to a NestJS queue producer?

Consider a NestJS service using a queue producer to send messages. What is the expected behavior when this.queueProducer.send('task', { data: 123 }) is called?

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

@Injectable()
export class TaskService {
  constructor(private readonly queueProducer: ClientProxy) {}

  async sendTask() {
    await this.queueProducer.send('task', { data: 123 }).toPromise();
  }
}
AThe message { data: 123 } is sent to the 'task' queue and the method waits for a response.
BThe message is logged locally but not sent to any queue.
CThe method throws a runtime error because send() does not exist on ClientProxy.
DThe message is sent but the method does not wait for any acknowledgment or response.
Attempts:
2 left
💡 Hint

Remember that send() returns an Observable that you can convert to a Promise to wait for a response.

📝 Syntax
intermediate
2:00remaining
Identify the correct syntax to inject a queue producer in a NestJS service

Which option shows the correct way to inject a queue producer client proxy named 'TASK_QUEUE' into a NestJS service constructor?

Aconstructor(@InjectQueue('TASK_QUEUE') private readonly client: ClientProxy) {}
Bconstructor(private readonly client: ClientProxy('TASK_QUEUE')) {}
Cconstructor(@InjectClient('TASK_QUEUE') private client: ClientProxy) {}
Dconstructor(@Inject('TASK_QUEUE') private readonly client: ClientProxy) {}
Attempts:
2 left
💡 Hint

Use the @Inject() decorator with the token name to inject providers.

🔧 Debug
advanced
2:00remaining
Why does this NestJS queue producer code fail to send a message?

Given the following code snippet, why does calling sendTask() fail to send the message?

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

@Injectable()
export class TaskService {
  constructor(private readonly client: ClientProxy) {}

  sendTask() {
    this.client.send('task', { id: 1 });
  }
}
ABecause <code>ClientProxy</code> must be injected with a token, and here it is injected directly causing undefined client.
BBecause <code>send()</code> requires a callback function as the third argument, which is missing.
CBecause <code>send()</code> returns an Observable and the code does not subscribe or await it, causing no message to be sent.
DBecause <code>send()</code> is not a method of <code>ClientProxy</code> and causes a TypeError.
Attempts:
2 left
💡 Hint

Think about how Observables work in NestJS microservices.

state_output
advanced
2:00remaining
What is the value of response after sending a message with a NestJS queue producer?

Assuming the queue consumer replies with { status: 'ok' }, what will be the value of response after this code runs?

NestJS
const response = await client.send('task', { id: 42 }).toPromise();
A{ status: 'ok' }
Bundefined
CAn Observable object
DA Promise object
Attempts:
2 left
💡 Hint

Remember that send() returns an Observable and toPromise() waits for the response.

🧠 Conceptual
expert
2:00remaining
Which statement best describes the role of a queue producer in NestJS microservices?

Choose the most accurate description of what a queue producer does in a NestJS microservice architecture.

AIt listens for incoming messages from clients and processes them synchronously.
BIt sends messages to a message broker queue to trigger asynchronous processing by consumers.
CIt manages database transactions and ensures data consistency across services.
DIt acts as a web server handling HTTP requests and responses.
Attempts:
2 left
💡 Hint

Think about the producer-consumer pattern in messaging systems.