Quick demo on how to use NATS Streams with NodeJS
- Docker
- NodeJS 16+
docker-compose up
and wait for Nats to startcd setup && ./build_and_run.sh
to create the stream (in a real world scenario this is done by the application)npm i -g pm2
(if you want to stress test)
- start multiple producers to fill the stream
pm2 start producer/producer.mjs && pm2 scale producer 10
(each producer publishes 20000 messages with delivery ack)- this will start 10 producers and keep running forever
- you can stop the producers with
pm2 stop producer
- start a worker (or multiple on different terminals)
node worker/worker.mjs
- you can also start workers with pm2
pm2 start workers/worker.mjs && pm2 scale worker 5 && pm2 logs
You can also look at debug output by running NATS toolbox container
docker run --network docker-compose-nats_nats --rm -it natsio/nats-box:latest
- then
nats sub debug --server nats-1
- refactor the NodeJS code to be a bit less hacky
- provide a more complex stream and consumer structure
- dockerize everything