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.
- 🔒 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)
.
├── 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.)
└── ...
- 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
- Store message content in MongoDB, metadata in SQL
- Broadcast to subject user via WebSocket
- Working on it
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
- Go (chi, goroutines, channels)
- WebSocket
- MongoDB
- PostgreSQL
- Redis
- Docker
This project is licensed under the MIT LICENSE