Skip to content

a-3isa/chatty

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

12 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

Chatty - Real-Time Chat Application

A modern real-time chat application built with NestJS backend and simple HTML frontend, featuring user authentication, WebSocket-based messaging, and persistent message storage.

πŸš€ Features

  • Real-time Messaging: Instant message delivery using WebSockets with Socket.IO
  • User Authentication: Secure JWT-based authentication with Passport
  • User Management: Registration and login system
  • Persistent Messages: All messages stored in MongoDB database with Prisma ORM
  • Simple UI: Clean, responsive HTML interface
  • Cross-platform: Works on desktop and mobile browsers

πŸ› οΈ Technology Stack

Backend

  • Framework: NestJS (Node.js)
  • Language: TypeScript
  • Database: MongoDB
  • ORM: Prisma
  • Authentication: JWT + Passport
  • Real-time: Socket.IO
  • Validation: class-validator + class-transformer
  • Password Hashing: bcrypt

Frontend

  • HTML5: Semantic markup
  • CSS3: Custom styling
  • JavaScript: ES6+ features
  • WebSockets: Socket.IO client

Development Tools

  • Linting: ESLint
  • Code Formatting: Prettier
  • Testing: Jest
  • Build Tool: NestJS CLI

πŸ“‹ Prerequisites

Before running this application, make sure you have the following installed:

  • Node.js (v16 or higher)
  • npm or yarn
  • MongoDB database
  • Git

πŸ”§ Installation

  1. Clone the repository

    git clone https://github.com/a-3isa/chatty
    cd chatty
  2. Install dependencies

    npm install
  3. Environment Setup Create a .env file in the root directory:

    DATABASE_URL="DATABASE_URL"
    JWT_SECRET="your-super-secret-jwt-key"
    PORT=3000
  4. Database Setup

    # Generate Prisma client
    npx prisma generate
    
    # Run database migrations
    npx prisma migrate dev
    
    # (Optional) Seed the database
    npx prisma db seed

πŸš€ Running the Application

Development Mode

# Start the backend server
npm run start:dev

# The server will be running at http://localhost:3000

Production Mode

# Build the application
npm run build

# Start the production server
npm run start:prod

Frontend Access

Open your browser and navigate to:

  • Login Page: http://localhost:3000/client/login.html
  • Register Page: http://localhost:3000/client/register.html
  • Chat Interface: http://localhost:3000/client/chat.html

πŸ“š API Documentation

Authentication Endpoints

Register User

POST /auth/register
Content-Type: application/json

{
  "username": "johndoe",
  "password": "securepassword"
}

Login User

POST /auth/login
Content-Type: application/json

{
  "username": "johndoe",
  "password": "securepassword"
}

Response:

{
  "accessToken": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9...",
  "userId": 1
}

Chat Endpoints

Get Messages (Protected)

GET /chat/messages?userId=2
Authorization: Bearer <access_token>

Response:

[
  {
    "id": 1,
    "content": "Hello!",
    "senderId": 1,
    "receiverId": 2,
    "createdAt": "2024-01-01T10:00:00.000Z",
    "sender": {
      "username": "johndoe"
    }
  }
]

Get Users (Protected)

GET /users
Authorization: Bearer <access_token>

WebSocket Events

Connection

const socket = io('http://localhost:3000', {
  auth: { token: accessToken },
  extraHeaders: { authorization: 'Bearer ' + accessToken },
});

Join Room

socket.emit('join', userId);

Send Message

socket.emit('sendMessage', {
  senderId: 1,
  receiverId: 2,
  content: 'Hello, World!',
});

Receive Message

socket.on('receiveMessage', (message) => {
  console.log('New message:', message);
});

πŸ—‚οΈ Project Structure

chatty/
β”œβ”€β”€ src/
β”‚   β”œβ”€β”€ app.controller.ts      # Main application controller
β”‚   β”œβ”€β”€ app.module.ts          # Root application module
β”‚   β”œβ”€β”€ app.service.ts         # Main application service
β”‚   β”œβ”€β”€ main.ts                # Application entry point
β”‚   β”œβ”€β”€ auth/                  # Authentication module
β”‚   β”‚   β”œβ”€β”€ auth.controller.ts
β”‚   β”‚   β”œβ”€β”€ auth.module.ts
β”‚   β”‚   β”œβ”€β”€ auth.service.ts
β”‚   β”‚   β”œβ”€β”€ jwt.strategy.ts
β”‚   β”‚   └── dto/
β”‚   β”œβ”€β”€ chat/                  # Chat module
β”‚   β”‚   β”œβ”€β”€ chat.controller.ts
β”‚   β”‚   β”œβ”€β”€ chat.gateway.ts    # WebSocket gateway
β”‚   β”‚   β”œβ”€β”€ chat.module.ts
β”‚   β”‚   └── chat.service.ts
β”‚   β”œβ”€β”€ messages/              # Messages module
β”‚   β”œβ”€β”€ users/                 # Users module
β”‚   └── prisma.service.ts      # Prisma database service
β”œβ”€β”€ client/                    # Frontend files
β”‚   β”œβ”€β”€ login.html
β”‚   β”œβ”€β”€ register.html
β”‚   └── chat.html
β”œβ”€β”€ prisma/
β”‚   β”œβ”€β”€ schema.prisma          # Database schema
β”‚   └── migrations/            # Database migrations
β”œβ”€β”€ test/                      # Test files
β”œβ”€β”€ package.json
β”œβ”€β”€ tsconfig.json
└── README.md

πŸ§ͺ Testing

# Run unit tests
npm run test

# Run tests in watch mode
npm run test:watch

# Run test coverage
npm run test:cov

# Run e2e tests
npm run test:e2e

πŸ“œ Scripts

  • npm run build - Build the application
  • npm run format - Format code with Prettier
  • npm run start - Start production server
  • npm run start:dev - Start development server with hot reload
  • npm run start:debug - Start debug mode
  • npm run start:prod - Start production server
  • npm run lint - Run ESLint
  • npm run test - Run Jest tests

πŸ”’ Security Features

For comprehensive security documentation, see SECURITY.md

Core Security Features

  • JWT Authentication: Secure token-based authentication with refresh tokens
  • Password Security: bcrypt hashing with strength validation
  • Account Lockout: Automatic lockout after failed login attempts
  • Input Sanitization: Global middleware preventing XSS and injection attacks
  • CSRF Protection: JWT-based CSRF protection for HTML clients
  • Security Headers: Helmet middleware with comprehensive HTTP security headers
  • CORS Configuration: Environment-specific CORS policies
  • Rate Limiting: Global throttling to prevent abuse
  • Password Reset: Secure token-based password reset flow
  • Input Validation: class-validator with comprehensive DTO validation
  • WebSocket Security: Authentication middleware for real-time connections

🀝 Contributing

  1. Fork the repository
  2. Create a feature branch (git checkout -b feature/amazing-feature)
  3. Commit your changes (git commit -m 'Add some amazing feature')
  4. Push to the branch (git push origin feature/amazing-feature)
  5. Open a Pull Request

πŸ“„ License

This project is licensed under the UNLICENSED License.

πŸ“ž Support

If you have any questions or need help, please open an issue in the repository.

πŸ”„ Future Enhancements

  • Add user profile pictures
  • Implement group chat functionality
  • Add message encryption
  • Mobile app development
  • File sharing capabilities
  • Message reactions and replies
  • Online/offline status indicators

About

No description, website, or topics provided.

Resources

Security policy

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published