Before we have finished with our producer that the producers of random animal names are each second
and the no, let's create the consumer that will consume those messages from animal storbeck.
And for this, let's create another file here in front of our project and let's name it consumer dogs.
And let's now go be a part of the code from producer Dodgiest File.
Let me go here.
Let me temporarily closed terminal and let me select the entire section like soul based in consumer
dogs.
And next, let me actually remove this entire produce message function like this.
Also, we don't need the chance here and we don't need the new instance of chance.
Let's remove it like so let's replace client ideal with my consumer, like this broker or that will
be unchanged here.
We still have three different brokers in the cluster.
And here let's create the new consumer that will be the result of the call to consumer method of the
Kafka Object.
And the topic will be still animals.
And here we will consume messages consuming here.
Let's first connect to consumer like this, and afterwards we need to subscribe to specific topic and
read messages from the topic.
Let's actually go to example for Kafka G.S. package.
This one, scroll down to the section with Sample Consumer.
Here it is, and we're able to use subscribe method like so, and afterwards we could use one method
of the consumer and, for example, print each message to the council.
Let's do that.
Let me copy this part like so go to the housecoat and pasted here and here.
We will subscribe to Typic.
Let's use again property shorthand because we have defined the topic here above and we don't need to
use from beginning fleg.
But if you want, you could leave it in place.
Let's use simply subscribe to topic and the here for each message we will bring to the console partition
idea of offset idea of every message and well you convert it to string.
Let's save this file.
Let's start first producer goal here.
Open up.
Time in and let's again run it using node producer dirigibles.
Let's start now with this produce and a random animal names, and now let's open up new terminal window
like so we are still located in our Kafka project.
Let's clear terminal and let's start consumer by end to note consumer dot Gere's.
It seems that there was an error.
Let me scroll up and verify what is that?
And it seems that we need to specify a consumer group idea.
Without that, it will not work.
Let me go to consumer and find a place where we should actually do that.
Maybe here, but probably it should be added somewhere here in the instance of the Kafka.
Let me try to edit here, group I.D. and here will be a consumer group like.
So let's save the file and let's try to rerun our application.
We still get the same error and it seems that the group idea should be specified in a call to consumer
method.
Here, let me copy it from here and afterwards.
Let's add new configuration object for consumer and based property group ID that will be equal to consumer
group.
Let's save the file and let's try to run our consumer, not consumer logs.
And now it works and we are receiving your messages every second and notice that we are getting actually
messages not every second, but instead we are getting them in batches after a specific amount of time.
And we have discussed that in a previous section in details.
And here in the console we see partition ID request that we have created the topic animals with file
partitions.
Also we see of said that is unique for every partition and we see values that are actually random names
for different animals.
And that means that our consumer works as well as.
And now there is a producer that produces messages to our coffeecake.
Last topic, animals.
And also there is a consumer that consumers massages regularly.
That's all for this lesson.
And that's all actually for this section where we have reviewed how you are able to create the coproducer
and Kafka consumer.
You know, I guess I'll see you in the next section where we will try to create consumers and producers
using Python.
Have a break and they will see you next.
Bye bye.