BullMQ is a Node.js library that uses Redis as a message broker to handle job queues. Here's an overview of how it functions:
add
method. These jobs are stored in Redis until they can be processed.const queue = new Queue('my-queue'); const job = await queue.add({ foo: 'bar' });
process
method specifies what to do with each job.const worker = new Worker('my-queue', async (job) => { // Do something with job data console.log(job.data.foo); });
const worker = new Worker('my-queue', async (job) => { // Do something with job data and return a result return {result: job.data.foo + ' processed'}; }); // Elsewhere... const job = await queue.getJob(jobId); if (job !== null) { const result = await job.finished(); console.log(result); // Logs: "{result: 'bar processed'}" }
const job = await queue.add( { foo: 'bar' }, { delay: 5000 } // Delay of 5 seconds );
const job = await queue.add( { foo: 'bar' }, { priority: 1 } // Lower numbers are higher priority );
const job = await queue.add( { foo: 'bar' }, { repeat: { every: 5000 } } // Repeats every 5 seconds );
queue.on('completed', (job, result) => { // Do something when a job completes console.log(`Job ${job.id} completed with result ${result}`); });
In these ways and more, BullMQ leverages Redis's capabilities to create a robust, flexible job queue system.
Dragonfly is fully compatible with the Redis ecosystem and requires no code changes to implement.