Run a containerized Kafka server and interact with it using Node.js
- Open
docker-compose.yml
and make sureKAFKA_ADVERTISED_HOST_NAME
is the same asdocker-compose ip
. - Open
package.json
and make sure the IP in theKAFKA_URL
is the same asdocker-compose ip
. - Make sure
KAFKA_CREATE_TOPICS
indocker-compose.yml
contains the same topic name asKAFKA_TOPIC
in thepackage.json
.
- run
npm install
- run
docker-compose up
- open a new terminal tab and run
npm start consumer
- open a new terminal tab and run
npm start producer
You can now see the producer tab making new messages and the consumer tab reading those messages. You can start more producers (while still running 1 consumer) and see more messages coming in.
If you are getting an error about the topic not existing, try running docker-compose rm
to delete the containers. Then re-run docker-compose up
again.