1. Production Message:
First, the message data (ChatMessage) will be sent to the Kafka topic (public-chats) using KafkaProducerServices.
The sendMessage method in KafkaProducerServices will send the message to Kafka.
2. Message Consume:
Once a message is sent to Kafka, KafkaConsumerServices will listen to the same topic (public-chats) and consume the incoming message.
Received messages are logged, added to the in-memory message list (chatMessages), and sent to WebSocket clients subscribed to /topic/public.
3. API to Retrieve Messages:
The /api/chat API endpoint will call the getChatMessages method of KafkaConsumerServices to retrieve the list of messages stored in memory.
These messages will then be returned as API responses.
With this explanation, you have an idea of how messages are sent and received using Kafka, how messages are stored in temporary memory, and why this approach is chosen for simple projects. In production, it is common to use databases to store messages to ensure data persists and can be managed properly.