NOTE: We use term messages instead of events
This is a good guide: https://www.rabbitmq.com/getstarted.html
- Producer sends messages
- Consumer receives and processes messages
# start consumer
node 1-messaging/index.js c hello_consumer hello_queue
# start producer
node 1-messaging/index.js p hello_producer hello_queue
We can add more consumers to have more processing power
- Producer sends messages
- Message bus distributes messages evenly to Consumers (usually distributed with round robin)
# start consumer multiple consumers
node 1-messaging/index.js c hello_consumer_first shared_hello_queue
node 1-messaging/index.js c hello_consumer_second shared_hello_queue
# start producer
node 1-messaging/index.js p hello_producer shared_hello_queue
- Producer sends message (event)
- Instead of going to predifined queue it is sent to an exchange
- Messge bus routes events from the topic to queues that are attached to it
- Consumers will receive every message from their queue
# start consumer multiple consumers
node 2-worker/index.js c hello_consumer_first hello_exchange
node 2-worker/index.js c hello_consumer_second hello_exchange
# start producer
node 2-worker/index.js p hello_producer hello_exchange
- Producer sends message (event)
- Exchange uses a routing key to send message to correct queue
- Consumers will receive every message from their queue
# start consumer multiple consumers
node 3-distributed-queue/index.js c consumer_us distributed_exchange US
node 3-distributed-queue/index.js c consumer_de distributed_exchange DE
# start producer
node 3-distributed-queue/index.js p hello_producer distributed_exchange
- Set up of exchanges more complicated
Remote Procedure Call / Replies to sender
Replies require an own queue. Producer is also Consumer and Consumer is a Producer...
- Client uses a producer to send a message
- Server receives a message with consumer
- Server sends a message with producer
- Client receives a message with consumer
# start server
node 4-rpc/index.js s
# start client
node 4-rpc/index.js c
$ docker run -d --name example-rabbit -p 5672:5672 -p 15672:15672 rabbitmq:3-management
Port 5672
is for rabbitmq client communication
Port 15672
is for Management UI
Go to http://localhost:15672/ to open Management UI (
username: guest
password: guest
AMQP 0-9-1 library and client for Node.JS
example_github_async.js
and example_github_promise.js
are async versions of https://github.com/squaremo/amqp.node#promise-api-example