Skip to content

About

No description, website, or topics provided.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Latest commit

Β 

History

3 Commits

Folders and files

Repository files navigation

Distributed Real-Time Chat Platform

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.


πŸš€ Overview

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

πŸ—οΈ Architecture

                         β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
                         β”‚      Client      β”‚
                         β””β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
                                  β”‚
                            WebSocket / HTTP
                                  β”‚
                         β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β–Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”
                         β”‚ WebSocket Gatewayβ”‚
                         β”‚      :4000       β”‚
                         β””β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
                                  β”‚
                    β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
                    β”‚             β”‚             β”‚
                    β–Ό             β–Ό             β–Ό
             β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β” β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β” β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
             β”‚    Auth    β”‚ β”‚   Message  β”‚ β”‚ Notification β”‚
             β”‚   :4001    β”‚ β”‚   :4002    β”‚ β”‚    :4003     β”‚
             β””β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”˜ β””β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”˜ β””β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”˜
                   β”‚               β”‚              β”‚
                   β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
                                   β”‚
                         β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β–Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”
                         β”‚       Kafka       β”‚
                         β”‚  Event Streaming  β”‚
                         β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
                                   β”‚
                         β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β–Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”
                         β”‚      Redis        β”‚
                         β”‚ Cache / State     β”‚
                         β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
                                   β”‚
                         β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β–Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”
                         β”‚    PostgreSQL     β”‚
                         β”‚ Persistent Data   β”‚
                         β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

🧩 Services

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

πŸ› οΈ Tech Stack

Backend

  • Node.js
  • TypeScript
  • WebSockets

Distributed Systems

  • Apache Kafka
  • Redis

Database

  • PostgreSQL

Infrastructure

  • Docker
  • Docker Compose
  • npm Workspaces

Observability

  • Structured JSON logging
  • Service health endpoints

πŸ“ Project Structure

distributed-real-time-chat-platform/
β”‚
β”œβ”€β”€ apps/
β”‚   β”œβ”€β”€ gateway/
β”‚   β”‚   └── ...
β”‚   β”œβ”€β”€ auth/
β”‚   β”‚   └── ...
β”‚   β”œβ”€β”€ message/
β”‚   β”‚   └── ...
β”‚   └── notification/
β”‚       └── ...
β”‚
β”œβ”€β”€ packages/
β”‚   └── shared/
β”‚       └── ...
β”‚
β”œβ”€β”€ docker-compose.yml
β”œβ”€β”€ package.json
β”œβ”€β”€ package-lock.json
└── README.md

⚑ Getting Started

Prerequisites

Make sure you have the following installed:

1. Clone the repository

git clone <repository-url>
cd distributed-real-time-chat-platform

2. Install dependencies

npm install

3. Start the complete environment

npm run compose:up

This starts:

  • PostgreSQL
  • Redis
  • Kafka
  • WebSocket Gateway
  • Auth Service
  • Message Service
  • Notification Service

πŸ” Health Checks

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/health

Expected response:

{
  "status": "ok"
}

πŸ§ͺ Development

Run individual services locally:

npm run dev:gateway
npm run dev:auth
npm run dev:message
npm run dev:notification

Run TypeScript type checking:

npm run typecheck

Start the Docker environment:

npm run compose:up

πŸ”„ Message Flow

A 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.


βš™οΈ Distributed System Design

WebSockets

Persistent WebSocket connections provide low-latency bidirectional communication between clients and the gateway.

Kafka

Kafka acts as the event backbone between services.

It provides:

  • Asynchronous processing
  • Service decoupling
  • Event replay
  • Consumer groups
  • Traffic buffering

Redis

Redis is intended for latency-sensitive and ephemeral data such as:

  • User presence
  • Connection metadata
  • Caching
  • Distributed state

PostgreSQL

PostgreSQL provides durable persistence for authoritative application data such as:

  • Users
  • Conversations
  • Messages
  • Message metadata

πŸ“Š Current Status

Phase 0 β€” Infrastructure & Service Scaffold

  • 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

Phase 1 β€” Authentication & Users

  • User registration
  • User login
  • JWT authentication
  • Token validation
  • Authorization middleware
  • User profiles

Phase 2 β€” Real-Time Messaging

  • WebSocket authentication
  • One-to-one messaging
  • Group conversations
  • Message persistence
  • Message delivery acknowledgements
  • Read receipts

Phase 3 β€” Event-Driven Architecture

  • Kafka event contracts
  • Message producers
  • Kafka consumers
  • Consumer groups
  • Idempotent message processing
  • Retry and dead-letter handling

Phase 4 β€” Distributed State

  • Redis presence tracking
  • Online/offline status
  • Distributed WebSocket state
  • Multi-instance gateway support
  • Horizontal scaling

Phase 5 β€” Reliability & Observability

  • Distributed tracing
  • Metrics
  • Centralized logs
  • Rate limiting
  • Circuit breakers
  • Failure recovery
  • Load testing

Phase 6 β€” Production Deployment

  • CI/CD
  • Container registry
  • Cloud deployment
  • Service autoscaling
  • Database backups
  • Production monitoring

🎯 Engineering Goals

The project focuses on understanding the practical engineering problems involved in building distributed real-time systems.

1. Low-Latency Communication

Use persistent WebSocket connections to minimize communication overhead and provide real-time message delivery.

2. Service Isolation

Authentication, messaging, notifications, and connection management remain separate services with clear responsibilities.

3. Asynchronous Processing

Kafka decouples producers and consumers, allowing downstream workloads to process events independently.

4. Fast Ephemeral State

Redis provides a low-latency storage layer for presence, caching, and distributed connection state.

5. Durable Persistence

PostgreSQL acts as the persistent source of truth for application data.

6. Horizontal Scalability

Services are designed so multiple instances can run concurrently as system traffic increases.


πŸ—ΊοΈ Roadmap

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

πŸ“Œ Project Philosophy

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.


πŸ“„ License

This project is currently intended as an engineering and learning project.

About

No description, website, or topics provided.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages