A real-time multiplayer Connect 4 game system built with Go backend, React frontend, and Kafka-based analytics pipeline.
๐ฎ Real-Time Multiplayer - Play live games via WebSocket with instant move synchronization
๐ค Smart AI Bot - Challenge an intelligent bot using minimax algorithm with alpha-beta pruning
โก Auto Matchmaking - 10-second queue with automatic bot fallback
๐ Kafka Analytics - Real-time game metrics and player behavior tracking via Apache Kafka
๐ Live Leaderboard - Track rankings, win rates, and player statistics
๐ Session Persistence - Automatic reconnection with 30-second grace period
- Real-time multiplayer gameplay via WebSocket connections
- Intelligent bot opponents using minimax algorithm with alpha-beta pruning
- Automatic matchmaking with 10-second timeout fallback to bot games
- Player reconnection support with 30-second session persistence
- Live leaderboard with player statistics and rankings
- Kafka-powered analytics - Tracks gameplay metrics, player behavior, game duration, and peak hours
- Go Backend Server: Game logic, WebSocket communication, REST API
- React Frontend: User interface for gameplay and leaderboard
- PostgreSQL: Game data and player statistics storage
- Kafka: Analytics event streaming
- Redis: Session management and caching
- Go 1.21+
- Node.js 18+
- Docker & Docker Compose
- Make (optional, for convenience commands)
-
Clone the repository
git clone <repository-url> cd connect4-multiplayer
-
Start the backend services
Option A: Using Docker (Recommended)
# Start all services (PostgreSQL, Redis, Kafka, Backend) docker-compose upOption B: Local Development
# 1. Start dependencies (PostgreSQL, Redis, Kafka) docker-compose up postgres redis kafka # 2. Run database migrations go run cmd/migrate/main.go # 3. Start the backend server go run cmd/server/main.go # OR with Make: make run-server # 4. (Optional) Start analytics service go run cmd/analytics/main.go
The server will start on http://localhost:8080 with WebSocket endpoint at ws://localhost:8080/ws
-
Start the frontend
cd web npm install npm run devFrontend will be available at http://localhost:5173
-
Set up credentials (Choose one option)
Option A: Interactive Setup (Recommended)
make setup-credentials
Option B: Manual Setup
# Create environment file make create-env # Edit .env with your Supabase and Confluent Cloud credentials # See CREDENTIALS_SETUP.md for detailed instructions
Option C: Local Development Only
# Use local services (no cloud setup required) make docker-up # Uses local PostgreSQL, Redis, and Kafka
-
Validate your setup
# Test database and Kafka connections make test-db make test-kafka -
Install development tools
make setup
-
Run database migrations
make migrate
-
Start the development server
make dev
The server will start at http://localhost:8080 with hot reload enabled.
๐ Need help with credentials? See CREDENTIALS_SETUP.md for a quick setup guide or docs/cloud-setup-guide.md for detailed instructions.
make run-analytics # Start Kafka analytics consumer
## ๐ Kafka Analytics
The system includes **real-time analytics** powered by Apache Kafka. Game events are published to Kafka and consumed by an analytics service that tracks:
- **Gameplay Metrics**: Average game duration, games per hour/day, min/max duration
- **Player Metrics**: Unique players per hour, active games, player win rates
- **Event Tracking**: All game events (started, moves, completed) stored for analysis
### Running Analytics Service
```bash
# Build analytics consumer
go build -o analytics.exe ./cmd/analytics
# Run analytics service
./analytics.exe
-- View latest analytics snapshot
SELECT
timestamp,
games_completed_hour,
games_completed_day,
avg_game_duration_sec,
unique_players_hour
FROM analytics_snapshots
ORDER BY timestamp DESC
LIMIT 10;
-- Most frequent winners
SELECT username, games_won, win_rate
FROM player_stats
ORDER BY games_won DESC
LIMIT 10;๐ For detailed analytics guide, see docs/kafka-analytics-guide.md
/
โโโ cmd/ # Application entry points
โ โโโ server/ # Game server main
โ โโโ analytics/ # Analytics service main
โ โโโ migrate/ # Database migration tool
โโโ internal/ # Private application code
โ โโโ game/ # Game logic and engine
โ โโโ websocket/ # WebSocket connection management
โ โโโ bot/ # AI bot implementation
โ โโโ matchmaking/ # Player matching service
โ โโโ analytics/ # Analytics event processing
โ โโโ database/ # Database models and operations
โโโ pkg/ # Public library code
โ โโโ models/ # Shared data structures
โ โโโ utils/ # Common utilities
โโโ web/ # Frontend React application
โโโ migrations/ # Database schema migrations
โโโ docs/ # Project documentation
Once the server is running, visit:
- Swagger UI:
http://localhost:8080/swagger/index.html - Health Check:
http://localhost:8080/health
| Variable | Description | Default |
|---|---|---|
ENVIRONMENT |
Application environment | development |
SERVER_PORT |
Server port | 8080 |
DATABASE_URL |
PostgreSQL connection string | Local PostgreSQL |
KAFKA_BOOTSTRAP_SERVERS |
Kafka brokers | localhost:9092 |
KAFKA_API_KEY |
Confluent Cloud API key | - |
KAFKA_API_SECRET |
Confluent Cloud API secret | - |
REDIS_URL |
Redis connection string | localhost:6379 |
- Create a new Supabase project
- Get your connection string from Settings > Database
- Set
DATABASE_URLenvironment variable
- Create a Confluent Cloud account
- Create a new cluster
- Generate API keys
- Set
KAFKA_BOOTSTRAP_SERVERS,KAFKA_API_KEY, andKAFKA_API_SECRET
๐ Deploy your game for FREE! See our comprehensive deployment guides:
-
DEPLOYMENT.md - Complete step-by-step deployment guide
- Deploy backend to Render.com (Free tier with WebSocket support)
- Deploy frontend to Vercel (Free tier with auto-deploy)
- No shell access needed - Migrations run automatically!
- Environment variable templates included
- Troubleshooting guide for common issues
-
DEPLOYMENT_QUICK_REF.md - Quick reference card
- Environment variables cheat sheet
- One-page deployment steps
- Common issues and solutions
- Backend: Render.com (Go server + PostgreSQL)
- Frontend: Vercel (React/Vite)
- Database: Supabase or Render PostgreSQL
- Kafka: Confluent Cloud (already configured)
# Install Railway CLI
npm install -g @railway/cli
# Login and deploy
railway login
railway init
railway up# Install flyctl
curl -L https://fly.io/install.sh | sh
# Deploy
fly launch
fly deploymake testmake test-propertymake test-integrationmake test-coverage
open coverage.html- Fork the repository
- Create a feature branch
- Make your changes
- Add tests for new functionality
- Run the test suite
- Submit a pull request
MIT License - see LICENSE file for details.
For questions and support, please open an issue on GitHub.