A production chat backend built as microservices with RabbitMQ, Redis and Docker, ready to self-host.
git clone https://www.fiston.dev/code/chatapp.gitUpdate .env.example with new database and service URLs, and remove default values for sensitive secrets. Add README.md for project documentation and setup instructions.
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:
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.
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>"
}
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.