How to use Bull with NestJS

Ravi Yasas
4 min readAug 25, 2021

--

This is the third part of File uploading example using NestJS and Angular 11 — Part 2 In this article, I will use Bull for uploading files.

What is Bull?

  • Bull is a high-performance queue based on Redis for Node-based applications.
  • Queues are very useful when we are developing microservices.
  • It will make smooth communication between microservices communication.
  • It is very useful in a situation like a network failure.
  • You may read the official documentation for more details.

Use Bull with NestJS

  • NestJS provides @nestjs/bull package as a wrapper on top of the Bull.
  • You need to install the Redis server before you get into the Bull. Because Bull is based on Redis.

Install Redis

  • Installing Redis may differ according to your operating system.
  • I am not going to explain this here.
  • You can install a Redis server locally and once you start the server, it may look like this

Now your Redis server is up and running. If you can install a Redis client, then it will be very easy to see statuses and the queue processors. Now let’s install the bull package for NestJS

Install Bull on Nest

  • Go to the automobile-backend application
  • Hit the following commands to install the Bull wrapper for NestJS

npm install — save @nestjs/bull bull

npm install — save-dev @types/bull

  • It will look like this

How to use Bull?

I am going to handle the file uploading process using a queue. Once the backend server receives a file, this file will be put in a queue. Then the queue will be processed one by one. Bull provides many functions to handle these kinds of problems. Let’s configure our applications one by one. Here is the flow.

  • Import Bull module to the root
  • Register a queue
  • Add files into the queue
  • Create a service to process queue

Import Bull module to the root

  • Add the bull module to the automobile.module.ts file as follows.
  • Now you can see, the BullModule.forRoot. Here we can set the configuration details of the locally running Redis server that we have already created.
  • My Redis server is running on port 6379.
  • Then I registered a queue called “upload-queue” to handle uploaded files.
  • This registered queue will be used to add data into the queue and in the processor as well.

Now we need some refactoring of our backend service. Just remember I am still working on the existing project that I completed in the last article. Now I am going to refactor automobile.controller.ts and add a new processor for file processing.

Refactoring controller to serve queue operations

I am not going to use the existing automobile.service .ts file. Instead, I create a new file for CSV file operations. Check this controller.

  • As you can see I removed the injection for the automobile.service.ts and added a new injection for the queue in the constructor.
  • The queue name is “upload-queue” as I registered in the automobile.module.ts file.
  • The received file will be added to the queue.

Adding a Processor

The processor will handle the operations of the data added to the queue. A queue can have one Processor and many processes. @Processor and @Process decorators can be used to indicate the processor and processes.

  • Create a new directory inside the vehicle module.
  • Create a new file (upload.processor.ts) in the created directory.
  • The data operations can be done in the relevant process.
  • The registered queue name will be used as the Processor name (upload-queue)
  • The process name is the name that we used in the controller when adding data into the queue.
  • The business logic goes inside the Process.

If you look carefully, you will be able to see I am not returning anything. All functions are void. Previously I return the list of data. It is not suitable when you are dealing with queues. To handle this we can use a socket service (socket.io, socket cluster) for notifications. I am not going to do this in this article. I will do this in a future article.

I make one change in the client application. I refactor the submit() method in the upload.component.ts file.

Now everything will be right. You can run both server and client and then check how it is working. You can find the code from below Git repository.

Read the original article: Use Bull with NestJS

--

--

Ravi Yasas

Full Stack Developer, talks about Java, Spring Boot, NestJS, Spring Security, Angular, React, GraphQL & Microservices. Author of https://www.javastackflow.com