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.
The application follows a microservices architecture with the following key patterns:
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).
The public-facing entry point for all API requests. Responsibilities include:
Manages user authentication and authorization:
Manages user profile data:
Handles real-time messaging features:
| Category | Technology |
|---|---|
| Runtime | Node.js 22 |
| Language | TypeScript (ESM) |
| Package Manager | pnpm 10 (workspaces) |
| Web Framework | Express 5 |
| Auth Service Database | MySQL 8.0 |
| User Service Database | PostgreSQL 16 |
| Chat Service Database | MongoDB 7 |
| Caching | Redis 7 |
| Message Broker | RabbitMQ 3 |
| ORM | Sequelize 6 |
| Validation | Zod |
| Logging | Pino |
| Containerization | Docker |
git clone https://www.fiston.dev/code/chatapp.git
cd chatapp-yt
pnpm install
Create a .env file in the root directory based on the example:
cp .env.example .env
Update the environment variables as needed.
docker compose up -d rabbitmq redis mongo user-db auth-db
pnpm --filter @chatapp/common build
pnpm dev
Or start individual services:
pnpm --filter gateway-service dev
pnpm --filter @chatapp/auth-service dev
pnpm --filter @chatapp/user-service dev
pnpm --filter chat-service dev
To run the entire stack with Docker:
docker compose up -d
This will start all services and their dependencies with health checks and automatic restarts.
| Variable | Description | Default |
|---|---|---|
NODE_ENV | Environment mode | development |
GATEWAY_PORT | Gateway service port | 4000 |
AUTH_SERVICE_PORT | Auth service port | 4003 |
USER_SERVICE_PORT | User service port | 4001 |
CHAT_SERVICE_PORT | Chat service port | 4002 |
JWT_SECRET | Secret for signing access tokens | - |
JWT_EXPIRES_IN | Access token expiry | 1d |
JWT_REFRESH_SECRET | Secret for signing refresh tokens | - |
JWT_REFRESH_EXPIRES_IN | Refresh token expiry | 30d |
INTERNAL_API_TOKEN | Token for inter-service auth | - |
AUTH_DB_URL | MySQL connection URL | - |
USER_DB_URL | PostgreSQL connection URL | - |
MONGO_URL | MongoDB connection URL | - |
REDIS_URL | Redis connection URL | - |
RABBITMQ_URL | RabbitMQ connection URL | - |
| Service | Port |
|---|---|
| RabbitMQ | 5672 |
| RabbitMQ Management | 15672 |
| Redis | 6379 |
| MongoDB | 27017 |
| PostgreSQL | 5432 |
| MySQL | 3306 |
All authentication endpoints are accessed through the gateway service at http://localhost:4000.
POST /auth/register
Content-Type: application/json
{
"email": "user@example.com",
"displayName": "John Doe",
"password": "SecurePassword123!"
}
Response (201 Created):
{
"accessToken": "eyJhbGciOiJIUzI1NiIs...",
"refreshToken": "eyJhbGciOiJIUzI1NiIs...",
"user": {
"id": "uuid",
"email": "user@example.com",
"displayName": "John Doe",
"createdAt": "2024-01-01T00:00:00.000Z"
}
}
POST /auth/login
Content-Type: application/json
{
"email": "user@example.com",
"password": "SecurePassword123!"
}
Response (200 OK):
{
"accessToken": "eyJhbGciOiJIUzI1NiIs...",
"refreshToken": "eyJhbGciOiJIUzI1NiIs..."
}
POST /auth/refresh
Content-Type: application/json
{
"refreshToken": "eyJhbGciOiJIUzI1NiIs..."
}
POST /auth/revoke
Content-Type: application/json
{
"userId": "uuid"
}
All conversation endpoints require authentication via Bearer token.
POST /conversations
Authorization: Bearer <access_token>
Content-Type: application/json
{
"title": "Project Discussion",
"participantIds": ["user-id-1", "user-id-2"]
}
GET /conversations
Authorization: Bearer <access_token>
GET /conversations/:id
Authorization: Bearer <access_token>
POST /conversations/:id/messages
Authorization: Bearer <access_token>
Content-Type: application/json
{
"body": "Hello, world!"
}
GET /conversations/:id/messages?limit=50&after=<message_id>
Authorization: Bearer <access_token>
Each service exposes a health endpoint:
GET /health
Response:
{
"status": "ok",
"service": "<service-name>"
}
| Command | Description |
|---|---|
pnpm dev | Start all services in development mode |
pnpm build | Build all packages and services |
pnpm lint | Run ESLint on all packages |
pnpm format | Check code formatting with Prettier |
pnpm typecheck | Run TypeScript type checking |
The project uses:
This is a monorepo managed with pnpm workspaces. The root contains docker-compose.yml for infrastructure, along with shared TypeScript and workspace configs.
packages/common: Shared library containing error classes, event types, HTTP middleware, validation utilities, and logging setup. All services depend on this package.
services/gateway-service: The API gateway. Contains controllers that proxy requests to internal services, JWT auth middleware, route definitions, and Zod validation schemas.
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.
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.
services/chat-service: Handles messaging. Uses MongoDB for storing conversations and messages, Redis for caching, and follows a repository pattern for data access.
git checkout -b feature/amazing-feature)git commit -m 'Add some amazing feature')git push origin feature/amazing-feature)This project is licensed under the ISC License.