Skip to content

aman-zulfiqar/GoChatly

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

5 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

GoChat - Complete Real-time Chat Application

A full-stack real-time chat application built with Go backend and Next.js frontend.

πŸš€ Features

Backend (Go)

  • Real-time WebSocket messaging
  • MongoDB for message persistence
  • Redis for job queuing
  • Email notifications via MailHog
  • Room-based chat system
  • User presence management
  • CORS-enabled API
  • Docker containerization

Frontend (Next.js)

  • Modern React with TypeScript
  • Real-time messaging interface
  • Connection testing & diagnostics
  • Responsive design with Tailwind CSS
  • shadcn/ui components
  • User presence indicators

πŸ“ Project Structure

``` gochat-server/ β”œβ”€β”€ cmd/ β”‚ └── main.go # Application entry point β”œβ”€β”€ internal/ β”‚ β”œβ”€β”€ config/ # Configuration management β”‚ β”œβ”€β”€ database/ # MongoDB connection β”‚ β”œβ”€β”€ handlers/ # HTTP & WebSocket handlers β”‚ β”œβ”€β”€ hub/ # WebSocket hub & client management β”‚ β”œβ”€β”€ models/ # Data models β”‚ β”œβ”€β”€ queue/ # Job queue management β”‚ └── services/ # Business logic services β”œβ”€β”€ frontend/ # Next.js frontend application β”‚ β”œβ”€β”€ app/ # Next.js app directory β”‚ β”œβ”€β”€ components/ # React components β”‚ └── lib/ # Utility functions β”œβ”€β”€ docker-compose.yaml # Docker services configuration β”œβ”€β”€ dockerfile # Go application container β”œβ”€β”€ go.mod # Go dependencies └── README.md # This file ```

πŸ› οΈ Setup & Installation

Prerequisites

  • Go 1.23+
  • Node.js 18+
  • Docker & Docker Compose

1. Clone the Repository

```bash git clone cd gochat-server ```

2. Start Backend Services

```bash

Start all services (MongoDB, Redis, MailHog, Go server)

docker-compose up -d

Check services are running

docker-compose ps ```

3. Setup Frontend

```bash cd frontend npm install npm run dev ```

4. Access the Application

πŸ”§ Configuration

Environment Variables

Create a .env file in the root directory:

```env

Server Configuration

PORT=8080

Database Configuration

MONGO_URI=mongodb://admin:password123@localhost:27017/chatdb?authSource=admin DATABASE_NAME=chatdb

Redis Configuration

REDIS_ADDR=localhost:6379

SMTP Configuration (MailHog)

SMTP_HOST=localhost SMTP_PORT=1025 SMTP_USER= SMTP_PASS=

CORS Configuration

CORS_ORIGINS=http://localhost:3000,http://127.0.0.1:3000 ```

πŸš€ Usage

1. Test Connection

  1. Visit http://localhost:3000/test
  2. Click "Run All Tests" to verify backend connectivity
  3. Ensure all tests pass before proceeding

2. Start Chatting

  1. Go to http://localhost:3000
  2. Enter your username, user ID, and room ID
  3. Click "Join Room" to start chatting
  4. Open multiple browser tabs to test real-time messaging

3. Monitor Services

πŸ—οΈ Architecture

Backend Architecture

``` β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β” β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β” β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β” β”‚ WebSocket β”‚ β”‚ REST API β”‚ β”‚ Job Queue β”‚ β”‚ Handlers β”‚ β”‚ Handlers β”‚ β”‚ Workers β”‚ β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜ β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜ β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜ β”‚ β”‚ β”‚ β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜ β”‚ β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β” β”‚ Hub (Message Broker) β”‚ β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜ β”‚ β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β” β”‚ β”‚ β”‚ β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β” β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β” β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β” β”‚ MongoDB β”‚ β”‚ Redis β”‚ β”‚ SMTP β”‚ β”‚ (Messages) β”‚ β”‚ (Queues) β”‚ β”‚ (MailHog) β”‚ β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜ β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜ β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜ ```

Frontend Architecture

``` β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β” β”‚ Next.js Frontend β”‚ β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€ β”‚ HomePage β”‚ ChatRoom β”‚ ConnectionTest β”‚ UI Components β”‚ β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€ β”‚ WebSocket Client & REST API Client β”‚ β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜ β”‚ β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β” β”‚ Go Backend API β”‚ β”‚ (localhost:8080) β”‚ β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜ ```

πŸ” API Endpoints

WebSocket

  • ws://localhost:8080/ws/{roomID}/{userID}?username={username}

REST API

  • GET /health - Health check
  • GET /test - Frontend connectivity test
  • GET /rooms/{roomID}/messages?limit={limit} - Get message history
  • GET /rooms/{roomID}/users - Get room users
  • POST /queue-email - Queue email notification

πŸ› Troubleshooting

Common Issues

  1. CORS Errors

    • Ensure CORS middleware is properly configured in cmd/main.go
    • Check that frontend URL is in allowed origins
  2. WebSocket Connection Failed

    • Verify Go server is running on port 8080
    • Check WebSocket upgrader configuration
    • Ensure no firewall blocking connections
  3. Database Connection Issues

    • Verify MongoDB is running: docker-compose ps
    • Check MongoDB credentials in environment variables
    • Ensure database is accessible on port 27017
  4. Frontend Build Errors

    • Run npm install in frontend directory
    • Check Node.js version (18+ required)
    • Verify all dependencies are installed

Debug Commands

```bash

Check Docker services

docker-compose ps

View Go server logs

docker-compose logs gochat-server

Check MongoDB connection

docker-compose exec mongodb mongosh

Check Redis connection

docker-compose exec redis redis-cli ping

Test backend health

curl http://localhost:8080/health ```

πŸš€ Deployment

Production Deployment

  1. Update environment variables for production
  2. Build frontend: cd frontend && npm run build
  3. Build Go binary: go build -o main cmd/main.go
  4. Deploy using Docker or your preferred platform

Docker Production Build

```bash

Build production images

docker-compose -f docker-compose.prod.yml build

Deploy to production

docker-compose -f docker-compose.prod.yml up -d ```

🀝 Contributing

  1. Fork the repository
  2. Create a feature branch: git checkout -b feature-name
  3. Make your changes
  4. Test thoroughly using the connection test
  5. Submit a pull request

πŸ“ License

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

πŸ™ Acknowledgments

  • Echo Framework for the Go web framework
  • Gorilla WebSocket for WebSocket implementation
  • Next.js for the React framework
  • shadcn/ui for the UI components
  • MongoDB for data persistence
  • Redis for job queuing
  • MailHog for email testing