POC setting up a pub/sub pattern using rabbitMq and node
- docker
- docker-compose
- node (>8)
- (yarn) (
npm install -g yarn
)
- start docker (if you're on windows/osx)
- run
docker-compose up
(anytime usedocker-compose down
if Ctrl+C doens't kill everything)
the broker address will be
amqp://{MQ_USER}:{MQ_PASS}@{MQ_HOST}:{MQ_PORT}
by default : (you can change it in docker-compose.yml
, also used in js through src/config.js
)
MQ_USER
= 'test'MQ_PASS
= '1234'MQ_HOST
= '127.0.0.1' (or192.168.99.100
on windows if notWindows 10 Pro
)MQ_PORT
: '5672'
you can open the rabbitmq manager tool in a browser : http://192.168.99.100:8080 (or http://localhost:8080) use the credential above ^
- go in
src/
(cd src
) - run
yarn
(ornpm i
)
two yarn aliases are defined to run publisher and subscriber scripts
-
yarn sub
will start a new subscriber, with a new temp queue (deleted when disconnected) -
yarn sub {id}
will start a new subscriber, with a named queue (from the id) => this way the queue is dirable, and you can connect multiple subs to the same queue. -
yarn pub
will start a new publisher that will publish some messages for all subscribers
you can run
1
publisher andn
subscribers
- use
topic
exchange type, (like mqtt). Subscribers can listen to everything or a part of a topic.