This is a real-time chat API built with NestJS, TypeORM, and PostgreSQL (running in Docker). It provides:
- User registration & authentication (JWT-based)
- Chat messaging between users
- Database persistence with PostgreSQL
- Dockerized setup for easy local development
- Ready to scale for real-time features or microservices integration
- Backend: NestJS, TypeScript, TypeORM
- Database: PostgreSQL (Dockerized)
- Authentication: JWT
- Dev Tools: Docker Compose, npm
- User management (register, login)
- CRUD operations for chat messages
- Modular and clean architecture
- Easily extensible for future real-time updates (WebSockets)
src ├── auth │ ├── dto │ │ └── login.dto.ts │ ├── auth.controller.ts │ ├── auth.module.ts │ ├── auth.service.ts │ ├── jwt-auth.guard.ts │ └── jwt.strategy.ts ├── chat │ ├── dto │ │ ├── create-room.dto.ts │ │ └── send-message.dto.ts │ ├── entities │ │ ├── message.entity.ts │ │ └── room.entity.ts │ ├── chat.controller.ts │ ├── chat.gateway.ts │ ├── chat.module.ts │ └── chat.service.ts ├── users │ ├── dto │ │ └── create-user.dto.ts │ ├── entities │ │ └── user.entity.ts │ ├── users.controller.ts │ ├── users.module.ts │ └── users.service.ts ├── app.controller.spec.ts ├── app.controller.ts ├── app.module.ts ├── app.service.ts └── main.ts 🛠 Setup & Run
- Clone the repository
git clone https://github.com/Vintanina/chat-api.git
cd chat-api
- Create .env file
DATABASE_URL=postgres://chat:chat@localhost:6543/chat_db PORT=3000 JWT_SECRET=your_jwt_secret
Adjust DATABASE_URL if Postgres is running in Docker with a different port or credentials. - Start PostgreSQL with Docker (Optionnal)
docker compose up -d
The container exposes Postgres on port 6543. Check logs if needed: docker compose logs -f db
- Install dependencies
npm install
- Run the NestJS application
npm run start:dev
The API runs on http://localhost:3000 - Test the API
Example: register a user via curl:
curl -X POST http://localhost:3000/auth/register
-H "Content-Type: application/json"
-d '{"username":"alice","email":"alice@mail.com","password":"secret123"}'
-
Notes
Make sure TypeOrmModule is properly configured with your entities and autoLoadEntities: true.
If the database is slow to start, NestJS retries automatically (configured via retryAttempts and retryDelay).
Use docker compose down -v to clean volumes if you want a fresh database.