| 1 | # ChatApp |
| 2 | |
| 3 | A production-ready, microservices-based real-time chat application built with TypeScript and Node.js. This project implements an API Gateway pattern with event-driven architecture, featuring JWT authentication, multi-database persistence, and Redis caching. |
| 4 | |
| 5 | ## Table of Contents |
| 6 | |
| 7 | - [Architecture](#architecture) |
| 8 | - [Services](#services) |
| 9 | - [Technology Stack](#technology-stack) |
| 10 | - [Prerequisites](#prerequisites) |
| 11 | - [Getting Started](#getting-started) |
| 12 | - [Configuration](#configuration) |
| 13 | - [API Reference](#api-reference) |
| 14 | - [Development](#development) |
| 15 | - [Project Structure](#project-structure) |
| 16 | - [Contributing](#contributing) |
| 17 | - [License](#license) |
| 18 | |
| 19 | ## Architecture |
| 20 | |
| 21 | The application follows a microservices architecture with the following key patterns: |
| 22 | |
| 23 | - **API Gateway Pattern**: All client requests flow through a single gateway service that handles authentication, request validation, and routing to internal services. |
| 24 | - **Event-Driven Architecture**: Services communicate asynchronously through RabbitMQ for loose coupling and eventual consistency. |
| 25 | - **Database per Service**: Each service manages its own database, promoting independence and scalability. |
| 26 | |
| 27 | **Request Flow**: Client requests hit the Gateway Service, which validates JWT tokens and forwards requests to the appropriate internal service (Auth, User, or Chat). Services communicate with each other through RabbitMQ events (e.g., user registration events are published by Auth Service and consumed by User Service to sync profile data). |
| 28 | |
| 29 | ## Services |
| 30 | |
| 31 | ### Gateway Service (Port 4000) |
| 32 | |
| 33 | The public-facing entry point for all API requests. Responsibilities include: |
| 34 | |
| 35 | - JWT token validation and user authentication |
| 36 | - Request routing to internal services |
| 37 | - Request/response transformation |
| 38 | - Rate limiting and security headers (Helmet) |
| 39 | |
| 40 | ### Auth Service (Port 4003) |
| 41 | |
| 42 | Manages user authentication and authorization: |
| 43 | |
| 44 | - User registration with password hashing (bcrypt) |
| 45 | - Login with credential validation |
| 46 | - JWT access token and refresh token generation |
| 47 | - Token refresh with rotation |
| 48 | - Token revocation |
| 49 | - Publishes user registration events to RabbitMQ |
| 50 | |
| 51 | ### User Service (Port 4001) |
| 52 | |
| 53 | Manages user profile data: |
| 54 | |
| 55 | - User profile retrieval |
| 56 | - User search functionality |
| 57 | - Consumes auth events to sync user data from registration |
| 58 | |
| 59 | ### Chat Service (Port 4002) |
| 60 | |
| 61 | Handles real-time messaging features: |
| 62 | |
| 63 | - Conversation creation and management |
| 64 | - Message sending and retrieval |
| 65 | - Redis caching for conversation data |
| 66 | - Message pagination with cursor-based navigation |
| 67 | |
| 68 | ## Technology Stack |
| 69 | |
| 70 | | Category | Technology | |
| 71 | | --------------------- | -------------------- | |
| 72 | | Runtime | Node.js 22 | |
| 73 | | Language | TypeScript (ESM) | |
| 74 | | Package Manager | pnpm 10 (workspaces) | |
| 75 | | Web Framework | Express 5 | |
| 76 | | Auth Service Database | MySQL 8.0 | |
| 77 | | User Service Database | PostgreSQL 16 | |
| 78 | | Chat Service Database | MongoDB 7 | |
| 79 | | Caching | Redis 7 | |
| 80 | | Message Broker | RabbitMQ 3 | |
| 81 | | ORM | Sequelize 6 | |
| 82 | | Validation | Zod | |
| 83 | | Logging | Pino | |
| 84 | | Containerization | Docker | |
| 85 | |
| 86 | ## Prerequisites |
| 87 | |
| 88 | - Node.js 22 or higher |
| 89 | - pnpm 10 or higher |
| 90 | - Docker and Docker Compose |
| 91 | - Git |
| 92 | |
| 93 | ## Getting Started |
| 94 | |
| 95 | ### 1. Clone the repository |
| 96 | |
| 97 | ```bash |
| 98 | git clone https://github.com/fiston-user/chatapp-yt.git |
| 99 | cd chatapp-yt |
| 100 | ``` |
| 101 | |
| 102 | ### 2. Install dependencies |
| 103 | |
| 104 | ```bash |
| 105 | pnpm install |
| 106 | ``` |
| 107 | |
| 108 | ### 3. Configure environment variables |
| 109 | |
| 110 | Create a `.env` file in the root directory based on the example: |
| 111 | |
| 112 | ```bash |
| 113 | cp .env.example .env |
| 114 | ``` |
| 115 | |
| 116 | Update the environment variables as needed. |
| 117 | |
| 118 | ### 4. Start infrastructure services |
| 119 | |
| 120 | ```bash |
| 121 | docker compose up -d rabbitmq redis mongo user-db auth-db |
| 122 | ``` |
| 123 | |
| 124 | ### 5. Build the common package |
| 125 | |
| 126 | ```bash |
| 127 | pnpm --filter @chatapp/common build |
| 128 | ``` |
| 129 | |
| 130 | ### 6. Start all services (development) |
| 131 | |
| 132 | ```bash |
| 133 | pnpm dev |
| 134 | ``` |
| 135 | |
| 136 | Or start individual services: |
| 137 | |
| 138 | ```bash |
| 139 | pnpm --filter gateway-service dev |
| 140 | pnpm --filter @chatapp/auth-service dev |
| 141 | pnpm --filter @chatapp/user-service dev |
| 142 | pnpm --filter chat-service dev |
| 143 | ``` |
| 144 | |
| 145 | ### Running with Docker Compose (Production) |
| 146 | |
| 147 | To run the entire stack with Docker: |
| 148 | |
| 149 | ```bash |
| 150 | docker compose up -d |
| 151 | ``` |
| 152 | |
| 153 | This will start all services and their dependencies with health checks and automatic restarts. |
| 154 | |
| 155 | ## Configuration |
| 156 | |
| 157 | ### Environment Variables |
| 158 | |
| 159 | | Variable | Description | Default | |
| 160 | | ------------------------ | --------------------------------- | ------------- | |
| 161 | | `NODE_ENV` | Environment mode | `development` | |
| 162 | | `GATEWAY_PORT` | Gateway service port | `4000` | |
| 163 | | `AUTH_SERVICE_PORT` | Auth service port | `4003` | |
| 164 | | `USER_SERVICE_PORT` | User service port | `4001` | |
| 165 | | `CHAT_SERVICE_PORT` | Chat service port | `4002` | |
| 166 | | `JWT_SECRET` | Secret for signing access tokens | - | |
| 167 | | `JWT_EXPIRES_IN` | Access token expiry | `1d` | |
| 168 | | `JWT_REFRESH_SECRET` | Secret for signing refresh tokens | - | |
| 169 | | `JWT_REFRESH_EXPIRES_IN` | Refresh token expiry | `30d` | |
| 170 | | `INTERNAL_API_TOKEN` | Token for inter-service auth | - | |
| 171 | | `AUTH_DB_URL` | MySQL connection URL | - | |
| 172 | | `USER_DB_URL` | PostgreSQL connection URL | - | |
| 173 | | `MONGO_URL` | MongoDB connection URL | - | |
| 174 | | `REDIS_URL` | Redis connection URL | - | |
| 175 | | `RABBITMQ_URL` | RabbitMQ connection URL | - | |
| 176 | |
| 177 | ### Default Infrastructure Ports |
| 178 | |
| 179 | | Service | Port | |
| 180 | | ------------------- | ----- | |
| 181 | | RabbitMQ | 5672 | |
| 182 | | RabbitMQ Management | 15672 | |
| 183 | | Redis | 6379 | |
| 184 | | MongoDB | 27017 | |
| 185 | | PostgreSQL | 5432 | |
| 186 | | MySQL | 3306 | |
| 187 | |
| 188 | ## API Reference |
| 189 | |
| 190 | ### Authentication Endpoints |
| 191 | |
| 192 | All authentication endpoints are accessed through the gateway service at `http://localhost:4000`. |
| 193 | |
| 194 | #### Register User |
| 195 | |
| 196 | ```http |
| 197 | POST /auth/register |
| 198 | Content-Type: application/json |
| 199 | |
| 200 | { |
| 201 | "email": "user@example.com", |
| 202 | "displayName": "John Doe", |
| 203 | "password": "SecurePassword123!" |
| 204 | } |
| 205 | ``` |
| 206 | |
| 207 | **Response** (201 Created): |
| 208 | |
| 209 | ```json |
| 210 | { |
| 211 | "accessToken": "eyJhbGciOiJIUzI1NiIs...", |
| 212 | "refreshToken": "eyJhbGciOiJIUzI1NiIs...", |
| 213 | "user": { |
| 214 | "id": "uuid", |
| 215 | "email": "user@example.com", |
| 216 | "displayName": "John Doe", |
| 217 | "createdAt": "2024-01-01T00:00:00.000Z" |
| 218 | } |
| 219 | } |
| 220 | ``` |
| 221 | |
| 222 | #### Login |
| 223 | |
| 224 | ```http |
| 225 | POST /auth/login |
| 226 | Content-Type: application/json |
| 227 | |
| 228 | { |
| 229 | "email": "user@example.com", |
| 230 | "password": "SecurePassword123!" |
| 231 | } |
| 232 | ``` |
| 233 | |
| 234 | **Response** (200 OK): |
| 235 | |
| 236 | ```json |
| 237 | { |
| 238 | "accessToken": "eyJhbGciOiJIUzI1NiIs...", |
| 239 | "refreshToken": "eyJhbGciOiJIUzI1NiIs..." |
| 240 | } |
| 241 | ``` |
| 242 | |
| 243 | #### Refresh Tokens |
| 244 | |
| 245 | ```http |
| 246 | POST /auth/refresh |
| 247 | Content-Type: application/json |
| 248 | |
| 249 | { |
| 250 | "refreshToken": "eyJhbGciOiJIUzI1NiIs..." |
| 251 | } |
| 252 | ``` |
| 253 | |
| 254 | #### Revoke Tokens |
| 255 | |
| 256 | ```http |
| 257 | POST /auth/revoke |
| 258 | Content-Type: application/json |
| 259 | |
| 260 | { |
| 261 | "userId": "uuid" |
| 262 | } |
| 263 | ``` |
| 264 | |
| 265 | ### Conversation Endpoints |
| 266 | |
| 267 | All conversation endpoints require authentication via Bearer token. |
| 268 | |
| 269 | #### Create Conversation |
| 270 | |
| 271 | ```http |
| 272 | POST /conversations |
| 273 | Authorization: Bearer <access_token> |
| 274 | Content-Type: application/json |
| 275 | |
| 276 | { |
| 277 | "title": "Project Discussion", |
| 278 | "participantIds": ["user-id-1", "user-id-2"] |
| 279 | } |
| 280 | ``` |
| 281 | |
| 282 | #### List Conversations |
| 283 | |
| 284 | ```http |
| 285 | GET /conversations |
| 286 | Authorization: Bearer <access_token> |
| 287 | ``` |
| 288 | |
| 289 | #### Get Conversation |
| 290 | |
| 291 | ```http |
| 292 | GET /conversations/:id |
| 293 | Authorization: Bearer <access_token> |
| 294 | ``` |
| 295 | |
| 296 | ### Message Endpoints |
| 297 | |
| 298 | #### Send Message |
| 299 | |
| 300 | ```http |
| 301 | POST /conversations/:id/messages |
| 302 | Authorization: Bearer <access_token> |
| 303 | Content-Type: application/json |
| 304 | |
| 305 | { |
| 306 | "body": "Hello, world!" |
| 307 | } |
| 308 | ``` |
| 309 | |
| 310 | #### List Messages |
| 311 | |
| 312 | ```http |
| 313 | GET /conversations/:id/messages?limit=50&after=<message_id> |
| 314 | Authorization: Bearer <access_token> |
| 315 | ``` |
| 316 | |
| 317 | ### Health Checks |
| 318 | |
| 319 | Each service exposes a health endpoint: |
| 320 | |
| 321 | ```http |
| 322 | GET /health |
| 323 | ``` |
| 324 | |
| 325 | **Response**: |
| 326 | |
| 327 | ```json |
| 328 | { |
| 329 | "status": "ok", |
| 330 | "service": "<service-name>" |
| 331 | } |
| 332 | ``` |
| 333 | |
| 334 | ## Development |
| 335 | |
| 336 | ### Scripts |
| 337 | |
| 338 | | Command | Description | |
| 339 | | ---------------- | -------------------------------------- | |
| 340 | | `pnpm dev` | Start all services in development mode | |
| 341 | | `pnpm build` | Build all packages and services | |
| 342 | | `pnpm lint` | Run ESLint on all packages | |
| 343 | | `pnpm format` | Check code formatting with Prettier | |
| 344 | | `pnpm typecheck` | Run TypeScript type checking | |
| 345 | |
| 346 | ### Code Quality |
| 347 | |
| 348 | The project uses: |
| 349 | |
| 350 | - ESLint for code linting |
| 351 | - Prettier for code formatting |
| 352 | - TypeScript strict mode for type safety |
| 353 | |
| 354 | ## Project Structure |
| 355 | |
| 356 | This is a monorepo managed with pnpm workspaces. The root contains `docker-compose.yml` for infrastructure, along with shared TypeScript and workspace configs. |
| 357 | |
| 358 | **packages/common**: Shared library containing error classes, event types, HTTP middleware, validation utilities, and logging setup. All services depend on this package. |
| 359 | |
| 360 | **services/gateway-service**: The API gateway. Contains controllers that proxy requests to internal services, JWT auth middleware, route definitions, and Zod validation schemas. |
| 361 | |
| 362 | **services/auth-service**: Handles authentication. Contains Sequelize models for user credentials and refresh tokens, JWT token utilities, and a RabbitMQ publisher that emits events when users register. |
| 363 | |
| 364 | **services/user-service**: Manages user profiles. Contains a RabbitMQ consumer that listens for auth events to sync user data, plus repositories and services for user CRUD operations. |
| 365 | |
| 366 | **services/chat-service**: Handles messaging. Uses MongoDB for storing conversations and messages, Redis for caching, and follows a repository pattern for data access. |
| 367 | |
| 368 | ## Contributing |
| 369 | |
| 370 | 1. Fork the repository |
| 371 | 2. Create your feature branch (`git checkout -b feature/amazing-feature`) |
| 372 | 3. Commit your changes (`git commit -m 'Add some amazing feature'`) |
| 373 | 4. Push to the branch (`git push origin feature/amazing-feature`) |
| 374 | 5. Open a Pull Request |
| 375 | |
| 376 | ## License |
| 377 | |
| 378 | This project is licensed under the ISC License. |