'NestJS RabbitMQ Microservice'

5.1k views Asked by At

I'm trying to connect to a Rabbit MQ docker container using NestJS microservice. I'm running it using vagrant. This is the error I'm getting:

[1601171008162] ERROR (32761 on local): Disconnected from RMQ. Trying to reconnect.
context: "Server"
trace: ""
[1601171008163] ERROR (32761 on local):
context: "Server"
trace: ""
msg: {
  "err": {
    "cause": {},
    "isOperational": true
  }
}

This is the server config.

{
  transport: Transport.RMQ,
  options: {
    urls: ['amp://username:[email protected]:5672'],
    queue: 'test_queue',
    queueOptions: {
      durable: true
    },
    noAck: true,
    prefetchCount: 1
  }
}

I've been trying to figure this out for over a week. Any help would be appreciated.

3

There are 3 answers

0
Lucas Sampaio On

I had this error, it was solved by just setting in the microservice configuration the noAck: false, and prefetchCount: 1.

Here is an example of the configuration:

 const app = await NestFactory.createMicroservice(AppModule, {
    transport: Transport.RMQ,
    options: {
      urls: [process.env.CLOUDAMQP_URL],
      queue: 'queueName',
      queueOptions: {
        durable: false,
      },
      noAck: false,
      prefetchCount: 1,
    },
  });
  await app.listen();

The explanation is:

In the nestjs documentation, a basic configuration is presented, where noAck is not defined, and then the example of how to consume the queue message is presented, but in the example itself, the manual ack is done, however, as in the configuration it was not when noAck:false is set, the error mentioned above is generated, because at the time of doing the manual Ack, the message no longer existed.

1
Daedalus On

I ran into this issue, and the cause was an improperly formed virtual host within RabbitMQ. I was reading amqp://localhost:5672/example in code and configured my virtual host as /example instead of just example. That resolved the issue for me, hope this helps someone!

0
Fabio Caetano On

This worked form me amqp://username:[email protected]:5672