A production-oriented distributed real-time messaging platform built with TypeScript, WebSockets, Kafka, Redis, PostgreSQL, and Docker.
A microservices-based chat platform designed to explore the engineering challenges behind low-latency communication, event-driven architecture, horizontal scalability, and distributed system reliability.
The platform separates authentication, real-time connections, message processing, and notifications into independently deployable services.
The architecture uses:
- WebSockets for real-time communication
- Apache Kafka for asynchronous event streaming
- Redis for low-latency state and caching
- PostgreSQL for persistent data
- Docker Compose for local infrastructure
- TypeScript for type-safe service development
ββββββββββββββββββββ
β Client β
ββββββββββ¬ββββββββββ
β
WebSocket / HTTP
β
ββββββββββΌββββββββββ
β WebSocket Gatewayβ
β :4000 β
ββββββββββ¬ββββββββββ
β
βββββββββββββββΌββββββββββββββ
β β β
βΌ βΌ βΌ
ββββββββββββββ ββββββββββββββ ββββββββββββββββ
β Auth β β Message β β Notification β
β :4001 β β :4002 β β :4003 β
βββββββ¬βββββββ ββββββββ¬ββββββ ββββββββ¬ββββββββ
β β β
βββββββββββββββββΌβββββββββββββββ
β
βββββββββββΌββββββββββ
β Kafka β
β Event Streaming β
βββββββββββ¬ββββββββββ
β
βββββββββββΌββββββββββ
β Redis β
β Cache / State β
βββββββββββββββββββββ
β
βββββββββββΌββββββββββ
β PostgreSQL β
β Persistent Data β
βββββββββββββββββββββ
| Service | Port | Responsibility |
|---|---|---|
| WebSocket Gateway | 4000 |
Maintains real-time client connections and routes events |
| Auth Service | 4001 |
Authentication, authorization, and identity management |
| Message Service | 4002 |
Message processing, persistence, and event publishing |
| Notification Service | 4003 |
Asynchronous notification processing |
- Node.js
- TypeScript
- WebSockets
- Apache Kafka
- Redis
- PostgreSQL
- Docker
- Docker Compose
- npm Workspaces
- Structured JSON logging
- Service health endpoints
distributed-real-time-chat-platform/
β
βββ apps/
β βββ gateway/
β β βββ ...
β βββ auth/
β β βββ ...
β βββ message/
β β βββ ...
β βββ notification/
β βββ ...
β
βββ packages/
β βββ shared/
β βββ ...
β
βββ docker-compose.yml
βββ package.json
βββ package-lock.json
βββ README.md
Make sure you have the following installed:
git clone <repository-url>
cd distributed-real-time-chat-platformnpm installnpm run compose:upThis starts:
- PostgreSQL
- Redis
- Kafka
- WebSocket Gateway
- Auth Service
- Message Service
- Notification Service
Every service exposes a /health endpoint.
| Service | Endpoint |
|---|---|
| WebSocket Gateway | http://localhost:4000/health |
| Auth | http://localhost:4001/health |
| Message | http://localhost:4002/health |
| Notification | http://localhost:4003/health |
Example:
curl http://localhost:4000/healthExpected response:
{
"status": "ok"
}Run individual services locally:
npm run dev:gatewaynpm run dev:authnpm run dev:messagenpm run dev:notificationRun TypeScript type checking:
npm run typecheckStart the Docker environment:
npm run compose:upA typical message flow through the platform is:
Client
β
β WebSocket
βΌ
WebSocket Gateway
β
β Publish Message Event
βΌ
Kafka
β
ββββββββββββββββββββββββ
β β
βΌ βΌ
Message Service Notification Service
β β
βΌ βΌ
PostgreSQL Notification
The gateway handles persistent client connections while Kafka decouples message processing from downstream consumers.
This allows message processing and notification workloads to scale independently.
Persistent WebSocket connections provide low-latency bidirectional communication between clients and the gateway.
Kafka acts as the event backbone between services.
It provides:
- Asynchronous processing
- Service decoupling
- Event replay
- Consumer groups
- Traffic buffering
Redis is intended for latency-sensitive and ephemeral data such as:
- User presence
- Connection metadata
- Caching
- Distributed state
PostgreSQL provides durable persistence for authoritative application data such as:
- Users
- Conversations
- Messages
- Message metadata
- npm workspace monorepo
- Shared TypeScript types
- Shared bootstrap utilities
- WebSocket Gateway service
- Authentication service
- Message service
- Notification service
- PostgreSQL container
- Redis container
- Kafka container
- Docker Compose environment
- Structured JSON logging
- Health endpoints
- TypeScript type checking
- User registration
- User login
- JWT authentication
- Token validation
- Authorization middleware
- User profiles
- WebSocket authentication
- One-to-one messaging
- Group conversations
- Message persistence
- Message delivery acknowledgements
- Read receipts
- Kafka event contracts
- Message producers
- Kafka consumers
- Consumer groups
- Idempotent message processing
- Retry and dead-letter handling
- Redis presence tracking
- Online/offline status
- Distributed WebSocket state
- Multi-instance gateway support
- Horizontal scaling
- Distributed tracing
- Metrics
- Centralized logs
- Rate limiting
- Circuit breakers
- Failure recovery
- Load testing
- CI/CD
- Container registry
- Cloud deployment
- Service autoscaling
- Database backups
- Production monitoring
The project focuses on understanding the practical engineering problems involved in building distributed real-time systems.
Use persistent WebSocket connections to minimize communication overhead and provide real-time message delivery.
Authentication, messaging, notifications, and connection management remain separate services with clear responsibilities.
Kafka decouples producers and consumers, allowing downstream workloads to process events independently.
Redis provides a low-latency storage layer for presence, caching, and distributed connection state.
PostgreSQL acts as the persistent source of truth for application data.
Services are designed so multiple instances can run concurrently as system traffic increases.
Phase 0
Infrastructure & Scaffolding
β
βΌ
Phase 1
Authentication & Users
β
βΌ
Phase 2
Real-Time Messaging
β
βΌ
Phase 3
Kafka Event Architecture
β
βΌ
Phase 4
Redis Presence & Horizontal Scaling
β
βΌ
Phase 5
Observability & Reliability
β
βΌ
Phase 6
Load Testing & Production Deployment
The goal is to understand how real-time systems behave when traffic increases, services fail, messages are duplicated, and individual components need to scale independently.
The architecture therefore prioritizes:
Reliability
β
Consistency
β
Scalability
β
Observability
β
Performance
Complexity is introduced only when it solves a measurable engineering problem.
This project is currently intended as an engineering and learning project.