RabbitMQ: events are being consumed before application is ready on Nest 8
See original GitHub issueHey all, I’m running into an issue where RabbitMQ consumers start up and consume events before the main Nest application is completely initialized. This started happening after we migrated from Nest 7 to Nest 8. I’ve resolved the issue in my application by moving the RabbitMQ module to the very end of the application module imports list:
// consumers start before application initialized
@Module({
imports: [
RabbitMQModule,
...ModulesThatDependOnRabbitMQ
]
})
// consumers start after application initialized
@Module({
imports: [
...ModulesThatDependOnRabbitMQ,
RabbitMQModule
]
})
Package versions:
@nestjs/core@8.2.6
@nestjs/common@8.2.6
@golevelup/nestjs-rabbitmq@1.18.1
Is anybody else seeing this behaviour?
Issue Analytics
- State:
- Created 2 years ago
- Comments:5 (1 by maintainers)
Top Results From Across the Web
Consumer Acknowledgements and Publisher Confirms
When a consumer (subscription) is registered, messages will be delivered (pushed) by RabbitMQ using the basic.deliver method. The method carries a delivery ...
Read more >RabbitMQ - Microservices - A progressive Node.js framework
Nest is a framework for building efficient, scalable Node.js server-side applications. It uses progressive JavaScript, is built with TypeScript and combines ...
Read more >RabbitMQ constant memory increase (binary_alloc) in idle state
I have detected a strange behavior on all of our RabbitMQ instances in different environments, where memory usage seem to increase with ~...
Read more >Handling of rabbit messages via NESTJs microservice issue
I'm running an NestJS microservice against a RabbitMQ broker to get messages that arrive from IOT devices and insert them into a MySQL...
Read more >Nest.js has trouble connecting to RabbitMQ with latest amqp ...
rabbitmq.reply-to' before setting up a consumer listening on that queue (as per this). This sounds like a bug outside of amqp-connection ...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found

I think it should probably be fine for us to move all the logic from
onModuleInittoonApplicationBootstraplike you suggested.@ckfngod I don’t think the order of the imports actually matters in your case, its more likely that its just a lucky thing with the timing that you happen to see a difference when you reorder the array
@underfisk @WonderPanda Thanks for the responses. Yeah it looks like it’s definitely lucky timing, wasn’t able to get it to work in my sample app. I’m seeing issues specifically with this in combination with a GRPC microservice. Rabbit consumers will begin processing events before the GRPC service is ready so as the application starts up it will throw a bunch of errors but afterwards it works fine.
Sample app is here: https://github.com/ckfngod/rabbitmq-app-init-issue
Start up the app to assert the queue, stop the app, publish a couple events to the queue then start the app again and you should see the issue.