How to implements a fast and robust queue system in Nodejs

Satish Mali
Oct 8, 2022

--

Queue system using bullmq library.

Install using npm:

npm install bullmq

Create file called job.ts add following code.

import { Injectable } from ‘@nestjs/common’;
import { Queue } from ‘bullmq’;

@Injectable()
export class BullmqService {

public async addJob() {
const queue = new Queue(‘my-queue’);
queue.add(‘cars1’, { color: ‘blue’ });
queue.add(‘cars2’, { color: ‘red’ });
queue.add(‘cars3’, { color: ‘yello’ });
queue.add(‘cars4’, { color: ‘pink’ });
}
}

Process the job from queue using worker

import { Worker } from ‘bullmq’;

const worker = new Worker(‘my-queue’, async job => {
if (job.name) {
console.log(‘==========>’, job.data);
}
});

To see full demo of bullmq with nestjs on github: https://github.com/satishkumarmali/NestJs-BullMQ-Demo

Sign up to discover human stories that deepen your understanding of the world.

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

--

--

Satish Mali
Satish Mali

No responses yet

Write a response