# Build a Chat Room Application with Redpanda Cloud and Java

> For the complete documentation index, see [llms.txt](https://docs.redpanda.com/llms.txt). Component-specific: [labs-full.txt](https://docs.redpanda.com/labs-full.txt)

---
title: Build a Chat Room Application with Redpanda Cloud and Java
latest-operator-version: v26.1.4
latest-console-tag: v3.7.3
latest-connect-version: 4.93.0
latest-redpanda-tag: v26.1.9
docname: cloud-java
page-component-name: labs
page-version: master
page-component-version: master
page-component-title: Labs
page-relative-src-path: cloud-java.adoc
page-edit-url: https://github.com/redpanda-data/redpanda-labs/edit/main/docs/modules/clients/pages/cloud-java.adoc
description: Create a basic chat room application with Redpanda Cloud and Kafka Java clients.
page-git-created-date: "2025-05-06"
page-git-modified-date: "2025-05-06"
---

<!-- Source: https://docs.redpanda.com/labs/clients/cloud-java.md -->

Create a basic chat room application with Redpanda Cloud and [Kafka Java clients](https://central.sonatype.com/artifact/org.apache.kafka/kafka-clients).

This example shows you how to:

-   Write a client application in Java to produce and consume chat room messages.

-   Build and run multiple clients to exchange chat messages streamed through Redpanda Cloud.


![Demo of the application](https://docs.redpanda.com/labs/clients/_images/chat-room.gif)

## [](#what-is-a-chat-room-application)What is a chat room application?

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.

## [](#why-use-redpanda)Why use Redpanda?

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.


## [](#prerequisites)Prerequisites

-   Complete the [Redpanda Cloud Quickstart](https://docs.redpanda.com/current/get-started/quick-start-cloud/) before continuing. This example expands on the quickstart.

-   Install the following:

    -   Java 11 or 17 (OpenJDK is recommended)

    -   Maven


    ### Windows/Linux

    You can download OpenJDK from [Adoptium](https://adoptium.net/temurin/releases), and can follow the installation instructions for Maven on the [official Maven website](https://maven.apache.org/install.html).


    ### macOS

    Mac users with Homebrew installed can run the following commands to install these dependencies:

    ```bash
    brew install openjdk@11 maven
    ```

    Make sure to follow any symlinking instructions in the Caveats output.


When the prerequisites are installed, the following commands should print the version of both Java and Maven:

```bash
java --version
mvn --version
```

> 📝 **NOTE**
>
> Redpanda Cloud uses TLS certificates signed by [Let’s Encrypt](https://letsencrypt.org/). Most programming languages will load their root certificate authority (CA), `ISRG Root X1`, by default so you shouldn’t need to provide a custom CA certificate.

## [](#run-the-lab)Run the lab

Compile the client chat application, run it from multiple client terminals, and chat between the clients.

1.  Clone this repository:

    ```bash
    git clone https://github.com/redpanda-data/redpanda-labs.git
    ```

2.  Change into the example directory:

    ```bash
    cd clients/chat-room/cloud/java
    ```

3.  Open the Java files and replace the placeholders wrapped in angle brackets (`<>`) with the same values that you used in the Redpanda Cloud Quickstart.

4.  Install the dependencies by building the project:

    ```bash
    mvn package
    ```

    The output is verbose, but you should see a successful build message:

    \[INFO\] BUILD SUCCESS

5.  Verify that the `chat-room` topic exists in your cluster by listing all topics:

    ```bash
    rpk topic list --tls-enabled
    ```

    Output:

    NAME       PARTITIONS  REPLICAS
    chat-room  1           1

6.  If the topic doesn’t exist yet, use [`rpk`](https://docs.redpanda.com/current/get-started/rpk/) to create a `chat-room` topic:

    ```bash
    rpk topic create chat-room --tls-enabled
    ```

    Output:

    TOPIC      STATUS
    chat-room  OK

7.  From `chat-room/cloud/java`, compile the client application:

    ```bash
    mvn compile
    ```

8.  Open at least two terminals, and for each terminal:

    1.  Run the client application:

        ```bash
        mvn exec:java -Dexec.mainClass="com.example.Main"
        ```

    2.  When prompted with `Enter user name:`, enter a unique name for the chat room.


9.  Use the chat application: enter a message in a terminal, and verify that the message is received in the other terminals.

    For example:

    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


## [](#files-in-the-example)Files in the example

This example includes the following files:

-   [`src/main/java/com/example/Admin.java`](https://github.com/redpanda-data/redpanda-labs/blob/main/clients/chat-room/cloud/java/src/main/java/com/example/Admin.java): Checks whether the `chat-room` topic exists and creates it if not.

-   [`src/main/java/com/example/ChatProducer.java`](https://github.com/redpanda-data/redpanda-labs/blob/main/clients/chat-room/cloud/java/src/main/java/com/example/ChatProducer.java): A producer that sends strings entered by the user of the terminal to the `chat-room` topic. Messages are sent as JSON encoded strings.

-   [`src/main/java/com/example/ChatConsumer.java`](https://github.com/redpanda-data/redpanda-labs/blob/main/clients/chat-room/cloud/java/src/main/java/com/example/ChatConsumer.java): A consumer that reads all messages from the `chat-room` topic 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.

-   [`src/main/java/com/example/Main.java`](https://github.com/redpanda-data/redpanda-labs/blob/main/clients/chat-room/cloud/java/src/main/java/com/example/Main.java): The client application that creates the topic, producer, and consumer and implements the chat logic.


## [](#next-steps)Next steps

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.


## [](#suggested-reading)Suggested reading

For additional resources to help you build stream processing applications that can aggregate, join, and filter your data streams, see:

-   [Redpanda University](https://university.redpanda.com/)

-   [Redpanda Blog](https://redpanda.com/blog)

-   [Resources](https://redpanda.com/resources)