Skip to content

Harsh5840/ai-chat-app---Copy

Repository files navigation

WebSocket AI Chat App

A real-time chat application with specialized AI assistants built using WebSocket, Express.js, Next.js, and Groq API.

Features

  • Real-time Chat: WebSocket-based messaging for instant communication
  • Specialized AI Assistants: 7 different AI experts (DevGPT, ChefGPT, DocGPT, LegalGPT, FitGPT, FinanceGPT, StoryGPT)
  • Room-based Chat: Each AI assistant has its dedicated chat room
  • User Authentication: JWT-based authentication with secure password hashing
  • Responsive UI: Modern, dark-themed interface with Tailwind CSS
  • Docker Support: Containerized microservices architecture

Architecture

  • HTTP Service (Port 3001): Express.js REST API with Prisma ORM and PostgreSQL
  • WebSocket Service (Port 7070): Real-time chat handling
  • Frontend (Port 3000): Next.js React application

Prerequisites

  • Docker and Docker Compose
  • Node.js 18+ (for local development)
  • PostgreSQL database
  • Groq API key

Quick Start with Docker

  1. Clone the repository

    git clone <repository-url>
    cd websocket-ai-chat-app
  2. Set up environment variables

    cp .env.example .env

    Edit .env and add your configuration:

    DATABASE_URL="postgresql://username:password@localhost:5432/database_name"
    JSON_WEB_TOKEN_SECRET="your-super-secret-jwt-key-here"
    GROQ_API_KEY="your-groq-api-key-here"
  3. Start the application

    docker-compose up --build
  4. Access the application

Local Development Setup

HTTP Service

cd http
npm install
npx prisma generate
npx prisma db push
npx prisma db seed
npm run dev

WebSocket Service

cd websocket
npm install
npm start

Frontend

cd frontend
npm install
npm run dev

Environment Variables

Variable Description Default
DATABASE_URL PostgreSQL connection string Required
JSON_WEB_TOKEN_SECRET JWT signing secret "secretttt"
GROQ_API_KEY Groq API key for AI responses Required
HTTP_SERVICE_URL HTTP service URL for WebSocket http://localhost:3001
FRONTEND_URL Frontend URL for CORS http://localhost:3000
PORT HTTP service port 3001

API Endpoints

Authentication

  • POST /api/v1/user/signup - User registration
  • POST /api/v1/user/signin - User login
  • PUT /api/v1/user - Update user profile
  • GET /api/v1/user/:id - Get user by ID

Chat

  • POST /api/v1/chat - Send chat message
  • GET /api/v1/chat/history/:roomName - Get chat history

Rooms

  • GET /api/v1/room/:name - Get room details
  • GET /api/v1/dashboard - Get all available rooms

WebSocket Events

Client to Server

// Join a room
{
  "type": "join",
  "roomName": "room-DevGPT"
}

// Send chat message
{
  "type": "chat",
  "content": "Hello, how can you help me?",
  "roomName": "room-DevGPT",
  "userId": 123
}

Server to Client

// Chat message received
{
  "type": "chat",
  "userMessage": { ... },
  "aiMessage": { ... }
}

// Typing indicator
{
  "type": "typing",
  "roomName": "room-DevGPT",
  "username": "john_doe"
}

// Error message
{
  "type": "error",
  "message": "Error description"
}

Available AI Assistants

  1. DevGPT - Programming and development assistance
  2. ChefGPT - Culinary guidance and recipes
  3. DocGPT - Medical consultation and health advice
  4. LegalGPT - Legal guidance and explanations
  5. FitGPT - Fitness coaching and workout plans
  6. FinanceGPT - Financial advice and budgeting
  7. StoryGPT - Creative writing and storytelling

Security Features

  • JWT-based authentication
  • Password hashing with bcrypt
  • Input validation and sanitization
  • Rate limiting protection
  • CORS configuration
  • SQL injection prevention with Prisma

Error Handling

The application includes comprehensive error handling:

  • Input validation
  • Database error handling
  • API timeout handling
  • WebSocket connection management
  • Graceful error responses

Production Deployment

For production deployment:

  1. Use strong JWT secrets
  2. Configure proper CORS origins
  3. Set up SSL/TLS certificates
  4. Use environment-specific database URLs
  5. Configure proper logging
  6. Set up monitoring and health checks

Contributing

  1. Fork the repository
  2. Create a feature branch
  3. Make your changes
  4. Add tests if applicable
  5. Submit a pull request

License

This project is licensed under the MIT License.

About

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors