A simple Node.js RabbitMQ demo with a publisher and consumer using amqplib.
This project demonstrates basic message queue communication:
publisher.jssends a job message to thejobsqueue.consumer.jslistens to the same queue and reads incoming messages.
- Node.js
- RabbitMQ
- amqplib
RabbitMQ1/
publisher.js
consumer.js
package.json
README.md
- Node.js 16+
- RabbitMQ server running on
amqp://localhost:5672
npm installnpm run publish-> runspublisher.jsnpm run consume-> runsconsumer.js
Make sure RabbitMQ is running locally.
npm run consumeYou should see:
Waiting for messages in jobs queue...
The publisher expects a number argument.
node publisher.js 7Expected publisher log:
Job sent successfully! 7
Expected consumer log:
Job received successfully! 7
Publisher sends JSON in this format:
{
"number": "7"
}- Queue name is fixed as
jobs. - Both publisher and consumer connect to
amqp://localhost:5672. - Consumer currently acknowledges (
ack) only whennumber == 3. - Consumer closes after 500ms, so it behaves like a short-lived demo process rather than a long-running worker.
-
ECONNREFUSED:- RabbitMQ is not running or not accessible on port
5672.
- RabbitMQ is not running or not accessible on port
-
No consumer output:
- Start consumer before publishing.
- Increase/remove the 500ms auto-close in
consumer.jsfor continuous listening.
Suprit Raj