Realtime Chat

Real-time chat application demonstrating Clean Architecture, WebSocket, and Redis Pub/Sub with Go and SvelteKit.
Features
- Real-time messaging with WebSocket
- User authentication (JWT-based)
- Room management (create, join, leave, delete)
- Message operations (send, edit, delete)
- Typing indicators
- User online/offline status
- Multi-server scaling with Redis Pub/Sub
- Clean Architecture design pattern
Tech Stack
Backend
- Go 1.22+ - Main programming language
- Fiber - Fast HTTP framework
- PostgreSQL - Primary database
- Redis - Pub/Sub for multi-server communication
- JWT - Authentication
- WebSocket - Real-time communication
Frontend
- SvelteKit 2 - Frontend framework
- Svelte 5 - UI framework with runes
- TypeScript - Type safety
- Vite - Build tool
Project Structure
realtime-chat/
├── cmd/
│ └── server/ # Application entry point
├── internal/
│ ├── config/ # Configuration
│ ├── domain/ # Domain entities
│ ├── handler/ # HTTP handlers
│ ├── infrastructure/ # External services (JWT, hash)
│ ├── middleware/ # HTTP middleware
│ ├── pubsub/ # Redis Pub/Sub
│ ├── repository/ # Data access layer
│ ├── usecase/ # Business logic
│ └── websocket/ # WebSocket handling
├── pkg/
│ └── response/ # HTTP response helpers
├── tests/
│ └── integration/ # Integration tests
├── scripts/ # SQL scripts
├── frontend/ # SvelteKit frontend
│ ├── src/
│ │ ├── lib/ # Shared components and utilities
│ │ ├── routes/ # Pages and layouts
│ │ └── tests/ # Frontend tests
│ └── e2e/ # Playwright E2E tests
└── .github/
└── workflows/ # CI/CD pipelines
Getting Started
Prerequisites
- Go 1.22+
- Node.js 20+
- Docker & Docker Compose
- PostgreSQL 15+ (or use Docker)
- Redis 7+ (or use Docker)
Quick Start with Docker
# Clone the repository
git clone https://github.com/kikikieieiei/realtime-chat.git
cd realtime-chat
# Start all services
docker-compose up -d
# Access the application
# Frontend: http://localhost:5173
# Backend API: http://localhost:8001
Local Development
Backend
# Install dependencies
go mod download
# Start PostgreSQL and Redis
docker-compose up -d postgres redis
# Run the server
go run cmd/server/main.go
Frontend
cd frontend
# Install dependencies
npm install
# Start development server
npm run dev
Testing
Backend Unit Tests
# Run all unit tests
go test ./internal/usecase/... -v
# Run with coverage
go test ./internal/usecase/... -coverprofile=coverage.out
go tool cover -html=coverage.out
Backend Integration Tests
# Requires Docker for test containers
go test ./tests/integration/... -v
Frontend Unit Tests
cd frontend
# Run tests
npm run test
# Run once
npm run test:run
# With coverage
npm run test:coverage
E2E Tests with Playwright
cd frontend
# Install Playwright browsers
npx playwright install
# Run E2E tests
npm run test:e2e
# Run with UI
npm run test:e2e:ui
API Endpoints
Authentication
| Method |
Endpoint |
Description |
| POST |
/api/v1/auth/register |
Register new user |
| POST |
/api/v1/auth/login |
Login |
| POST |
/api/v1/auth/refresh |
Refresh access token |
| POST |
/api/v1/auth/logout |
Logout |
Rooms
| Method |
Endpoint |
Description |
| GET |
/api/v1/rooms |
Get user's rooms |
| POST |
/api/v1/rooms |
Create new room |
| GET |
/api/v1/rooms/:id |
Get room details |
| PUT |
/api/v1/rooms/:id |
Update room |
| DELETE |
/api/v1/rooms/:id |
Delete room |
| POST |
/api/v1/rooms/:id/invite |
Invite user |
| POST |
/api/v1/rooms/:id/leave |
Leave room |
| GET |
/api/v1/rooms/:id/members |
Get room members |
Messages
| Method |
Endpoint |
Description |
| GET |
/api/v1/rooms/:id/messages |
Get messages |
| POST |
/api/v1/rooms/:id/messages |
Send message |
| PUT |
/api/v1/rooms/:id/messages/:msgId |
Edit message |
| DELETE |
/api/v1/rooms/:id/messages/:msgId |
Delete message |
WebSocket
| Endpoint |
Description |
ws://localhost:8001/ws |
WebSocket connection |
WebSocket Events
| Event |
Direction |
Description |
join_room |
Client -> Server |
Join a chat room |
leave_room |
Client -> Server |
Leave a chat room |
send_message |
Client -> Server |
Send a message |
typing_start |
Client -> Server |
Start typing indicator |
typing_stop |
Client -> Server |
Stop typing indicator |
new_message |
Server -> Client |
New message received |
message_edited |
Server -> Client |
Message was edited |
message_deleted |
Server -> Client |
Message was deleted |
user_joined |
Server -> Client |
User joined room |
user_left |
Server -> Client |
User left room |
user_typing |
Server -> Client |
User is typing |
Environment Variables
Backend
| Variable |
Default |
Description |
SERVER_PORT |
8001 |
Server port |
DB_HOST |
localhost |
PostgreSQL host |
DB_PORT |
5432 |
PostgreSQL port |
DB_USER |
chatuser |
Database user |
DB_PASSWORD |
chatpass |
Database password |
DB_NAME |
chatdb |
Database name |
REDIS_HOST |
localhost |
Redis host |
REDIS_PORT |
6379 |
Redis port |
JWT_SECRET |
(required) |
JWT signing secret |
Frontend
| Variable |
Default |
Description |
VITE_API_URL |
http://localhost:8001 |
Backend API URL |
VITE_WS_URL |
ws://localhost:8001 |
WebSocket URL |
Test Accounts
When using the seeded database:
Architecture
This project follows Clean Architecture principles:
- Domain Layer: Core business entities and logic
- Use Case Layer: Application-specific business rules
- Interface Layer: Controllers, presenters, and gateways
- Infrastructure Layer: External tools and frameworks
Key Design Patterns
- Repository Pattern: Abstract data access
- Dependency Injection: Loose coupling
- Clean Architecture: Separation of concerns
- WebSocket Hub: Centralized connection management
- Pub/Sub Pattern: Multi-server message broadcasting
License
MIT License