Quick Start

This is a basic guide to get your first queue working.

Install

Install using yarn:

$ yarn add bullmq

Bull is written in typescript, and although it can be used in vanilla javascript, all examples in this guide will be written in typescript.

Import into your project and add some jobs:

import { Queue } from 'bullmq';
const myQueue = new Queue('foo');
async function addJobs(){
await myQueue.add('myJobName', { foo: 'bar' });
await myQueue.add('myJobName', { qux: 'baz' });
}
addJobs();

You need to have a Redis service running in your local computer to run these examples successfully. You can read more about redis connections here.

Jobs are added to the queue and can be processed at any time, with at least one Nodejs process running a worker:

import { Worker } from 'bullmq'
const worker = new Worker(queueName, async job => {
// Will print { foo: 'bar'} for the first job
// and { qux: 'baz' } for the second.
console.log(job.data):
});

You can have has many worker processes you want, BullMQ will distribute the jobs across your workers in a round robin fashion.

You can listen to completed (or failed) jobs by attaching listeners to the workers:

worker.on('completed', (job) => {
console.log(`${job.id} has completed!`);
});
worker.on('failed', (job, err) => {
console.log(`${job.id} has failed with ${err.message}`);
});

There are many other events available, check the Guide or the API reference for more information.

Sometimes you need to listen to all the workers events in a given place, for this you need to use a special classQueueEvents:

import { QueueEvents } from 'bullmq'
const queueEvents = new QueueEvents();
queueEvents.on('completed', (jobId) => {
console.log(`${jobId} has completed!`);
});
queueEvents.on('failed', (jobId, err) => {
console.log(`${jobId} has failed with ${err.message}`);
});

Note that the global events listeners do only return the job Id, not the job instance. This is for performance reasons, if you need the complete job you can always use theQueue##getJob method.