Chatterbox is a real-time chat application built with NestJS, designed for scalability and maintainability. It features WebSocket communication, JWT-based authentication, and a PostgreSQL database backend.
- Real-time Messaging: Utilizes WebSockets (Socket.IO) for instant message delivery.
- Scalable Architecture: Leverages Redis with
@socket.io/redis-adapterfor multi-instance WebSocket scaling. - Authentication & Authorization: Secure JWT-based authentication (access and refresh tokens) using RSA keys.
- Database: PostgreSQL managed with TypeORM, including support for migrations.
- API Documentation: Swagger (OpenAPI) integration for easy API exploration and testing.
- Configuration Management: Flexible configuration using
@nestjs/configand.envfiles. - Logging: Comprehensive logging with Winston.
- Input Validation: Uses
class-validatorandclass-transformerfor robust request validation. - Structured Codebase: Follows a modular approach with clear separation of concerns (application, infrastructure, presentation layers).
Before you begin, ensure you have the following installed:
- Node.js (v18.x or later recommended)
- npm (v9.x or later) or yarn
- PostgreSQL (v13.x or later)
- Redis (v6.x or later)
- OpenSSL (for generating RSA keys)
- Docker (Optional, for running PostgreSQL and Redis in containers)
-
Clone the repository:
git clone <your-repository-url> cd nestjs-chat
-
Install dependencies:
npm install # or # yarn install
-
Set up environment variables: Copy the example environment file and update it with your local configuration:
cp .env.example .env
Edit
.envwith your specific settings (database credentials, ports, key paths, etc.). See the Environment Variables section for details. -
Generate RSA Keys for Authentication: The application uses RSA keys for signing and verifying JWTs. Use the provided Makefile to generate them:
make generate_keys
This will create a
keys/directory withaccess_private.key,access_public.key,refresh_private.key, andrefresh_public.key. Ensure the paths in your.envfile (AUTH_ACCESS_PUBLIC_KEY_PATH,AUTH_ACCESS_PRIVATE_KEY_PATH, etc.) point to these generated keys (e.g.,keys/access_private.key). -
Set up the Database: Ensure your PostgreSQL server is running and accessible with the credentials provided in your
.envfile. Run database migrations to create the necessary tables:npm run migration:run
-
Development Mode (with hot-reloading):
npm run start:dev
The application will typically be available at
http://localhost:<PORT>(as defined in your.env). -
Production Mode:
npm run build npm run start:prod
-
Debugging Mode:
npm run start:debug
-
Run all tests:
npm test -
Run tests in watch mode:
npm run test:watch
-
Run tests with coverage report:
npm run test:cov
-
Run End-to-End (E2E) tests:
npm run test:e2e
Swagger API documentation is available once the application is running. Navigate to:
http://localhost:<PORT>/swagger
(Replace <PORT> with the HTTP port specified in your .env file).
The following environment variables need to be configured in your .env file:
| Variable | Description |
|---|---|
POSTGRES_HOST |
PostgreSQL server host. |
POSTGRES_PORT |
PostgreSQL server port. |
POSTGRES_USERNAME |
PostgreSQL username. |
POSTGRES_PASSWORD |
PostgreSQL password. |
POSTGRES_DATABASE |
PostgreSQL database name. |
POSTGRES_LOG |
Enable/disable TypeORM logging (true/false). |
POSTGRES_SLOW_QUERY_LIMIT |
Slow query limit in milliseconds for logging. |
LOG_USE_FILE |
Whether to log to a file (true/false). |
LOG_FILE |
Path to the log file (if LOG_USE_FILE is true). |
LOG_LEVEL |
Logging level (e.g., debug, info, warn, error). |
PORT |
Port number for the HTTP server. |
REDIS_HOST |
Redis server host. |
REDIS_PORT |
Redis server port. |
REDIS_USERNAME |
Redis username (if applicable). |
REDIS_PASSWORD |
Redis password (if applicable). |
WEBSOCKET_PORT |
Port number for the WebSocket server (Note: NestJS integrates WS with HTTP port by default unless explicitly separated). |
AUTH_ACCESS_PUBLIC_KEY_PATH |
Path to the access token public RSA key. |
AUTH_ACCESS_PRIVATE_KEY_PATH |
Path to the access token private RSA key. |
AUTH_REFRESH_PUBLIC_KEY_PATH |
Path to the refresh token public RSA key. |
AUTH_REFRESH_PRIVATE_KEY_PATH |
Path to the refresh token private RSA key. |
The Makefile provides convenient commands for managing RSA keys used for JWT authentication:
make generate_keys: Generates new RSA private and public key pairs for both access and refresh tokens. It will create them in thekeys/directory. If keys already exist, it will prompt an error; usemake delete_keysfirst.make delete_keys: Deletes thekeys/directory and all its contents.make help: Displays available Makefile commands.
A brief overview of the main directories in src/:
src/main.ts: Application entry point, initializes NestJS app, Swagger, WebSocket adapter, etc.src/app.module.ts: Root module of the application.src/application/: Contains core business logic, services, and use cases for different domains (e.g.,auth,chat,user).src/common/: Shared utilities, decorators, enums, constants, and base classes used across the application.src/infrastructure/: Handles external concerns like database interactions (TypeORM entities, repositories), third-party service integrations (Redis, logging), and WebSocket adapters.src/presentation/: Manages how the application interacts with the outside world. Includes HTTP controllers, WebSocket gateways, Data Transfer Objects (DTOs), and guards.
This project is UNLICENSED.