Go tilida yozilgan, minimal dependency'lar bilan real-time chat ilovasi. Educational project - Go concurrency pattern'larini o'rganish uchun.
- ✅ WebSocket Real-time Chat - bir vaqtda xabar almashish
- ✅ Hub-and-Spoke Pattern - markazlashtirilgan connection management
- ✅ Goroutines & Channels - concurrent programming
- ✅ Thread-safe Storage - Mutex bilan xavfsiz xotira
- ✅ Message History - yangi user eski xabarlarni ko'radi
- ✅ Multiple Rooms - turli xonalar, alohida history
- ✅ Join/Leave Messages - kim keldi/ketdi xabarlari
- ✅ Configuration Management - env variable'lar bilan sozlash
- ✅ Graceful Shutdown - context bilan to'g'ri to'xtash
- ✅ Input Validation - username, room, message validation
- Language: Go 1.25+
- WebSocket: golang.org/x/net/websocket
- Architecture: Hub-and-Spoke pattern
- Storage: In-memory (map + mutex)
- Config: Environment variables
chatx/
├── cmd/server/main.go # Entry point
├── internal/
│ ├── config/config.go # Configuration management
│ ├── handler/handler.go # HTTP -> WebSocket upgrade
│ ├── message/message.go # Message struct
│ ├── store/store.go # In-memory storage
│ ├── validator/validator.go # Input validation
│ └── websocket/
│ ├── hub.go # Connection manager
│ └── client.go # Per-connection handler
├── .env # Configuration file
└── test-client.html # Browser test client
- Clone va dependencies:
git clone <repo>
cd chatx
go mod download- Serverni ishga tushirish:
go run cmd/server/main.go- Browser'da test qilish:
open test-client.html.env fayl yarating yoki environment variable'lar bilan sozlang:
# ChatX Application Configuration
CHATX_PORT=:8080
CHATX_STORE_MAX=100
CHATX_HISTORY_COUNT=50
CHATX_MAX_MESSAGE_SIZE=512
CHATX_WRITE_WAIT=10s
CHATX_PONG_WAIT=60s
CHATX_PING_PERIOD=54s
CHATX_LOG_LEVEL=infoEnv bilan ishga tushirish:
CHATX_PORT=:8081 CHATX_STORE_MAX=200 go run cmd/server/main.go- Concurrency: goroutines, channels, select statement
- Patterns: Hub-and-Spoke, Constructor, Dependency Injection
- Thread-safety: sync.RWMutex, Lock/Unlock
- Network: WebSocket, HTTP handlers
- Architecture: Package separation, import cycles hal qilish
- Configuration: Environment variables, default values
- Context: Graceful shutdown, cancellation
- Validation: Input validation, error handling
- Data structures: Maps, slices, channels
Connect:
ws://localhost:8080/ws?username=Ali&room=general
Message format (JSON):
{
"type": "chat",
"content": "Salom hammaga!",
"username": "Ali",
"room_id": "general",
"timestamp": 1234567890
}Message types:
chat- Oddiy chat xabarijoin- User xonaga kirdileave- User xonadan chiqdi
Build:
go build ./...Test with multiple rooms:
- Browser'da bir nechta tab oching
- Har xil username va room bilan ulaning
- Xabar yuboring va room filter'ni tekshiring
Graceful shutdown:
Ctrl+C # yoki kill -TERM <pid>- Unit Tests - validator, store, hub testlari
- Rate Limiting - spam prevention
- Metrics - monitoring va statistics
- User List - kimlar online ko'rsatish
- Typing Indicator - kim yozyapti
- Private Messages - shaxsiy xabarlar
- Authentication - JWT token
- Database - PostgreSQL persistence
- Docker - containerization
- Deploy - production'ga chiqarish
Educational project - free to use and learn from.