Handling asynchronous tasks with Node.js and BullMQ involves setting up a job queue using BullMQ and executing tasks in a worker process. Here's a step-by-step guide:
Installation:Make sure you have Node.js installed. Then, set up a Node.js project and install BullMQ:
npm install bullmq
Using BullMQ to Handle Asynchronous Tasks:
-
Setting up a Queue:
Create a BullMQ queue to handle your asynchronous tasks. Define the queue and connect it to a Redis instance.
const { Queue } = require('bullmq'); // Create a queue named 'myQueue' const myQueue = new Queue('myQueue', { connection: { host: '127.0.0.1', // Redis server host port: 6379, // Redis server port }, });
-
Adding Jobs to the Queue:
Enqueue jobs into the BullMQ queue.
// Enqueue a job async function addJob(data) { await myQueue.add('taskName', data); // 'taskName' is the name of the task }
-
Processing Jobs in a Worker:
Create a worker process that listens to and processes jobs from the queue.
const { Worker } = require('bullmq'); // Create a worker to process jobs const worker = new Worker('myQueue', async (job) => { const { data } = job; // Perform the task with the job data console.log(`Processing job with data: ${data}`); // Your processing logic here });
-
Handling Job Completion and Failures:
Handle successful completion and failures of jobs within the worker.
worker.on('completed', (job) => { console.log(`Job ${job.id} completed with result: ${job.returnvalue}`); }); worker.on('failed', (job, err) => { console.log(`Job ${job.id} failed with error: ${err}`); });
-
Enqueueing Jobs and Testing:
// Enqueue a job (for testing purposes) addJob({ message: 'Hello, BullMQ!' });
-
Replace the Redis connection details (
host, port
) with your actual Redis server configuration. - Customize the task logic inside the worker function where the job processing occurs.
- BullMQ provides more advanced features such as job retries, delay, and prioritization. Refer to the documentation for detailed configuration options.
This basic setup demonstrates how to handle asynchronous tasks using BullMQ in Node.js. It allows you to enqueue tasks, process them asynchronously in a worker, and handle successful completions or failures of those tasks.