A real-time chat application built with Spring Boot, WebSockets (STOMP), and Redis Pub/Sub to enable stateless, horizontally scalable, multi-instance real-time communication.
This project demonstrates:
- Live chat using WebSockets
- Redis-based message broadcasting
- Running multiple Spring Boot instances
- Stateless architecture suitable for clustering
Uses Spring WebSocket + STOMP to communicate instantly between clients and server.
Redis distributes messages between multiple application instances:
- Instance A receives a WebSocket message
- Publishes to Redis
- Redis broadcasts to all instances
- Every instance pushes message to its connected WebSocket clients
The frontend includes:
- Username prompt
- Scrollable chat area
- Live updates
- Automatic message rendering
Run 2+ application instances on different ports (8080, 8081, β¦) and they all stay in sync.
| Layer | Technology |
|---|---|
| Backend | Spring Boot |
| Realtime Messaging | WebSocket + STOMP |
| Distributed Messaging | Redis Pub/Sub |
| Serialization | Jackson JSON |
| Frontend | HTML, JS, SockJS, STOMP.js |
| Build Tool | Maven / Gradle |
src/main/java/com/shumisoft/chatroom/
β
βββ config/
β βββ WebSocketConfig.java
β βββ RedisConfig.java
β
βββ controller/
β βββ ChatController.java
β
βββ model/ or dto/
β βββ ChatMessageDTO.java
β
βββ redis/
β βββ RedisMessagePublisher.java
β βββ RedisMessageSubscriber.java
β
βββ ChatApplication.java
Frontend:
src/main/resources/static/
βββ index.html
The browser initializes a STOMP connection:
/ws-chat
STOMP sends messages to:
/app/sendMessage
redisTemplate.convertAndSend("chatroom", message);Using:
/topic/public
Result: All connected clients see the same messages instantly, no matter which backend instance they connect to.
docker run -p 6379:6379 redisInstall Redis for your OS.
mvn spring-boot:run(or Gradle)
mvn spring-boot:run -Dspring-boot.run.arguments="--server.port=8081"./gradlew bootRun --args='--server.port=8081'java -jar target/chatroom.jar --server.port=8081
-
Open:
-
Enter different usernames
-
Send messages
You will see:
- When you send a message from port 8080, it appears instantly on the page running on port 8081
- And vice-versa
This confirms Redis Pub/Sub is working.
Enables STOMP and WebSocket endpoints.
Configures:
- JSON serialization
- Subscriber
- RedisMessageListenerContainer
Publishes chat messages to Redis.
Listens for Redis events and forwards to WebSocket clients.
- Stateless β no user sessions stored in memory
- Scalable β run unlimited app instances
- Resilient β if one instance goes down, chat stays alive
- Standards-based β no vendor lock-in
This is the same pattern behind apps like Slack, Discord, and large-scale notification systems.
- Redis Streams for message history
- Presence/online-user tracking
- Authentication + JWT WebSocket handshakes
- Docker Compose for full local cluster
- Kubernetes deployment
- Load balancing (NGINX / Spring Cloud Gateway)
Feel free to submit issues or PRs! This project is great for anyone learning distributed real-time systems.
Give the repo a star and feel free to use it in interviews or portfolios!