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?
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(); } }
Remember that send() returns an Observable that you can convert to a Promise to wait for a response.
In NestJS, ClientProxy.send() sends a message to the specified pattern (queue) and returns an Observable. Using toPromise() waits for the response, so the method waits until the message is processed.
Which option shows the correct way to inject a queue producer client proxy named 'TASK_QUEUE' into a NestJS service constructor?
Use the @Inject() decorator with the token name to inject providers.
In NestJS, to inject a provider by token, use @Inject('TOKEN') before the parameter. The other options use invalid decorators or syntax.
Given the following code snippet, why does calling sendTask() fail to send the message?
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 }); } }
Think about how Observables work in NestJS microservices.
The send() method returns an Observable. Without subscribing or awaiting it, the message is never sent. This causes the method to silently fail or not behave as expected.
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?
const response = await client.send('task', { id: 42 }).toPromise();
Remember that send() returns an Observable and toPromise() waits for the response.
The send() method returns an Observable that emits the consumer's reply. Using toPromise() waits for that reply, so response holds the actual reply object.
Choose the most accurate description of what a queue producer does in a NestJS microservice architecture.
Think about the producer-consumer pattern in messaging systems.
A queue producer's job is to send messages to a queue so that other services (consumers) can process them asynchronously. It does not handle HTTP requests or database transactions directly.