How to get returned data when work with NestJS Bull queue?

3.4k views Asked by At

Currently, I am working on a NestJS project with the bull queue. In my controller, I have a get function to receive the request from the front end. Based on the request, I will send a gRPC call to retrieve data from other microservice. I would like to let the gRPC call function work with the bull queue. So, in the get function, I put the gRPC call function into producer, which can be executed in the consumer. However, after the gRPC call function is executed in the consumer, I can not find a way to return the retrieved data to the previous get function so that I can send the data back to the front end.

Any help would be appreciated.

2

There are 2 answers

2
LIIT On

You can, actually !

Here is an example :

import { Process, Processor } from '@nestjs/bull';
import { Job } from 'bull';

@Processor('myProcessor')
export class MyProcessor {
  @Process('myProcess')
  async handleMyProcess(job: Job<{ myInput: string }>) {
    await new Promise((resolve) => setTimeout(resolve, 5000));
    return 'hello world !';
  }
}

Then in your service :


    const compressJob = await this.myQueue.add('myProcess', {
      myInput: 'foo',
    });

    const test = await compressJob.finished();
    console.log(compressJob, test);
0
Falyoun On

You won't be able, the main purpose of using queues is non-blockage of any incoming request.

What you can do is, returning the job id of bull queue and then the front-end dev can track the response on it, or maybe use some event-driven approaches or websocket so you can tell him to refresh the resposne for it