0
0
NestJSframework~20 mins

Queue consumers (processors) in NestJS - Mini Project: Build & Apply

Choose your learning style9 modes available
Queue Consumers (Processors) in NestJS
📖 Scenario: You are building a simple task processing system using NestJS. Tasks are added to a queue, and a consumer (processor) picks up tasks to process them one by one.This is like a restaurant kitchen where orders (tasks) come in, and a chef (consumer) prepares each order in turn.
🎯 Goal: Create a NestJS queue consumer (processor) that listens to a queue named task_queue and processes incoming tasks by logging their content.
📋 What You'll Learn
Create a queue named task_queue in the consumer service
Configure a consumer (processor) method to handle jobs from task_queue
Log the job data inside the processor method
Use NestJS Bull module patterns for queue and processor setup
💡 Why This Matters
🌍 Real World
Queue consumers are used in real applications to process background tasks like sending emails, resizing images, or processing payments without blocking the main app.
💼 Career
Understanding queue consumers is important for backend developers working with microservices, distributed systems, or any app needing reliable background task processing.
Progress0 / 4 steps
1
Set up the queue consumer service
Create a NestJS service class called TaskConsumerService and import Processor and Process decorators from @nestjs/bull. Use the @Processor('task_queue') decorator on the class.
NestJS
Need a hint?

Use @Processor('task_queue') above the class to link it to the queue.

2
Add a processor method to handle jobs
Inside TaskConsumerService, add a method called handleTask decorated with @Process() to process jobs from the queue. The method should accept a single parameter job of type any.
NestJS
Need a hint?

The @Process() decorator marks the method as a job processor.

3
Log the job data inside the processor method
Inside the handleTask method, add a line to log the job's data property using console.log(job.data).
NestJS
Need a hint?

Use console.log(job.data) to see the task details when processed.

4
Export the consumer service for use in the module
Add export keyword before the TaskConsumerService class declaration to make it available for import in other parts of the application.
NestJS
Need a hint?

The class must be exported so NestJS can use it as a provider.