Skip to content

Xenn-00/chat-system

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

15 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Real-Time Chat System

A real-time chat backend built with Go for scalability, concurrency safety, and clean architecture.

Supports private and group chats, with WebSocket-based communication, MongoDB for messages, and PostgreSQL for room metadata.

✨ Features

  • 🔒 Concurrency-safe message handling
  • ⚡ Real-time communication via WebSocket
  • 📬 Private chat flow (lazy room creation) → room would be created when first message sent
  • 👥 Group chat flow → WhatsApp/Discord-like group creation & invites
  • 📨 Async worker for background tasks (priority queue, message persistence)
  • 💾 Hybrid storage → MongoDB for messages, SQL for metadata
  • ⏱️ Queue-based architecture (priority queue + worker)

🏗️ Project Tree Architecture

.
├── cmd/                # Entry point (main.go)
├── config/             # App configuration (config.go)
├── internal/           # Core business logic
│   ├── dtos/           # Data transfer objects (request/response)
│   ├── entity/         # Domain entities (Message, Room, User, dll.)
│   ├── errors/         # Custom error handling
│   ├── handlers/       # HTTP/WebSocket handlers
│   ├── middleware/     # Middleware (auth, logging, dsb.)
│   ├── queue/          # Queue management (priority queue)
│   ├── repo/           # Repository layer (Mongo + SQL access)
│   ├── routers/        # Routing definitions
│   ├── use-case/       # Application/business logic
│   ├── utils/          # Utility functions
│   ├── websocket/      # WebSocket hub & client management
│   └── worker/         # Async workers (process tasks dari queue)
├── migrations/         # Database migrations
├── state/              # State initialization (Mongo, Postgres, Redis, Secret)
│   ├── initMongo.go
│   ├── initPostgres.go
│   ├── initRedis.go
│   ├── initSecret.go
│   └── initState.go
├── docker-compose.yaml # Docker setup (Postgres, Mongo, Redis, dsb.)
└── ...

💡 Private Chat Flow

  1. User sent first message to subject -> system would checking if there any chat room respect to participant is already exists or not
  • If yes -> reuse
  • If not -> create new room + add chat participant
  1. Store message content in MongoDB, metadata in SQL
  2. Broadcast to subject user via WebSocket

💡 Group Chat Flow

  • Working on it

🚀 Getting Started

Prerequisites

  • Go >=1.21
  • PostgreSQL / MySQL (just fit with your preference SQL)
  • MongoDB
  • Redis (for working queue) Run Locally
# clone repo
git clone https://github.com/Xenn-00/chat-system.git
cd chat-system

# install deps
go mod tidy

# run service
go run main.go

🛠️ Tech Stack

  • Go (chi, goroutines, channels)
  • WebSocket
  • MongoDB
  • PostgreSQL
  • Redis
  • Docker

License

This project is licensed under the MIT LICENSE

About

I create a scalable chat system that following best practices in real world industries with go

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published