Kafka Nodejs Example with Producers and Consumers

Let’s take a look at a Kafka Nodejs example with Producers and Consumers. We will be creating a kafka producer and consumer in Nodejs. If you haven’t already, check out my previous tutorial on how to setup Kafka in docker. I already created a topic called cat that I will be using. Kafka Producer We are going to use the npm module called kafka-node to interface with our kafka and zookeeper.

Setup Kafka Zookeeper in Docker

Setting up Apache Kafka and Zookeeper in docker is easy thanks to popular DockerFiles like wurstmeister/kafka-docker. Let’s go through a quick setup of Kafka and Zookeeper in docker on a single node system. Prerequisites Linux System Git Docker Docker-compose Kafka and Zookeeper Start by cloning the repo: git clone https://github.com/wurstmeister/kafka-docker Then change to the kafka-docker directory: cd kafka-docker Now let’s edit the docker-compose.yml file. Change the Kafka Advertised Hostname to localhost: