A real-time multiplayer Tic-Tac-Toe game built with Next.js, WebSockets, and PostgreSQL. Features include live gameplay, spectator mode, chat functionality, user profiles, and game statistics tracking.
2025-11-30.18-34-27.mp4
- Real-time Multiplayer: Play against other players in real-time using WebSocket connections
- Spectator Mode: Watch ongoing games without participating
- Room System: Create or join game rooms with unique room IDs
- Turn-based Gameplay: Automatic turn management with visual indicators
- Win Detection: Automatic detection of wins, losses, and draws
- Sound Effects: Audio feedback for moves, wins, and draws
- Authentication: Secure JWT-based authentication with 1-hour token expiry
- User Profiles: View personal stats including total games, wins, losses, and draws
- Game History: Track and review your last 20 games
- Live Chat: In-game chat for players and spectators
- Room Management: Admin controls for starting games and closing rooms
- Player Count: Real-time display of connected players
- Premium Design: Modern, glassmorphic UI with gradient effects
- Responsive Layout: Works seamlessly on desktop and mobile devices
- Dark Theme: Eye-friendly dark color scheme
- Smooth Animations: Polished transitions and hover effects
- Framework: Next.js 15 (React 19)
- Styling: Tailwind CSS
- State Management: React Hooks & Context API
- HTTP Client: Axios
- Notifications: Sonner
- WebSocket: Native WebSocket API
- Runtime: Bun
- Framework: Express.js
- WebSocket: ws
- Database: PostgreSQL
- ORM: Prisma
- Authentication: JWT
- Password Hashing: bcryptjs
- Validation: Zod
- Build System: Turborepo
- Package Manager: Bun
- Workspace Structure: Apps and shared packages
tic-tac-toe/
โโโ apps/
โ โโโ backend/ # Express API server
โ โ โโโ controller/ # Route controllers
โ โ โโโ middleware/ # Auth middleware
โ โ โโโ routes/ # API routes
โ โโโ web/ # Next.js frontend
โ โ โโโ app/ # App router pages
โ โ โโโ component/ # React components
โ โ โโโ context/ # React contexts
โ โ โโโ hooks/ # Custom hooks
โ โ โโโ utils/ # Utility functions
โ โโโ ws/ # WebSocket server
โ โโโ GameManager.ts
โ โโโ index.ts
โโโ packages/
โโโ db/ # Shared Prisma schema
โโโ prisma/
- Node.js >= 18
- Bun >= 1.2.22
- PostgreSQL database
-
Clone the repository
git clone https://github.com/Ameerjafar/tictactoe.git cd tic-tac-toe -
Install dependencies
bun install
-
Set up environment variables
Create
.envfiles in the following locations:apps/backend/.envDATABASE_URL="postgresql://user:password@localhost:5432/tictactoe" JWT_SECRET="your-secret-key-here" PORT=5000
apps/web/.envNEXT_PUBLIC_BACKEND_URL="http://localhost:5000" NEXT_PUBLIC_WS_URL="ws://localhost:8080"
packages/db/.envDATABASE_URL="postgresql://user:password@localhost:5432/tictactoe"
-
Set up the database
cd packages/db bunx prisma migrate dev bunx prisma generate cd ../..
-
Start the development servers
# Start all services (recommended) bun run dev # Or start individually: # Terminal 1 - Backend API cd apps/backend && bun run dev # Terminal 2 - WebSocket Server cd apps/ws && bun run dev # Terminal 3 - Frontend cd apps/web && bun run dev
-
Access the application
- Frontend: http://localhost:3000
- Backend API: http://localhost:5000
- WebSocket: ws://localhost:8080
-
Sign Up / Sign In
- Create an account or sign in with existing credentials
-
Create or Join a Room
- Click "Create New Room" to start a new game
- Or enter a Room ID to join an existing game
- Choose between Player or Spectator mode
-
Wait for Players
- The game starts automatically when 2 players join
- Spectators can watch and chat
-
Play the Game
- Take turns placing X or O on the board
- First to get 3 in a row wins!
- Use the chat to communicate with other players
-
View Your Stats
- Click "View Profile" to see your game statistics
- Review your game history and performance
- JWT-based authentication with 1-hour token expiry
- Automatic token validation on protected routes
- Secure password hashing with bcrypt
- Client-side token expiry checking
id: UUID (Primary Key)name: Stringemail: String (Unique)password: String (Hashed)totalGames: Integerwins: Integerlosses: Integerdraws: Integer
id: UUID (Primary Key)roomId: StringuserId: String (Foreign Key)isWin: BooleancreatedAt: DateTime
POST /api/v1/auth/signup- Create new userPOST /api/v1/auth/signin- Sign in user
GET /api/v1/game/userstats- Get user statisticsGET /api/v1/game/usergames- Get user game historyPOST /api/v1/game/creategame- Create game recordPOST /api/v1/game/updatestats- Update user stats
createRoom- Create a new game roomjoinRoom- Join an existing roomupdateGameState- Send game movemessage- Send chat messagerestartGame- Restart game (admin only)closeRoom- Close room (admin only)
userList- Updated list of users in roomupdateGameState- Game state updatemessage- Chat messagestartGame- Game startedrestartGame- Game restartedcloseRoom- Room closed
# Development
bun run dev # Start all services in dev mode
# Build
bun run build # Build all apps for production
# Linting
bun run lint # Run ESLint on all packages
# Type Checking
bun run check-types # Run TypeScript type checking
# Formatting
bun run format # Format code with Prettiercd packages/db
# Create a new migration
bunx prisma migrate dev --name migration_name
# Apply migrations
bunx prisma migrate deploy
# Generate Prisma Client
bunx prisma generate
# Open Prisma Studio
bunx prisma studioContributions are welcome! Please feel free to submit a Pull Request.
- Fork the repository
- Create your feature branch (
git checkout -b feature/AmazingFeature) - Commit your changes (
git commit -m 'Add some AmazingFeature') - Push to the branch (
git push origin feature/AmazingFeature) - Open a Pull Request
This project is open source and available under the MIT License.
Ameer Jafar
- GitHub: @Ameerjafar
- Built with โค๏ธ using modern web technologies
- Inspired by the classic Tic-Tac-Toe game
- Special thanks to the open-source community
Enjoy playing! ๐ฎ