Skip to content

arafat2020/node_chat_microservice_kafaka

Folders and files

NameName
Last commit message
Last commit date

Latest commit

ย 

History

62 Commits
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 

Repository files navigation

๐Ÿš€ Event-Driven Microservices Chat Platform

A production-ready, scalable microservices architecture built with NestJS, Apache Kafka, and Nx Monorepo. This project demonstrates modern backend development practices including event-driven architecture, domain-driven design, and containerized infrastructure.

NestJS Apache Kafka Docker Nx PostgreSQL Prisma

๐Ÿ“‹ Table of Contents

๐ŸŽฏ Overview

This project is a research and development initiative exploring microservices architecture and event-driven design patterns using Apache Kafka. It implements a real-time chat platform with server/channel management, user authentication, file handling, and messaging capabilities.

Key Features

โœ… Event-Driven Architecture - Asynchronous communication using Apache Kafka
โœ… Microservices Pattern - Loosely coupled, independently deployable services
โœ… Nx Monorepo - Efficient code sharing and dependency management
โœ… Type-Safe DTOs - Shared data transfer objects across services
โœ… Database per Service - Each microservice manages its own PostgreSQL database
โœ… Docker Compose - Complete infrastructure orchestration
โœ… Kafka UI - Real-time monitoring of message streams
โœ… API Gateway - Centralized entry point with Swagger documentation

๐Ÿ—๏ธ Architecture

โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
โ”‚                        API Gateway                          โ”‚
โ”‚                    (HTTP REST Interface)                    โ”‚
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
                 โ”‚
                 โ–ผ
    โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
    โ”‚         Apache Kafka Broker            โ”‚
    โ”‚    (Event Bus & Message Queue)         โ”‚
    โ””โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
      โ”‚        โ”‚        โ”‚        โ”‚
      โ–ผ        โ–ผ        โ–ผ        โ–ผ
   โ”Œโ”€โ”€โ”€โ”€โ”€โ” โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ” โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ” โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”
   โ”‚Auth โ”‚ โ”‚Serverโ”‚ โ”‚ Msg  โ”‚ โ”‚ File โ”‚
   โ”‚Svc  โ”‚ โ”‚ Svc  โ”‚ โ”‚ Svc  โ”‚ โ”‚ Svc  โ”‚
   โ””โ”€โ”€โ”ฌโ”€โ”€โ”˜ โ””โ”€โ”€โ”ฌโ”€โ”€โ”€โ”˜ โ””โ”€โ”€โ”ฌโ”€โ”€โ”€โ”˜ โ””โ”€โ”€โ”ฌโ”€โ”€โ”€โ”˜
      โ”‚       โ”‚        โ”‚        โ”‚
      โ–ผ       โ–ผ        โ–ผ        โ–ผ
    โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
    โ”‚      PostgreSQL Databases      โ”‚
    โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜

Communication Flow

  1. Client โ†’ HTTP Request โ†’ API Gateway
  2. API Gateway โ†’ Kafka Event โ†’ Microservice
  3. Microservice โ†’ Process Event โ†’ Database Operation
  4. Microservice โ†’ Kafka Response โ†’ API Gateway
  5. API Gateway โ†’ HTTP Response โ†’ Client

๐Ÿ› ๏ธ Tech Stack

Core Technologies

Technology Purpose Version
NestJS Backend framework 11.x
Apache Kafka Event streaming platform 7.6.1
Nx Monorepo management 21.5.2
TypeScript Programming language 5.9.2
Prisma ORM & Database toolkit 6.16.2
PostgreSQL Relational database Latest

Infrastructure

  • Docker & Docker Compose - Containerization
  • Zookeeper - Kafka coordination service
  • Kafka UI - Stream monitoring (Port 8080)
  • Swagger/OpenAPI - API documentation

Additional Libraries

  • class-validator & class-transformer - DTO validation
  • argon2 - Password hashing
  • AWS SDK - S3 file storage
  • BullMQ - Job queue management
  • uuid - Unique identifier generation

๐Ÿ”ง Microservices

1. API Gateway (api_geteway)

  • Port: 3001
  • Type: HTTP REST API
  • Responsibilities:
    • Client-facing REST endpoints
    • Request routing to microservices
    • Swagger API documentation
    • Bearer token authentication
  • Endpoints: /api/docs (Swagger UI)

2. Auth Service (auth-service)

  • Transport: Kafka Microservice
  • Consumer Group: auth-consumer-group
  • Responsibilities:
    • User authentication & authorization
    • JWT token generation & validation
    • Password hashing with Argon2
    • User profile management
  • Message Patterns:
    • user.signin - User login
    • user.signup - User registration
    • user.verifyToken - Token validation

3. Server Service (server-service)

  • Transport: Kafka Microservice
  • Consumer Group: server-consumer-group
  • Responsibilities:
    • Discord-like server management
    • Channel creation & management
    • Member roles & permissions
    • Invite code generation
  • Message Patterns:
    • create.server, delete.server, update.server
    • get.server, get.involved.server
    • leave.server, is.server.exists
    • channel.create, channel.update, channel.delete, channel.get

4. Message Service (message-service)

  • Transport: Kafka Microservice
  • Consumer Group: message-consumer-group
  • Responsibilities:
    • Real-time message handling
    • Message persistence
    • Message history retrieval

5. File Service (file-service)

  • Port: 3002
  • Transport: Hybrid (HTTP + Kafka)
  • Consumer Group: file-service-consumer
  • Responsibilities:
    • File upload/download
    • AWS S3 integration
    • Presigned URL generation
    • File metadata management

๐Ÿš€ Getting Started

Prerequisites

  • Node.js (v18 or higher)
  • Docker & Docker Compose
  • npm or yarn

