RelayChat is a real-time messaging system, it can handle thousands of concurrent WebSocket connections with low latency and high availability.
- FastAPI Backend: Handles HTTP REST APIs and WebSocket connections
- Redis Pub/Sub: Cross-pod message broadcasting for horizontal scaling
- MongoDB: Persistent message storage
- WebSocket Manager: Manages active connections per pod
[Sender] -> (WebSocket) -> [Backend Pod] -> [MongoDB] (Persist)
|
v
[Redis Pub/Sub]
|
(Broadcast to all instances)
|
v
[All Backend Pods]
|
v
(WebSocket)
|
v
[Recipient Clients]
- JWT-based authentication
- Real-time messaging via WebSockets
- Private and group chat rooms
- Message persistence (MongoDB)
- Horizontal scalability (Redis Pub/Sub)
- Connection management
- Message history
- Basic React UI
Backend
- FastAPI (Python)
- WebSockets
- Redis (Pub/Sub)
- MongoDB (async with Motor)
- JWT authentication
Frontend
- React 19
- Tailwind CSS
- Shadcn UI components
- Native WebSocket API
Infrastructure
- Docker Compose (local testing)
- Kubernetes-ready architecture
- Docker & Docker Compose
- Python 3.11+
- Node.js 18+
- Redis
- MongoDB
- Start Redis & MongoDB:
# Start Redis (required for WebSocket pub/sub)
./scripts/start-redis.sh
# Or use Docker Compose
docker-compose up redis mongodb -d- Backend:
cd backend
pip install -r requirements.txt
uvicorn server:app --reload --port 8001- Frontend:
cd frontend
yarn install
yarn startImportant: Redis must be running before starting the backend. If you see "Authentication failed" errors, check that Redis is running with redis-cli ping.
# Start all services
docker-compose up -d
# View logs
docker-compose logs -f backend
# Stop all services
docker-compose downPOST /api/auth/register- Create accountPOST /api/auth/login- LoginGET /api/auth/me- Get current user
POST /api/rooms- Create roomGET /api/rooms- List user's roomsGET /api/rooms/{room_id}/messages- Get room message history
WS /api/ws?token={jwt}- WebSocket connection
{"action": "join_room", "room_id": "xxx"}
{"action": "send_message", "room_id": "xxx", "content": "Hello!"}- Stateless Backend: No shared memory between pods
- Redis Pub/Sub: Synchronizes messages across all instances
- Kubernetes Ready: Deploy multiple replicas
- Load Balancer: WebSocket-aware routing (sticky sessions not required)
- 5K+ concurrent WebSocket connections per pod
- <100ms message delivery latency
- Horizontal scaling via pod replication
apiVersion: apps/v1
kind: Deployment
metadata:
name: relaychat-backend
spec:
replicas: 3
selector:
matchLabels:
app: relaychat
template:
spec:
containers:
- name: backend
image: relaychat:latest
ports:
- containerPort: 8001
env:
- name: REDIS_URL
value: "redis://redis-service:6379"
- name: MONGO_URL
value: "mongodb://mongo-service:27017"cd backend
pytest# Install artillery
npm install -g artillery
# Run WebSocket load test
arsenal run tests/load-test.ymlMONGO_URL=mongodb://localhost:27017
DB_NAME=test_database
REDIS_URL=redis://localhost:6379
JWT_SECRET_KEY=your-secret-key-change-in-production
CORS_ORIGINS=*
REACT_APP_BACKEND_URL=http://localhost:8001
- JWT token-based authentication
- WebSocket connections validated with JWT
- Password hashing with bcrypt
- Room access control (membership verification)
- CORS configuration
# Backend logs
tail -f /var/log/supervisor/backend.*.log
# Docker logs
docker-compose logs -f backend- Active WebSocket connections
- Redis Pub/Sub latency
- MongoDB write performance
- Message delivery time
- Pod CPU/Memory usage