chatflux Svelte Themes

Chatflux

A scalable, distributed real-time chat application built with Express.js, Socket.IO, Redis, Kafka, ZooKeeper, and MongoDB. Features low-latency messaging, fault tolerance, and a modern Svelte frontend.

ChatFlux ๐Ÿš€

A distributed, real-time chat platform built with modern microservices architecture.

๐Ÿ—๏ธ Architecture

User โ†’ Socket.IO โ†’ Express Server
              โ†“
    โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ดโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
    โ†“                   โ†“
Redis Pub/Sub      Kafka Queue
    โ†“                   โ†“
Broadcast          Consumer Batch
                        โ†“
                   MongoDB

๐Ÿ› ๏ธ Tech Stack

  • Backend: Node.js, Express.js
  • Real-time: Socket.IO
  • Message Queue: Apache Kafka
  • Caching: Redis (Pub/Sub + Cache)
  • Database: MongoDB
  • Monitoring: Prometheus metrics
  • Logging: Winston

๐Ÿ“‹ Prerequisites

  • Node.js 18+
  • MongoDB 7+
  • Redis 7+
  • Apache Kafka 3.5+
  • Docker & Docker Compose (optional)

๐Ÿš€ Quick Start

# Clone the repository
git clone <your-repo>
cd chatflux

# Start all services
docker-compose up -d

# View logs
docker-compose logs -f chatflux-app

Manual Setup

  1. Install dependencies

    npm install
    
  2. Configure environment

    cp .env.example .env
    # Edit .env with your configuration
    
  3. Start infrastructure (MongoDB, Redis, Kafka)

    # Using Docker for infrastructure only
    docker-compose up -d mongodb redis kafka
    
  4. Run the application ```bash

    Terminal 1: Main server

    npm start

Terminal 2: Kafka consumer

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

๐Ÿ“Š API Endpoints

Health Check

GET /health

Metrics (Prometheus)

GET /metrics

Get Messages

POST /get_messages
Content-Type: application/json

{
  "limit": 50,
  "roomId": "global"
}

Statistics

GET /stats

๐Ÿ”Œ Socket.IO Events

Client โ†’ Server

  • chat message - Send a message
  • typing - Typing indicator

Server โ†’ Client

  • chat message - Receive broadcast message
  • user typing - User typing notification
  • error - Error messages

๐Ÿ“ˆ Monitoring

Access Prometheus metrics at /metrics:

  • chat_messages_total - Total messages processed
  • chat_active_connections - Active WebSocket connections
  • chat_message_processing_duration_seconds - Processing latency
  • kafka_batch_size - Kafka batch sizes
  • redis_operation_duration_seconds - Redis operation times

๐Ÿงช Testing

# Run tests
npm test

# Lint code
npm run lint

๐ŸŽฏ Features

โœ… 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

๐Ÿ” Security

  • Helmet.js for security headers
  • Rate limiting on API endpoints
  • Input validation with Joi
  • CORS protection
  • Message size limits

๐Ÿšฆ Production Deployment

  1. Set environment to production

    NODE_ENV=production
    
  2. Use process manager

    pm2 start index.js -i max --name chatflux-app
    pm2 start kafkaConsumerRunner.js --name chatflux-consumer
    
  3. 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; }

}

```

๐Ÿ“ License

ISC

๐Ÿค Contributing

Contributions welcome! Please open an issue or submit a PR.

Top categories

Loading Svelte Themes