Spring for Websocket Why WebSockets? I performed these operations using WebSockets which provide full-duplex bidirectional communication, which means that information can flow from the client to the server and also in the opposite direction simultaneously.
It is widely used in chat applications. First lets create a Message Modal which would hold the message content.
We also declared a KafkaTemplate bean to perform high-level operations on your producer. In other words, the template can do operations such as sending a message to a topic and efficiently hides under-the-hood details from you.
The next step is to create an endpoint to send the messages to the Kafka topic. Create the following controller class for that.
Let's test everything we build until now. Run the main method inside KafakaJavaApp. To run from the command line, execute the following command mvn spring-boot:run Enter fullscreen mode Exit fullscreen mode Your server should be running on port and you can make API requests against it!
You can use postman to do a POST request as shown below. But how do you know the command successfully sent a message to the topic?
Fortunately, there is an easy way to create a consumer to test right away. Inside the bin folder of your Kafka directory, boof the following command:.
For that, we would need to build a Consumer or Chah in Java. Develop a Consumer to listen to Kafka Topic. Similar to ProducerConfig. In this project, we are using the value "earliest" so that we will get all the values in the queue from the beginning.
hoot Instead, we can also use "latest" to get only the latest value. Next, we need to configure the Websocket to send the Message to the client system. Next, let's move on to developing the UI part. FrontEnd Development in ReactJS We would create a simple chat with a list of messages and a text field at the bottom of the to send the messages to Kafka backend.