A distributed, real-time chat platform built with modern microservices architecture.
User โ Socket.IO โ Express Server
โ
โโโโโโโโโโโดโโโโโโโโโโ
โ โ
Redis Pub/Sub Kafka Queue
โ โ
Broadcast Consumer Batch
โ
MongoDB
# Clone the repository
git clone <your-repo>
cd chatflux
# Start all services
docker-compose up -d
# View logs
docker-compose logs -f chatflux-app
Install dependencies
npm install
Configure environment
cp .env.example .env
# Edit .env with your configuration
Start infrastructure (MongoDB, Redis, Kafka)
# Using Docker for infrastructure only
docker-compose up -d mongodb redis kafka
Run the application ```bash
npm start
npm run consumer
## ๐ Project Structure
chatflux/ โโโ app/ โ โโโ config/ # Configuration management โ โโโ db/ # Database models & operations โ โโโ jobs/ # Cron jobs โ โโโ kafka/ # Kafka producer/consumer โ โโโ redis/ # Redis client & operations โ โโโ routes/ # API routes โ โโโ socket/ # Socket.IO server โ โโโ utils/ # Logger & metrics โโโ logs/ # Application logs โโโ index.js # Main entry point โโโ kafkaConsumerRunner.js # Consumer service โโโ docker-compose.yml # Docker orchestration โโโ Dockerfile # Container image
## ๐ง Configuration
Key environment variables:
```env
# Server
WEB_APP_PORT=3000
NODE_ENV=production
# MongoDB
MONGO_CONNECT_STRING=mongodb://localhost:27017/chatflux
# Redis
REDIS_HOST=localhost
REDIS_PORT=6379
REDIS_CHANNEL=chat-messages
# Kafka
KAFKA_BROKERS=localhost:9092
KAFKA_TOPIC=chat-messages
KAFKA_GROUP_ID=chat-consumer-group
GET /health
GET /metrics
POST /get_messages
Content-Type: application/json
{
"limit": 50,
"roomId": "global"
}
GET /stats
chat message - Send a messagetyping - Typing indicatorchat message - Receive broadcast messageuser typing - User typing notificationerror - Error messagesAccess Prometheus metrics at /metrics:
chat_messages_total - Total messages processedchat_active_connections - Active WebSocket connectionschat_message_processing_duration_seconds - Processing latencykafka_batch_size - Kafka batch sizesredis_operation_duration_seconds - Redis operation times# Run tests
npm test
# Lint code
npm run lint
โ
Horizontal scalability with Redis Pub/Sub
โ
Message persistence with Kafka + MongoDB
โ
Rate limiting & input validation
โ
Structured logging with Winston
โ
Prometheus metrics integration
โ
Graceful shutdown handling
โ
Health checks & monitoring
โ
Docker containerization
Set environment to production
NODE_ENV=production
Use process manager
pm2 start index.js -i max --name chatflux-app
pm2 start kafkaConsumerRunner.js --name chatflux-consumer
Setup reverse proxy (nginx) ```nginx server { listen 80; server_name your-domain.com;
location / { proxy_pass http://localhost:3000; proxy_http_version 1.1; proxy_set_header Upgrade $http_upgrade; proxy_set_header Connection 'upgrade'; proxy_set_header Host $host; proxy_cache_bypass $http_upgrade; }
}
```
ISC
Contributions welcome! Please open an issue or submit a PR.