A real-time multiplayer chess application built with React, Node.js, Socket.IO, and PostgreSQL. Features include matchmaking, live gameplay, game history, and a responsive chess board interface.
- Real-time Chess: Play chess with other players in real-time using Socket.IO
- Interactive Chess Board: Drag-and-drop interface powered by react-chessboard
- Move Validation: Server-side move validation using chess.js
- Game States: Support for checkmate, stalemate, draws, and resignations
- Queue-based Matchmaking: Join a queue to find opponents with similar ratings
- Real-time Queue Updates: See live updates of players in queue and wait times
- Automatic Pairing: Random color assignment when matches are found
- Game History: View your recent games with results and statistics
- Resume Games: Continue active games from where you left off
- Game Timeout: Automatic draw after 10 minutes of inactivity
- Spectator Mode: View completed games
- User Registration/Login: Secure authentication with JWT tokens
- Player Ratings: Elo-based rating system (starting at 1200)
- User Profiles: Track games played and current rating
- React 18 with TypeScript
- React Router for navigation
- Socket.IO Client for real-time communication
- react-chessboard for the chess interface
- chess.js for game logic
- Axios for API requests
- Tailwind CSS for styling
- Node.js with Express and TypeScript
- Socket.IO for WebSocket connections
- PostgreSQL with connection pooling
- JWT for authentication
- bcryptjs for password hashing
- chess.js for server-side game validation
- PostgreSQL (compatible with Supabase)
- UUID primary keys for better scalability
- Row Level Security policies
- Node.js 18+ and npm
- PostgreSQL 14+ (or Supabase account)
- Git
-
Clone the repository
git clone https://github.com/yourusername/chess-clone.git cd chess-clone
-
Install backend dependencies
cd backend npm install
-
Install frontend dependencies
cd ../frontend npm install
-
Database Setup
Supabase
- Create a new Supabase project
- Go to SQL Editor and run the contents of
backend/supabase-schema.sql
- Get your database URL from Settings > Database
-
Environment Configuration
Backend (.env)
# Supabase, transaction pooler uri # DATABASE_URL=postgresql://postgres.[project id]:[YOUR-PASSWORD]@aws-1-us-east-1.pooler.supabase.com:6543/postgres # Security JWT_SECRET=your-super-secret-jwt-key-here # CORS FRONTEND_URL=http://localhost:3000 # Server PORT=5000
Frontend (.env)
REACT_APP_API_URL=http://localhost:5000/api REACT_APP_SOCKET_URL=http://localhost:5000
-
Start the backend server
cd backend npm run dev
Server will start on http://localhost:5000
-
Start the frontend development server
cd frontend npm start
Application will open at http://localhost:3000
-
Create user accounts and start playing!
chess-clone/
├── backend/
│ ├── src/
│ │ ├── config/
│ │ │ ├── database.ts # Database connection
│ │ │ └── index.ts # App configuration
│ │ ├── middleware/
│ │ │ └── auth.ts # JWT authentication
│ │ ├── models/
│ │ │ ├── Game.ts # Game database model
│ │ │ └── User.ts # User database model
│ │ ├── routes/
│ │ │ ├── auth.ts # Authentication routes
│ │ │ └── games.ts # Game routes
│ │ ├── services/
│ │ │ ├── GameService.ts # Game logic & caching
│ │ │ ├── MatchmakingService.ts # Queue management
│ │ │ ├── SocketService.ts # Socket.IO handling
│ │ │ └── GameTimeoutService.ts # Game timeouts
│ │ ├── app.ts # Express app setup
│ │ └── server.ts # Server entry point
│ ├── migrations/ # Database migrations
│ │ └── supabase-schema.sql # Complete DB schema
│ └── package.json
│
├── frontend/
│ ├── src/
│ │ ├── components/
│ │ │ ├── ChessBoard.tsx # Main game component
│ │ │ ├── Lobby.tsx # Matchmaking lobby
│ │ │ ├── LoginForm.tsx # User login
│ │ │ └── RegistrationForm.tsx # User registration
│ │ ├── contexts/
│ │ │ ├── AuthContext.tsx # Authentication state
│ │ │ └── SocketContext.tsx # Socket connection
│ │ ├── config.ts # API configuration
│ │ └── App.tsx # Main app component
│ └── package.json
│
└── README.md
POST /api/auth/register
- User registrationPOST /api/auth/login
- User login
GET /api/games/my-games
- Get user's game historyGET /api/games/:gameId
- Get specific game detailsPOST /api/games/create
- Create a new game (deprecated, use matchmaking)
join-matchmaking
- Join the matchmaking queueleave-matchmaking
- Leave the matchmaking queuejoin-game
- Join a specific game roommake-move
- Make a chess moveoffer-draw
- Offer a draw to opponentaccept-draw
/decline-draw
- Respond to draw offerresign
- Resign from the game
match-found
- Match found with game detailsgame-joined
- Successfully joined gamemove-made
- Opponent made a movegame-over
- Game ended with resultdraw-offered
/draw-accepted
/draw-declined
- Draw offer updatesopponent-disconnected
- Opponent left the game
Backend:
cd backend
npm run build
npm start
Frontend:
cd frontend
npm run build
# Serve the build folder with a web server
The project uses SQL files for database schema. To make changes:
- Update
backend/supabase-schema.sql
- Create migration files in
backend/migrations/
- Apply changes to your database
- Fork the repository
- Create a feature branch (
git checkout -b feature/amazing-feature
) - Commit your changes (
git commit -m 'Add some amazing feature'
) - Push to the branch (
git push origin feature/amazing-feature
) - Open a Pull Request
- Game Caching: Active games are cached in memory for faster access
- Connection Pooling: Database connections are pooled for efficiency
- Queue Optimization: Matchmaking uses in-memory data structures
- chess.js for chess game logic
- react-chessboard for the chess board component
- Socket.IO for real-time communication
Happy Chess Playing! _