Bull for JavaScript (Node.js): Complete Guide with Examples

Online Python Trainer for Beginners

Learn Python easily without overwhelming theory. Solve practical tasks with automatic checking, get hints in Russian, and write code directly in your browser — no installation required.

Start Course

What is Bull and why do you need it?

\n\n

Bull is a powerful and popular library for managing job queues in the Node.js environment. It is built on top of Redis and provides a reliable, scalable, and flexible way to handle background tasks. If your application needs to perform operations that should not block the main thread (for example, sending emails, image processing, PDF generation, synchronization with external APIs, data parsing), Bull is the ideal solution.

\n\n

Why use Bull?

\n
    \n
  • Asynchronous processing: Offloads the main server by moving heavy tasks to a background process.
  • \n
  • Reliability: Tasks are stored in Redis, so they are not lost when the application restarts or a worker crashes.
  • \n
  • Retries: Built-in support for automatic retries on errors.
  • \n
  • Scheduling: Ability to delay tasks or run them on a schedule (cron).
  • \n
  • Priorities: Control the order of task execution.
  • \n
  • Monitoring: Easily track queue status using the built-in UI (Bull Board).
  • \n
\n\n

Installing Bull

\n\n

To get started, make sure you have Redis installed and running. Bull does not work without it.

\n\n
    \n
  1. Installing Redis (example for Ubuntu/Debian):\n
    sudo apt update\nsudo apt install redis-server\nsudo systemctl start redis
    \n
  2. \n
  3. Installing Bull via npm:\n
    npm install bull
    \n
  4. \n
  5. Installing Bull Board (optional, for web interface):\n
    npm install @bull-board/express @bull-board/api
    \n
  6. \n
\n\n

Quick Start: First Example

\n\n

Let's create a simple queue for sending welcome emails.

\n\n

Step 1: Create the Producer (producer.js)

\n
const Queue = require('bull');\n\n// Create a queue named 'emailQueue'\nconst emailQueue = new Queue('emailQueue', {\n    redis: { host: 'localhost', port: 6379 }\n});\n\n// Function to add a job to the queue\nasync function addEmailJob(to, subject, body) {\n    const job = await emailQueue.add(\n        { to, subject, body }, // Job data\n        {\n            attempts: 3, // Number of retries on error\n            backoff: { type: 'fixed', delay: 5000 } // Delay between retries (5 sec)\n        }\n    );\n    console.log(`Job added with ID: ${job.id}`);\n    return job;\n}\n\n// Example usage\naddEmailJob('user@example.com', 'Welcome!', 'Thank you for registering.')\n    .then(() => process.exit(0))\n    .catch(err => console.error(err));\n
\n\n

Step 2: Create the Worker (worker.js)

\n
const Queue = require('bull');\n\n// Connect to the same queue\nconst emailQueue = new Queue('emailQueue', {\n    redis: { host: 'localhost', port: 6379 }\n});\n\n// Job handler\nemailQueue.process(async (job) => {\n    console.log(`Processing job ${job.id}...`);\n    const { to, subject, body } = job.data;\n\n    // Simulate sending an email (in reality, use nodemailer)\n    await new Promise(resolve => setTimeout(resolve, 2000));\n    console.log(`Email sent to ${to}: ${subject}`);\n\n    // If something goes wrong, throw an error\n    // throw new Error('Send error');\n});\n\nconsole.log('Worker started and waiting for jobs...');\n
\n\n

Step 3: Run

\n

First, start the worker: node worker.js. Then, in another terminal, start the producer: node producer.js. You will see the job enter the queue and be processed.

\n\n

Main Bull Methods and Functions

\n\n

1. new Queue(name, [options]) — Creating a Queue

\n

What it does: Creates

Recommendations