ChatSphere is a TypeScript microservices backend for chat applications. It uses a pnpm monorepo, an API gateway, service-owned databases, and RabbitMQ events to keep data in sync across authentication, user, and conversation domains.
services/gateway-servicehandles client-facing HTTP trafficservices/auth-servicemanages registration, login, refresh, and token revocationservices/user-servicestores user profiles and search dataservices/chat-servicemanages conversations and messagespackages/commoncontains shared validation, auth helpers, env parsing, events, and error utilities
Client
|
v
Gateway Service
|- /auth ----------> Auth Service ------> MySQL
|- /users ---------> User Service ------> PostgreSQL
`- /conversations -> Chat Service ------> MongoDB
|
`-> Redis
RabbitMQ
Auth Service ---- publishes auth.user.registered
User Service ---- consumes auth.user.registered
User Service ---- publishes user.created
Chat Service ---- consumes user.created
- Node.js
- TypeScript
- Express 5
pnpmworkspaces- Zod
- Sequelize
- MySQL
- PostgreSQL
- MongoDB
- Redis
- RabbitMQ
- JWT
- Docker Compose
.
+-- packages/
| `-- common/
+-- services/
| +-- gateway-service/
| +-- auth-service/
| +-- user-service/
| `-- chat-service/
+-- docker-compose.yml
+-- request.http
+-- pnpm-workspace.yaml
`-- readme.md
The gateway is the public entry point. It exposes:
POST /auth/registerPOST /auth/loginPOST /auth/refreshPOST /auth/revokeGET /usersGET /users/:idGET /users/searchPOST /usersPOST /conversationsGET /conversationsGET /conversations/:conversationIdPOST /conversations/:conversationId/messagesGET /conversations/:conversationId/messagesGET /health
Protected routes require:
Authorization: Bearer <access_token>The gateway also forwards internal requests with:
x-internal-token: <shared_internal_token>Default port: 4000
Responsibilities:
- register users
- hash passwords with
bcrypt - issue access and refresh tokens
- rotate refresh tokens
- revoke user refresh tokens
- publish
auth.user.registeredevents - expose
GET /health
Default port: 4003
Database: MySQL
Responsibilities:
- persist user profiles
- serve lookup and search operations
- consume
auth.user.registered - publish
user.created - expose
GET /health
Default port: 4001
Database: PostgreSQL
Responsibilities:
- create and list conversations
- create and list messages
- enforce participant access
- cache conversation data in Redis
- consume
user.created - expose
GET /health
Default port in Compose: 4002
Datastores:
- MongoDB
- Redis
- A client registers through
POST /auth/register. - The gateway forwards the request to the auth service.
- The auth service stores credentials and publishes
auth.user.registered. - The user service consumes that event and syncs the user into PostgreSQL.
- The user service publishes
user.created. - The chat service consumes
user.createdand upserts user data needed for chat operations.
- Node.js 20+
pnpm- Docker / Docker Compose
pnpm installThis repo currently uses a root .env for Docker Compose and service configuration. At minimum, make sure these values are present:
NODE_ENV=production
GATEWAY_PORT=4000
AUTH_SERVICE_PORT=4003
USER_SERVICE_PORT=4001
CHAT_SERVICE_PORT=4002
JWT_SECRET=replace-with-a-strong-secret
JWT_REFRESH_SECRET=replace-with-a-second-strong-secret
INTERNAL_API_TOKEN=replace-with-a-shared-internal-token
JWT_EXPIRES_IN=1d
JWT_REFRESH_EXPIRES_IN=7d
RABBITMQ_USER=guest
RABBITMQ_PASSWORD=guest
RABBITMQ_PORT=5672
RABBITMQ_MANAGEMENT_PORT=15672
REDIS_URL=redis://redis:6379
MONGO_URL=mongodb://root:password@mongo:27017/chatapp_chat_service?authSource=admin
USER_DB_URL=postgres://chatapp_user:chatapp_password@user-db:5432/chatapp_user_service
AUTH_DB_URL=mysql://chatapp_auth_user:chatapp_auth_password@auth-db:3306/chatapp_auth_serviceIf you use real secrets locally or in deployment, rotate them before sharing or committing the file.
Build and start the full stack:
docker compose up --buildRun in the background:
docker compose up -d --buildThe Compose setup includes:
gateway-serviceauth-serviceuser-servicechat-serviceauth-db(MySQL)user-db(PostgreSQL)mongoredisrabbitmq
If you want to run the Node services directly:
pnpm devSingle-service examples:
pnpm --filter gateway-service dev
pnpm --filter @chat_app/auth-service dev
pnpm --filter @chat_app/user-service dev
pnpm --filter chat-service devpnpm buildpnpm --filter gateway-service start
pnpm --filter @chat_app/auth-service start
pnpm --filter @chat_app/user-service start
pnpm --filter chat-service startFrom the repo root:
pnpm devpnpm buildpnpm lintpnpm formatpnpm test
At the moment, service test scripts return No tests yet.
The repo includes request.http with starter requests for:
- user registration
- user login
Base gateway URL:
http://localhost:4000
Each service already has a Dockerfile, and docker-compose.yml is set up to run the full application stack with health checks and container networking.
If you are deploying with Dokploy or another Compose-based platform:
- deploy the repository as a Compose project
- provide the same environment variables used locally
- expose the gateway service port publicly
- replace all development secrets with production-grade values
- enable TLS at the platform or proxy layer
- automated tests are not implemented yet
- the root
.envcurrently carries real runtime configuration, so secret handling should be tightened before wider sharing - some service source files still contain small inconsistencies in health-check labels and comments, even though the overall runtime flow is clear
ISC