Production-oriented multiplayer Tic-Tac-Toe built for the LILA backend assignment. The app uses a React frontend and a Nakama backend with server-authoritative match logic, automatic matchmaking, public and private rooms, timed mode, persistent leaderboard stats, and account-based authentication.
- Server-authoritative game logic in Nakama Lua runtime
- Server-side move validation and cheating prevention
- Realtime state broadcasting to both clients
- Automatic matchmaking
- Public room creation, discovery, and joining
- Graceful disconnect handling
- Responsive mobile-friendly frontend
- Realtime match state, player info, and match status in the UI
- Concurrent game support through isolated Nakama match instances
- Global leaderboard with wins, losses, and streak tracking
- Timed mode with 30-second turns and automatic timeout forfeits
- Mode-aware matchmaking for classic vs timed games
- Persistent player stats in Nakama storage
- React
- React Router
- Nakama JavaScript client
- Custom CSS
- Nakama 3.x
- Lua runtime modules
- CockroachDB
- Docker Compose for local orchestration
| Layer | Responsibility |
|---|---|
src/pages/* |
Screens for auth, lobby, game, and leaderboard |
src/nakama.js |
Session management, auth helpers, RPC calls, websocket lifecycle |
server/modules/tictactoe.lua |
Authoritative match state, move validation, timed turns, disconnect handling, leaderboard updates |
server/modules/backend.lua |
RPC endpoints for room creation, room discovery, signup/login lookup, and quick match creation |
| CockroachDB | Nakama user/account data and persistent game stats |
- User signs up or signs in with email/password, then may sign in later using either email or username.
- Lobby allows:
- Quick Match
- Public Room creation/discovery
- Private Room creation/join by room ID
- Client sends only move intents to the server.
- Nakama validates the move, updates authoritative board state, and broadcasts the result.
- Finished matches update persistent stats and leaderboard records.
- Sign up with unique email and unique username
- Sign in using email or username
- Route protection for
/,/lobby,/game, and/leaderboard - Session restore on reload using stored Nakama tokens
- Quick Match for classic mode
- Quick Match for timed mode
- Public room creation and room discovery
- Private room creation with shareable room ID
- Join room directly by room ID
- Waiting-room flow before game starts
- Server-authoritative Tic-Tac-Toe
- Turn validation
- Occupied-cell validation
- Win and draw detection
- Opponent disconnect detection with win award
- Result popup with win/lose/draw state
- Wins tracked as leaderboard score
- Win streak tracked as leaderboard subscore
- Loss count stored in leaderboard metadata
- Persistent per-player stats in Nakama storage
- 30-second turn timer
- Countdown shown in UI
- Automatic forfeit when a player times out
.
├── docker-compose.yml
├── docker-compose.gcp.yml
├── deploy/
│ └── gcp/
│ ├── Caddyfile
│ └── README.md
├── server/
│ └── modules/
│ ├── backend.lua
│ └── tictactoe.lua
├── src/
│ ├── nakama.js
│ └── pages/
│ ├── Auth.js
│ ├── Game.js
│ ├── Leaderboard.js
│ ├── Lobby.js
│ └── pages.css
└── README.md
- Node.js 18+
- npm
- Docker
- Docker Compose
docker compose up -dLocal endpoints:
- Nakama API/WebSocket:
http://127.0.0.1:7350 - Nakama Console:
http://127.0.0.1:7351 - CockroachDB SQL:
127.0.0.1:26257 - CockroachDB Admin UI:
http://127.0.0.1:8080
Copy .env.example to .env if you want to override defaults:
REACT_APP_NAKAMA_HOST=127.0.0.1
REACT_APP_NAKAMA_PORT=7350
REACT_APP_NAKAMA_KEY=defaultkey
REACT_APP_NAKAMA_HTTP_KEY=defaultkey
REACT_APP_NAKAMA_USE_SSL=falsenpm installnpm startFrontend runs at:
http://localhost:3000
- Open the app in two browsers or two separate profiles.
- Create two different accounts.
- Confirm sign-in works with:
- email + password
- username + password
- In both clients, choose
Quick Match. - Select the same mode on both sides:
Classic- or
Timed
- Click
Find Match. - Confirm both players land in the same match and see synchronized state.
- Client A opens
Public Room. - Client A creates a room.
- Client B opens
Public Room. - Confirm the room appears in the list.
- Client B joins and the game starts once both players are present.
- Client A opens
Private Room. - Client A creates a room.
- Copy the room ID.
- Client B opens
Private Roomand joins using the room ID.
- Join a live game with two players.
- Try to click:
- out of turn
- an already occupied cell
- Confirm the board does not accept the invalid move.
- Start a game with two players.
- Close one player tab or navigate away.
- Confirm the remaining player sees the opponent-left result and is awarded the win.
- Start a timed match.
- Let one player sit idle on their turn for 30 seconds.
- Confirm timeout triggers a forfeit and the other player wins.
- Finish one or more decisive matches.
- Open
/leaderboard. - Confirm wins, streak, and losses appear correctly.
1: move intent from client to server2: authoritative game-state broadcast from server to clients
Defined in server/modules/backend.lua:
create_roomcreate_private_matchlist_open_matchesresolve_login_identifiercheck_signup_availability
- board cells
- player marks
- current turn
- winner
- finish reason
- player count
- usernames
- timed-mode remaining seconds
- room type and mode
All game state lives on the Nakama server. The client never decides whether a move is valid; it only sends a move request. This prevents tampering and keeps both clients synchronized from a single source of truth.
Room creation, public-room discovery, and username lookup are handled through explicit RPCs so the frontend stays simple and the room lifecycle remains controlled on the backend.
Leaderboard records are great for ranking, while storage objects make it easy to persist extra stats cleanly. This project uses both:
- storage for wins/losses/streak history
- leaderboard for ranking and display
The backend distinguishes:
publicprivatematchmaking
This keeps public discovery clean while allowing private joins and quick-match-created games to stay isolated.
This repository includes:
- a complete local Docker Compose stack
- a GCP-oriented production Docker Compose stack in
docker-compose.gcp.yml - a Caddy HTTPS reverse-proxy config for a public Nakama hostname in
deploy/gcp/Caddyfile - frontend env examples for Vercel in
.env.example - backend env examples for GCP in
.env.gcp.example
Live deployment for this submission:
- Frontend:
https://lila-multiplayer-tic-tac-toe-six.vercel.app/auth - Nakama endpoint:
https://nakama-136-115-5-124.nip.io
- Frontend: Vercel
- Backend: GCP Compute Engine VM running Docker Compose
- HTTPS/TLS for Nakama: Caddy on the VM
Because the frontend is served over https:// on Vercel, the browser should talk to Nakama over https:// / wss:// as well. That means the Nakama VM needs a real public hostname such as:
nakama.your-domain.com- or a free DNS hostname you control
For this submission, the deployment uses:
nakama-136-115-5-124.nip.io
The provided GCP stack uses Caddy so the public Nakama hostname can terminate TLS automatically.
- Push the repo to GitHub.
- In Vercel, import the GitHub repository.
- Let Vercel build the project as a React app.
- Set these environment variables in the Vercel project:
REACT_APP_NAKAMA_HOST=nakama-136-115-5-124.nip.io
REACT_APP_NAKAMA_PORT=443
REACT_APP_NAKAMA_KEY=your-server-key
REACT_APP_NAKAMA_HTTP_KEY=your-http-key
REACT_APP_NAKAMA_USE_SSL=true- Deploy.
Build settings:
- Build command:
npm run build - Output directory:
build
Use a Compute Engine VM with a small always-on machine type suitable for a demo or assignment, for example e2-small or better.
Suggested OS:
- Ubuntu LTS
Reserve one static external IP and attach it to the VM so your backend hostname does not change after restarts.
Create an A record for a hostname such as:
nakama.your-domain.com
Point it to the VM static IP.
For this submission, a nip.io hostname was used instead of a custom domain:
nakama-136-115-5-124.nip.io
Allow inbound traffic to:
80443
Do not expose these publicly:
7351Nakama Console7349gRPC admin port26257Cockroach SQL8080Cockroach admin UI
Install Docker Engine and Docker Compose on the Compute Engine VM.
Place the full project on the VM so the compose file can mount:
./server/modules./deploy/gcp/Caddyfile
From the repo root:
cp .env.gcp.example .env.gcpUpdate:
NAKAMA_DOMAINNAKAMA_SERVER_KEYNAKAMA_HTTP_KEYNAKAMA_CONSOLE_USERNAMENAKAMA_CONSOLE_PASSWORD
From the repo root:
docker compose --env-file .env.gcp -f docker-compose.gcp.yml up -dThis stack will:
- start CockroachDB
- run Nakama migrations
- start Nakama with your Lua modules
- expose Nakama publicly through Caddy at
https://NAKAMA_DOMAIN
After DNS has propagated and Caddy has issued TLS certificates, your public Nakama endpoint should be:
https://nakama.your-domain.com
That same hostname should be used by the Vercel frontend.
For this deployed submission, the live endpoint is:
https://nakama-136-115-5-124.nip.io
Use SSH tunneling instead of public exposure:
gcloud compute ssh YOUR_INSTANCE_NAME --zone YOUR_ZONE -- -L 7351:localhost:7351Then open:
http://localhost:7351
Additional GCP notes are in deploy/gcp/README.md.
- Frontend should use:
REACT_APP_NAKAMA_HOST=nakama-136-115-5-124.nip.ioREACT_APP_NAKAMA_PORT=443REACT_APP_NAKAMA_USE_SSL=true
- Backend should use strong values for:
NAKAMA_SERVER_KEYNAKAMA_HTTP_KEY
- GCP compose keeps the database, console, and admin interfaces bound to loopback only.
- Source code repository: https://github.com/shrxyaaa/lila-multiplayer-tic-tac-toe
- Deployed frontend URL: https://lila-multiplayer-tic-tac-toe-six.vercel.app/auth
- Deployed Nakama endpoint: https://nakama-136-115-5-124.nip.io
- README with setup, architecture, deployment, server config, and multiplayer test instructions: included here
- If Safari or macOS has trouble connecting to Nakama on
localhost, the app defaults to127.0.0.1for Nakama to avoid IPv6 localhost websocket issues. - If quick match waits for a long time, make sure both players selected the same mode.
- If public rooms do not appear, make sure the creating player has already entered the waiting room.
- If the leaderboard is empty, finish at least one non-draw match.
- If auth fails after idle time, sign in again to refresh the Nakama session.
server/modules/tictactoe.luaserver/modules/backend.luasrc/nakama.jssrc/pages/Lobby.jssrc/pages/Game.jssrc/pages/Leaderboard.jsdocker-compose.ymldocker-compose.gcp.ymldeploy/gcp/Caddyfile