Installation

  1. Clone the repository

    git clone <repository-url>
    cd node_chat
  2. Install dependencies

    npm install
  3. Start infrastructure services

    docker compose up -d

    This starts:

    • Zookeeper (Port 2181)
    • Kafka Broker (Ports 9092, 29092)
    • Kafka UI (Port 8080)
  4. Set up environment variables

    Each service has its own .env file. Configure:

    • Database URLs (PostgreSQL)
    • AWS credentials (for file service)
    • JWT secrets (for auth service)
  5. Run database migrations

    # For each service with Prisma
    npx nx run auth-service:prisma-migrate
    npx nx run server-service:prisma-migrate
    npx nx run message-service:prisma-migrate
    npx nx run file-service:prisma-migrate
  6. Start all services

    # API Gateway
    npx nx serve api_geteway
    
    # Microservices (in separate terminals)
    npx nx serve auth-service
    npx nx serve server-service
    npx nx serve message-service
    npx nx serve file-service

Verify Installation

๐Ÿ“ Project Structure

node_chat/
โ”œโ”€โ”€ apps/
โ”‚   โ”œโ”€โ”€ api_geteway/          # HTTP REST API Gateway
โ”‚   โ”œโ”€โ”€ auth-service/         # Authentication microservice
โ”‚   โ”œโ”€โ”€ server-service/       # Server & channel management
โ”‚   โ”œโ”€โ”€ message-service/      # Message handling
โ”‚   โ”œโ”€โ”€ file-service/         # File storage & retrieval
โ”‚   โ””โ”€โ”€ *-e2e/               # End-to-end tests
โ”œโ”€โ”€ shared/                   # Shared library
โ”‚   โ””โ”€โ”€ src/
โ”‚       โ””โ”€โ”€ lib/
โ”‚           โ”œโ”€โ”€ dto/          # Data Transfer Objects
โ”‚           โ”œโ”€โ”€ guards/       # Auth guards
โ”‚           โ”œโ”€โ”€ interfaces/   # TypeScript interfaces
โ”‚           โ”œโ”€โ”€ types/        # Type definitions
โ”‚           โ””โ”€โ”€ pipes/        # Validation pipes
โ”œโ”€โ”€ compose.yaml              # Docker Compose configuration
โ”œโ”€โ”€ nx.json                   # Nx workspace configuration
โ””โ”€โ”€ package.json              # Root dependencies

Shared Library

The shared library provides:

  • DTOs - Type-safe data transfer objects for all services
  • Guards - Microservice authentication guards
  • Interfaces - Common TypeScript interfaces
  • Types - Shared type definitions
  • Utilities - Common helper functions

This ensures consistency across microservices and reduces code duplication.

๐Ÿ“ก Event Patterns

Authentication Events

// user.signin
{ email: string, password: string }

// user.signup
{ name: string, email: string, password: string }

// user.verifyToken
{ token: string }

Server Management Events

// create.server
{ name: string, imageUrl: string, profileId: string }

// channel.create
{ name: string, type: 'TEXT' | 'AUDIO' | 'VIDEO', serverId: string }

๐Ÿ—„๏ธ Database Schema

Auth Service - Profile

model Profile {
  id        String   @id @default(uuid())
  name      String
  email     String   @unique
  password  String?
  createdAt DateTime @default(now())
  updatedAt DateTime @updatedAt
}

Server Service - Server, Channel, Member

model Server {
  id         String    @id @default(uuid())
  name       String
  imageUrl   String
  inviteCode String    @unique
  profileId  String
  members    Member[]
  channels   Channel[]
  createdAt  DateTime  @default(now())
  updatedAt  DateTime  @updatedAt
}

model Channel {
  id        String      @id @default(uuid())
  name      String
  type      ChannelType @default(TEXT)
  serverId  String
  profileId String
  createdAt DateTime    @default(now())
  updatedAt DateTime    @updatedAt
}

enum ChannelType { TEXT, AUDIO, VIDEO }
enum MemberRole { ADMIN, MODERATOR, GUEST }

๐Ÿ’ป Development

Run a specific service

npx nx serve <service-name>

Build for production

npx nx build <service-name>

Run tests

npx nx test <service-name>
npx nx e2e <service-name>-e2e

View dependency graph

npx nx graph

Lint code

npx nx lint <service-name>

Generate Prisma client

npx nx run <service-name>:prisma-generate

๐Ÿ“š API Documentation

Once the API Gateway is running, access the interactive Swagger documentation:

URL: http://localhost:3001/api/docs

The Swagger UI provides:

  • Complete API endpoint documentation
  • Request/response schemas
  • Try-it-out functionality
  • Bearer token authentication

๐ŸŽ“ Learning Outcomes

This project demonstrates proficiency in:

  • โœ… Designing and implementing microservices architecture
  • โœ… Event-driven communication with Apache Kafka
  • โœ… Managing monorepos with Nx
  • โœ… Database design and ORM usage (Prisma)
  • โœ… Docker containerization and orchestration
  • โœ… RESTful API design and documentation
  • โœ… Authentication and authorization patterns
  • โœ… Type-safe development with TypeScript
  • โœ… Code sharing and reusability strategies
  • โœ… Scalable backend architecture patterns

๐Ÿ”ฎ Future Enhancements

  • WebSocket integration for real-time messaging
  • Redis caching layer
  • Kubernetes deployment manifests
  • CI/CD pipeline with GitHub Actions
  • Distributed tracing with Jaeger
  • Rate limiting and API throttling
  • Comprehensive integration tests
  • Message encryption
  • Multi-tenancy support

๐Ÿ“ License

MIT


Built with โค๏ธ as a research project exploring modern microservices architecture

About

An R&D project to explore the complexity of microservices by thinking of everything as an event.

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors