Build a Chat Room Application with Redpanda and Node.js
Create a basic chat room application with Redpanda and Kafka clients developed with kafkajs.
This example shows you how to:
Write a client application in TypeScript to produce and consume chat room messages.
Build and run multiple clients to exchange chat messages streamed through Redpanda.
A chat room application is software that enables users to engage in real-time textual communication with one another. These applications typically allow multiple users to join a chat room, where they can send messages and interact with others in a group conversation.
Chat room applications often include features such as private messaging, user profiles, and notifications. Some popular chat room applications include Slack, Discord, and WhatsApp.
Redpanda offers several features that make it ideal for building a fast, scalable, and robust chat room application.
Scalability: Redpanda can scale horizontally and vertically to accommodate growing chat room usage over time.
Low-latency: Redpanda is designed for minimal latency to provide a smooth user experience and fast message delivery.
Fault tolerance: Redpanda is resilient to failures, thanks to its built-in replication and partitioning capabilities. This built-in resilience ensures that the chat room application continues to serve users even if individual brokers in the cluster experience downtime.
Durability: Redpanda persists messages on disk, maintaining chat history and allowing users to read previous conversations.
Build the client chat application, run it from multiple client terminals, and chat between the clients.
Clone this repository:
git clone https://github.com/redpanda-data/redpanda-labs.git
Change into the example directory:
Install the required dependencies:
Verify that the
chat-roomtopic exists in your cluster by listing all topics:
docker exec -it redpanda-0 rpk topic list
NAME PARTITIONS REPLICAS chat-room 1 1
If the topic doesn’t exist yet, use
rpkto create a
docker exec -it redpanda-0 rpk topic create chat-room
TOPIC STATUS chat-room OK
Open at least two terminals, and for each terminal:
Run the client application:
When prompted with
Enter user name:, enter a unique name for the chat room.
Use the chat application: enter a message in a terminal, and verify that the message is received in the other terminals.
Enter user name: Alice Connected, press Ctrl+C to exit Alice: Hi, I'm Alice Bob: Hi Alice, I'm Bob, nice to meet you
This example includes the following files:
sr/admin.ts: Checks whether the
chat-roomtopic exists and creates it if not.
The broker settings in this code are from the Redpanda Quickstart, where the external port for broker
redpandais set to port 19092.
src/producer.ts: A producer that sends strings entered by the user of the terminal to the
chat-roomtopic. Messages are sent as JSON encoded strings.
src/consumer.ts: A consumer that reads all messages from the
chat-roomtopic and prints them to the console. You can start as many consumer groups as you like, but each group reads a message only once, which is why the example is using a generated UUID for the group ID. This way, each time you run the application, you see all previous messages.
eachMessage()function automatically commits on a heartbeat interval, there is no
commit()method or auto-commit configuration in the code.
src/index.ts: The client application that creates the topic, producer, and consumer and implements the chat logic.
This is a basic example of a chat room application. You can improve this application by implementing additional features and components, such as:
A user interface to make it more interactive and user-friendly.
A user registration and login system to authenticate users before they can access the chat room.
Rate limiting and other measures to prevent spamming and abuse in the chat room.