A scalable, real-time chat backend built with Node.js, designed for high-traffic and distributed environments.
It combines:
- WebSockets for instant delivery
- Redis for fast caching
- Apache Kafka (3-broker cluster) for distributed streaming
- Aiven Cloud kafka Support
- MongoDB for long-term persistence
- Gemini API for AI-powered responses
The system follows a Real-Time First + Asynchronous Persistence architecture and is production-ready for distributed deployments.
The AI Chat Application is architected around:
- Instant message delivery via Socket.IO
- Redis caching (cache-aside strategy)
- Kafka-based asynchronous persistence
- Bulk MongoDB writes with idempotency
- Offset commits only after successful DB persistence
- Self-healing retry & recovery mechanisms
Designed for:
- Zero message loss
- Horizontal scalability
- High-throughput environments
- Instant user-to-user delivery
- Optimistic UI support
- JWT-secured WebSocket authentication
/aiprefixed messages trigger Gemini API- AI responses are delivered as standard chat messages
- 3-broker Kafka cluster
- Partition-based scaling
- Idempotent message handling via
messageId - Offset commit after MongoDB bulk write
- Recent conversation storage with TTL
- Cache-aside strategy
- Automatic read-repair (self-healing cache)
- Kafka failure → Redis backup queue
- Background retry worker
- Worker pause/resume memory protection
- MongoDB failure → buffer preservation
- Node.js
- Express
- Socket.IO
- Apache Kafka
- Redis
- MongoDB
- Gemini API
- React (Vite)
- Tailwind CSS
- Socket.IO Client
- Docker
- Docker Compose
- 3-node Kafka Cluster
- Kafka UI (Docker-only monitoring)
This project includes detailed documentation to help you understand the API and the chat persistence architecture.
-
🔗 Chat Persistence Flow – Understand how chat interactions are stored, retrieved, and persisted across sessions. 👉 docs/CHAT_PERSISTANCE_FLOW.md
-
🔗 API Documentation – Complete reference for all API endpoints, request/response schemas, authentication, and usage examples. 👉 docs/API_DOCUMENTATION.md
Use the following Docker images from Docker Hub:
shubha69/ai_chatapp-frontend:latest
shubha69/ai_chatapp-backend:latest
Docker Hub links:
- Backend: https://hub.docker.com/r/shubha69/ai_chatapp-backend
- Frontend: https://hub.docker.com/r/shubha69/ai_chatapp-frontend
If you are running the app locally, you need the infrastructure running in Docker:
docker-compose up -d zookeeper1 zookeeper2 zookeeper3 kafka kafka2 kafka3 kafka-ui
# Ensure you also have MongoDB and Redis running locally or in Docker or Cloud Servicecd backend
npm install
npm run devcd frontend
npm install
npm run dev- Supports cloud-based Aiven Kafka only
- Designed for cloud deployments
- ❌ Does NOT support local Docker Kafka setup
- Supports local Docker Kafka setup
- Can be used for local development environments
- Also compatible with custom Kafka configurations
If you want to modify the Kafka configuration:
- Update the Kafka setup code
- Build your own backend Docker image
- Push it to your Docker Hub repository
- Use your custom version tag
Your provided Docker Compose setup includes:
- 3 Zookeeper nodes
- 3 Kafka brokers
- Kafka UI (for monitoring only in Docker mode)
Start Kafka cluster:
docker-compose up -d zookeeper1 zookeeper2 zookeeper3 kafka kafka2 kafka3 kafka-uiKafka will be available on:
localhost:9092localhost:9093localhost:9094
Kafka UI (Docker-only):
http://localhost:8080
When using Docker Kafka:
- Kafka UI → topic & partition monitoring
- Offset commit tracking
- Replication visibility
- Broker health checks
In cloud mode, use your provider’s monitoring tools instead.
- Multiple workers across Kafka partitions
- Kafka auto-rebalancing
- Backend replicas behind load balancer
- Bulk MongoDB writes
- Composite DB indexing
We welcome contributions to improve scalability, reliability, and features.
-
Fork the repository
-
Create a feature branch
git checkout -b feature/your-feature-name -
Make your changes following project architecture guidelines
-
Ensure linting and formatting are clean
-
Commit with a clear message
git commit -m "feat: add your feature" -
Push to your fork
git push origin feature/your-feature-name -
Open a Pull Request
- End-to-End Encryption (E2EE)
- Media & file sharing
- Message reactions
This system is designed with distributed systems best practices in mind:
- Event-driven architecture
- Strong durability guarantees
- Fault-tolerant recovery
- Horizontal scalability
Whether running locally with Docker Kafka or using a managed Kafka provider, the architecture remains consistent and production-ready.
If you’re building scalable real-time systems — this project provides a strong, extensible foundation.
