A production-ready real-time chat system built with Erlang/OTP and Node.js
Features β’ Architecture β’ Quick Start β’ Configuration β’ Deployment
- β OTP Supervision Tree with fault tolerance
- β Secure password hashing with salt + SHA-512
- β JWT-like session tokens with expiry
- β Rate limiting to prevent brute force attacks
- β ETS tables for in-memory data storage
- β Connection pooling via supervisor limits
- β Structured logging with audit trail
- β WebSocket Server with connection management
- β Security headers via Helmet
- β CORS support for cross-origin requests
- β Rate limiting per IP and per connection
- β Structured JSON logging with Pino
- β
Health check endpoint (
/health) - β
Prometheus metrics (
/metrics) - β Graceful shutdown handling
- β Docker support for both services
- β Docker Compose for local development
- β GitHub Actions CI/CD pipeline
- β Security scanning with Trivy and Snyk
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
β CLIENT LAYER β
β βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ β
β β index.html (Web UI) β β
β β β’ Telegram/WhatsApp-style interface β β
β β β’ Dark theme with responsive design β β
β β β’ WebSocket connection management β β
β ββββββββββββββββββββββββββ¬βββββββββββββββββββββββββββββββββββββ β
βββββββββββββββββββββββββββββββΌβββββββββββββββββββββββββββββββββββββββ
β WebSocket (ws://)
βββββββββββββββββββββββββββββββ΄βββββββββββββββββββββββββββββββββββββββ
β RELAY LAYER (Node.js) β
β βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ β
β β server.js (WebSocket β TCP relay) β β
β β β’ Connection management & rate limiting β β
β β β’ Token verification & refresh β β
β β β’ Health & metrics endpoints β β
β β β’ Structured JSON logging β β
β ββββββββββββββββββββββββββ¬βββββββββββββββββββββββββββββββββββββ β
βββββββββββββββββββββββββββββββΌβββββββββββββββββββββββββββββββββββββββ
β TCP (localhost:5678)
βββββββββββββββββββββββββββββββ΄βββββββββββββββββββββββββββββββββββββββ
β CORE LAYER (Erlang/OTP) β
β βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ β
β β OTP Supervision Tree β β
β β βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ β β
β β β monarchs_app (Application) β β β
β β βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ β β
β β β² β β
β β βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ β β
β β β monarchs_sup (Root Supervisor) β β β
β β β βββ monarchs_config (Config Server) β β β
β β β βββ monarchs_server (Main gen_server) β β β
β β β βββ monarchs_user_sup (User Supervisor) β β β
β β β βββ monarchs_room_sup (Room Supervisor) β β β
β β β βββ monarchs_connection_sup (Connection Supervisor) β β β
β β βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ β β
β β β β β
β β βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ β β
β β β ETS Tables β β β
β β β β’ monarchs_users (User registry) β β β
β β β β’ monarchs_rooms (Room registry) β β β
β β β β’ monarchs_messages (Message history) β β β
β β β β’ monarchs_sessions (Session store) β β β
β β βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ β β
β βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ β
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
- Node.js >= 18.0.0
- Erlang/OTP >= 26
- Docker & Docker Compose (optional)
# Clone the repository
git clone https://github.com/your-org/monarchs.git
cd monarchs
# Copy environment file
cp .env.example .env
# Edit .env with your configuration
nano .env
# Start all services
docker-compose up -d
# Check logs
docker-compose logs -f
# Verify health
curl http://localhost:8080/health# Clone the repository
git clone https://github.com/your-org/monarchs.git
cd monarchs
# Install Node.js dependencies
npm install
# Start Erlang backend (in one terminal)
erl -pa src -s monarchs_app -noshell
# Start Node.js relay (in another terminal)
npm start
# Open browser
# Navigate to http://localhost:8080Monarchs/
βββ src/ # Erlang/OTP Backend
β βββ monarchs_app.erl # Application callback
β βββ monarchs_sup.erl # Root supervisor
β βββ monarchs_config.erl # Configuration server
β βββ monarchs_server.erl # Main gen_server
β βββ monarchs_user_sup.erl # User process supervisor
β βββ monarchs_room_sup.erl # Room process supervisor
β βββ monarchs_connection_sup.erl # Connection supervisor
β βββ monarchs_connection.erl # Connection handler
βββ tests/ # Test suite
β βββ server.test.js # Node.js tests
βββ server.js # WebSocket relay server
βββ config.yaml # Application configuration
βββ package.json # Node.js dependencies
βββ Dockerfile.erlang # Erlang backend image
βββ Dockerfile.node # Node.js relay image
βββ docker-compose.yml # Container orchestration
βββ .env.example # Environment template
βββ README.md # This file
All settings can be configured in config.yaml:
# Application Settings
app:
name: "monarchs"
version: "2.0.0"
environment: "production"
# Backend Settings
backend:
host: "0.0.0.0"
port: 5678
max_connections: 10000
# Security Settings
security:
bcrypt_cost: 12
token_expiry: 3600
refresh_token_expiry: 86400
rate_limit:
max_attempts: 5
window_ms: 60000
password:
min_length: 8
require_uppercase: true
require_lowercase: true
require_numbers: true
# WebSocket Relay Settings
relay:
host: "0.0.0.0"
port: 8080
path: "/ws"
heartbeat_interval: 30000
max_payload_size: 65536
# Logging
logging:
level: "info"
format: "json"
audit:
enabled: true
# Monitoring
monitoring:
health:
enabled: true
path: "/health"
metrics:
enabled: true
path: "/metrics"Override configuration with environment variables:
| Variable | Description |
|---|---|
MONARCHS_TOKEN_SECRET |
Secret for JWT tokens (generate with openssl rand -hex 64) |
MONARCHS_PORT |
Erlang backend port (default: 5678) |
MONARCHS_RELAY_PORT |
WebSocket relay port (default: 8080) |
MONARCHS_LOG_LEVEL |
Logging level (debug, info, warn, error) |
NODE_ENV |
Environment (development, production) |
ERLANG_HOST |
Erlang backend hostname |
ERLANG_PORT |
Erlang backend port |
Register:
{"type": "register", "username": "user", "password": "pass"}Login:
{"type": "login", "username": "user", "password": "pass"}Response:
{
"type": "success",
"content": "Login successful!",
"token": "eyJhbG...",
"expiresIn": 3600000
}Create Room:
{"type": "create_room", "room_name": "general"}Join Room:
{"type": "join_room", "room_name": "general"}Leave Room:
{"type": "leave"}Send Message:
{"type": "message", "content": "Hello everyone!"}Private Message:
{"type": "private", "to_user": "username", "content": "Hello!"}Get Rooms:
{"type": "get_rooms"}Get Users:
{"type": "get_users"}GET /health
Response:
{
"status": "healthy",
"timestamp": "2024-01-15T10:30:00.000Z",
"uptime": 3600,
"memory": {...},
"erlang": {"connected": true, "lastPing": 1234567890},
"checks": {"erlang": "ok"}
}GET /metrics
Returns Prometheus-compatible metrics format.
GET /info
Returns service version and uptime.
- Password Hashing: SHA-512 with unique salt per user
- Session Tokens: JWT-like tokens with expiry
- Token Refresh: Automatic token refresh capability
- Login Attempts: 5 attempts per minute per IP
- Messages: 30 messages per second per connection
- Connections: 10 connections per IP
- HTTPS: Enable with reverse proxy (nginx, Traefik)
- WSS: WebSocket over TLS
- Helmet: Security headers
- Login attempts (success/failure)
- Registration events
- Session creation/destruction
- Security violations
# Install dependencies
npm install
# Run all tests
npm test
# Run with coverage
npm test -- --coverage
# Watch mode
npm run test:watch# Check code style
npm run lint
# Auto-fix issues
npm run lint:fix# Check for vulnerabilities
npm run security:audit# Build images
docker-compose build
# Deploy
docker-compose -f docker-compose.yml up -d
# Check status
docker-compose ps
# View logs
docker-compose logs -fSee k8s/ directory for Kubernetes manifests.
# Build Erlang backend
cd /path/to/monarchs
erlc -o src src/*.erl
erl -pa src -s monarchs_app -detached
# Build Node.js relay
npm ci --production
npm start# Check relay health
curl http://localhost:8080/health
# Check Prometheus metrics
curl http://localhost:8080/metrics# View relay logs
docker-compose logs -f node-relay
# View backend logs
docker-compose logs -f erlang-backend# (Future) Run migrations
npm run migrate# Restart without downtime
docker-compose restart node-relay# Backup session data
docker-compose exec redis redis-cli BGSAVE- Fork the repository
- Create a feature branch (
git checkout -b feature/amazing-feature) - Commit changes (
git commit -m 'Add amazing feature') - Push to branch (
git push origin feature/amazing-feature) - Open a Pull Request
This project is licensed under the ISC License - see the LICENSE file for details.
- Erlang/OTP for the robust BEAM VM
- Node.js for the efficient JavaScript runtime
- WebSocket for real-time communication