← BACK

chat-graphql

TypeScript ⭐ 0

🚀 Professional Chat System

A scalable real-time chat system built with NestJS, GraphQL, PostgreSQL, and Prisma. This project leverages modern technologies to provide a robust backend for a chat application, focusing on real-time communication and secure authentication.


✨ Features

  • 🔐 JWT Authentication: Secure user authentication using JSON Web Tokens and bcrypt for password hashing.
  • 💬 Real-time Messaging: Implements real-time communication through GraphQL subscriptions.
  • 🗄️ PostgreSQL Database: Utilizes PostgreSQL as the primary database, managed with the Prisma ORM for efficient data handling.
  • 📐 Clean Architecture: Adheres to SOLID principles and clean architecture for maintainable and scalable code.
  • 🛡️ Security Measures: Includes rate limiting and other security best practices.
  • 🧪 Testing Suite: Comprehensive unit and integration tests to ensure reliability.
  • 📊 Health Checks & Metrics: Basic health check endpoints for monitoring.
  • 🐳 Dockerization: Provided Dockerfile for containerized deployment.

📚 Table of Contents


🛠️ Tech Stack

  • Backend: NestJS, TypeScript
  • Database: PostgreSQL, Prisma ORM
  • APIs: GraphQL (Apollo Server)
  • Authentication: JWT, Passport.js
  • Real-time: WebSocket Subscriptions
  • Testing: Jest
  • Containerization: Docker

🚀 Installation

Prerequisites

  • Node.js (v18 or higher recommended)
  • npm or yarn
  • PostgreSQL database

Steps

  1. Clone the repository:

    git clone https://github.com/phambaophuc/chat-graphql.git
    cd chat-graphql
    
  2. Install dependencies:

    npm install
    
  3. Configure Environment Variables:
    Copy the example environment file and fill in your database connection details and JWT secret:

    cp .env.example .env
    # Edit .env with your database URL and JWT_SECRET
    
  4. Set up the Database:

    • Ensure your PostgreSQL server is running.
    • Create a database for the application (e.g., chat_db).
    • Configure the DATABASE_URL in your .env file accordingly.
  5. Generate Prisma Client:
    This command generates the Prisma Client based on your schema.

    npm run db:generate
    
  6. Run Database Migrations:
    Applies the database schema to your PostgreSQL instance.

    npm run db:migrate
    
  7. Seed the Database (Optional but recommended for development):
    Populates the database with mock data using faker.

    npm run db:seed
    

▶️ Usage

Local Development Server

Start the NestJS application in development mode with hot-reloading:

npm run start:dev

The server will typically run on http://localhost:3000.

GraphQL Playground

Once the server is running, you can access the GraphQL Playground at http://localhost:3000/graphql. This interactive interface allows you to explore the schema, test queries, and mutations.

Example GraphQL Operations

1. User Registration:

mutation {
  register(input: {
    username: "john_doe"
    password: "password123"
  }) {
    userId
    token
  }
}

2. User Login:

mutation {
  login(input: {
    username: "john_doe"
    password: "password123"
  }) {
    userId
    token
  }
}

*Note: For real-time messaging and subscriptions, you would typically use a GraphQL client that supports subscriptions (e.g., Apollo Client) and connect to the WebSocket endpoint.

📚 API Reference

This project exposes a GraphQL API. The schema is defined in schema.gql and generated dynamically.

Key areas covered by the API include:

  • Authentication: User registration and login mutations.
  • Rooms: Operations for creating, joining, and managing chat rooms.
  • Messages: Real-time message broadcasting and retrieval via subscriptions and queries.

Refer to the GraphQL Playground (/graphql) for the most up-to-date schema and available operations.

📄 License

This project is licensed under the MIT License - see the LICENSE file for details.

🏡 Footer

This project is the chat-graphql repository.

Built with ❤️ by phambaophuc