What is better-queue?
better-queue is a simple and efficient job queue for Node.js. It allows you to manage and process tasks asynchronously, with features like priority handling, concurrency control, and job persistence.
What are better-queue's main functionalities?
Basic Queue
This code demonstrates how to create a basic queue and add tasks to it. The tasks are processed asynchronously.
const Queue = require('better-queue');
let q = new Queue(function (input, cb) {
console.log('Processing:', input);
cb(null, input);
});
q.push('Task 1');
q.push('Task 2');
Concurrency Control
This code shows how to set up a queue with concurrency control, allowing up to 2 tasks to be processed simultaneously.
const Queue = require('better-queue');
let q = new Queue(function (input, cb) {
console.log('Processing:', input);
setTimeout(() => cb(null, input), 1000);
}, { concurrent: 2 });
q.push('Task 1');
q.push('Task 2');
q.push('Task 3');
Priority Handling
This code demonstrates how to add tasks with different priorities to the queue. Higher priority tasks are processed first.
const Queue = require('better-queue');
let q = new Queue(function (input, cb) {
console.log('Processing:', input);
cb(null, input);
});
q.push('Low Priority Task', { priority: 10 });
q.push('High Priority Task', { priority: 1 });
Job Persistence
This code shows how to set up job persistence using an in-memory store. This allows tasks to be stored and retrieved even if the process is restarted.
const Queue = require('better-queue');
const Store = require('better-queue-memory');
let q = new Queue(function (input, cb) {
console.log('Processing:', input);
cb(null, input);
}, { store: new Store() });
q.push('Persistent Task');
Other packages similar to better-queue
bull
Bull is a popular Redis-based queue for Node.js. It offers advanced features like job retries, rate limiting, and job scheduling. Compared to better-queue, Bull is more feature-rich and suitable for larger-scale applications that require persistent storage and advanced job management.
kue
Kue is another Redis-based priority job queue for Node.js. It provides a simple API for creating, processing, and monitoring jobs. Kue is similar to better-queue in terms of ease of use but offers additional features like job event handling and a built-in UI for monitoring.
bee-queue
Bee-Queue is a high-performance Redis-based job queue for Node.js. It focuses on speed and simplicity, making it a good choice for applications that require fast job processing. Compared to better-queue, Bee-Queue is more performant but requires Redis for job storage.