📖 Scenario: You are building a simple NestJS application that sends messages to a queue system. This is common in real-world apps where tasks are processed asynchronously, like sending emails or processing orders.
🎯 Goal: Create a NestJS queue producer that sends messages to a queue named task_queue. You will set up the data, configure the queue name, implement the message sending logic, and complete the producer service.
📋 What You'll Learn
Create a message object with specific content
Define a queue name variable
Implement a method to send the message to the queue
Complete the NestJS service class with the producer logic
💡 Why This Matters
🌍 Real World
Queue producers are used in applications to send tasks or events to be processed asynchronously, improving performance and user experience.
💼 Career
Understanding how to create queue producers is essential for backend developers working with microservices, event-driven architectures, and scalable systems.
Progress0 / 4 steps
1
Create the message data
Create a constant object called message with the exact properties: { taskId: 101, taskName: 'SendEmail' }.
NestJS
Hint
Use const to create an object named message with the given properties.
2
Define the queue name
Add a constant string variable called queueName and set it exactly to 'task_queue'.
NestJS
Hint
Use const queueName = 'task_queue'; to define the queue name.
3
Implement the sendMessage method
Inside a NestJS service class, write a method called sendMessage that takes no parameters and calls this.queueClient.send(queueName, message) to send the message.
NestJS
Hint
Define sendMessage() method that calls this.queueClient.send(queueName, message);
4
Complete the producer service
Add the @Injectable() decorator above the QueueProducerService class to complete the NestJS service setup.
NestJS
Hint
Import and add @Injectable() decorator above the class to make it a NestJS service.
Practice
(1/5)
1. What is the main role of a queue producer in a NestJS application?
easy
A. To monitor the queue status and logs
B. To process tasks received from the queue
C. To delete completed tasks from the queue
D. To send tasks or messages to a queue for later processing
Solution
Step 1: Understand the role of queue producer
A queue producer is responsible for sending tasks or messages to a queue, not processing them.
Step 2: Differentiate producer from consumer
Consumers process tasks, while producers only add tasks to the queue.
Final Answer:
To send tasks or messages to a queue for later processing -> Option D
Quick Check:
Queue producer = sends tasks [OK]
Hint: Producers add tasks; consumers process them [OK]
Common Mistakes:
Confusing producer with consumer
Thinking producer processes tasks
Assuming producer deletes tasks
2. Which of the following is the correct way to inject a queue named 'email' in a NestJS producer service?
easy
A. @InjectQueue('email') emailQueue: Queue = new Queue();
B. @InjectQueue private emailQueue: Queue('email');
C. @InjectQueue('email') private readonly emailQueue: Queue;
D. @InjectQueue('email') private emailQueue = Queue;
Solution
Step 1: Recall correct syntax for @InjectQueue
The decorator requires the queue name as a string and the property must be typed as Queue.
Step 2: Check property declaration
It should be private readonly with type Queue, no manual instantiation needed.
Final Answer:
@InjectQueue('email') private readonly emailQueue: Queue; -> Option C
5. You want to create a NestJS queue producer that sends different types of jobs to the same queue with different priorities. Which approach correctly implements this?
hard
A. Use queue.add(jobName, data, { priority: number }) to set priority per job
B. Create multiple queues with different names for each priority level
C. Set priority globally on the queue during injection with @InjectQueue
D. Use queue.add(jobName, data) and sort jobs manually later
Solution
Step 1: Understand job priority in queues
Queue jobs can have individual priorities set when added, using options like { priority: number }.
Step 2: Evaluate options
Creating multiple queues or setting priority globally is not the recommended way; manual sorting is inefficient.
Final Answer:
Use queue.add(jobName, data, { priority: number }) to set priority per job -> Option A
Quick Check:
Set priority per job with add() options [OK]
Hint: Set priority in add() options per job [OK]
Common Mistakes:
Trying to set priority globally on queue
Creating many queues instead of one
Ignoring built-in priority support
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.