Quick Start
This guide will get you up and running with bunqueue in 5 minutes.
Create a Queue
import { Queue } from 'bunqueue/client';
// Create a typed queueinterface EmailJob { to: string; subject: string; body: string;}
const emailQueue = new Queue<EmailJob>('emails');Add Jobs
// Add a single jobconst job = await emailQueue.add('send-email', { to: 'user@example.com', subject: 'Welcome!', body: 'Thanks for signing up.'});
console.log(`Job created: ${job.id}`);
// Add with optionsawait emailQueue.add('send-email', data, { priority: 10, // Higher = processed first delay: 5000, // Wait 5 seconds before processing attempts: 3, // Retry up to 3 times backoff: 1000, // Wait 1 second between retries});
// Add multiple jobs (batch optimized)await emailQueue.addBulk([ { name: 'send-email', data: { to: 'a@test.com', subject: 'Hi', body: '...' } }, { name: 'send-email', data: { to: 'b@test.com', subject: 'Hi', body: '...' } },]);Create a Worker
import { Worker } from 'bunqueue/client';
const worker = new Worker<EmailJob>('emails', async (job) => { console.log(`Processing: ${job.name}`);
// Update progress await job.updateProgress(50, 'Sending email...');
// Do the work await sendEmail(job.data);
// Log messages await job.log('Email sent successfully');
// Return a result return { sent: true, timestamp: Date.now() };}, { concurrency: 5, // Process 5 jobs in parallel});Handle Events
worker.on('completed', (job, result) => { console.log(`Job ${job.id} completed:`, result);});
worker.on('failed', (job, error) => { console.error(`Job ${job.id} failed:`, error.message);});
worker.on('progress', (job, progress) => { console.log(`Job ${job.id} progress: ${progress}%`);});
worker.on('active', (job) => { console.log(`Job ${job.id} started`);});Full Example
import { Queue, Worker, shutdownManager } from 'bunqueue/client';
interface EmailJob { to: string; subject: string;}
// Producerconst queue = new Queue<EmailJob>('emails');
// Add some jobsawait queue.add('welcome', { to: 'new@user.com', subject: 'Welcome!' });await queue.add('newsletter', { to: 'sub@user.com', subject: 'News' });
// Consumerconst worker = new Worker<EmailJob>('emails', async (job) => { console.log(`Sending ${job.data.subject} to ${job.data.to}`); await job.updateProgress(100); return { sent: true };}, { concurrency: 3 });
worker.on('completed', (job) => { console.log(`✓ ${job.id}`);});
// Graceful shutdownprocess.on('SIGINT', async () => { await worker.close(); shutdownManager(); process.exit(0);});Next Steps
- Queue API - Full queue operations
- Worker API - Worker configuration
- Stall Detection - Handle unresponsive jobs
- DLQ - Dead letter queue management