A real-time multiplayer tic-tac-toe game built with SvelteKit and Cloudflare infrastructure. Features instant WebSocket updates, persistent game storage, and global scalability.
git clone https://github.com/barrybecker4/svelte-OnlineTTT
cd svelte-OnlineTTT
npm install
The WebSocket worker handles real-time game coordination using Cloudflare Durable Objects:
# Navigate to websocket worker directory
cd websocket-worker
# Install dependencies
npm install
# Deploy the WebSocket worker to Cloudflare
npm run deploy
# Return to main directory
cd ..
Note: The WebSocket worker creates a separate Durable Object instance for each game, enabling real-time updates between players. You only need to deploy this once.
Verify your WebSocket worker is running:
curl https://svelte-ttt-websocket.YOUR_USERNAME.workers.dev/health
# Should return: "WebSocket service is running"
Replace YOUR_USERNAME
with your actual Cloudflare username.
For local development, use the simple setup:
npm run dev
# Visit http://localhost:5173
How it works:
To run the unit tests, use npm run test:unit
.
To run e2e tests, use npm run test:e2e
.
Deploy your main SvelteKit app to Cloudflare Pages:
npm run build
npm run deploy
Your app will be available at your Cloudflare Pages URL with instant real-time updates.
Local Development:
npm run dev
setupProduction:
# Local development (recommended)
npm run dev # SvelteKit dev server + deployed WebSocket worker
# WebSocket worker management
cd websocket-worker
npm run deploy # Deploy WebSocket worker (one-time setup)
npm run tail # View WebSocket worker logs
# Main app deployment
npm run build # Build for production
npm run deploy # Deploy to Cloudflare Pages
# Code quality
npm run lint # Lint code
npm run format # Format code
npm run check # Type checking
The WebSocket client is configured to use:
svelte-ttt-websocket.barrybecker4.workers.dev
To use your own deployed worker, update src/lib/websocket/client.ts
:
// Replace with your worker URL
const host = 'svelte-ttt-websocket.YOUR_USERNAME.workers.dev';
Update KV namespace IDs in wrangler.toml
to match your Cloudflare dashboard:
[env.preview]
kv_namespaces = [
{ binding = "TTT_GAME_KV", id = "YOUR_PREVIEW_KV_ID" }
]
[env.production]
kv_namespaces = [
{ binding = "TTT_GAME_KV", id = "YOUR_PRODUCTION_KV_ID" }
]
Game not updating in real-time:
WebSocket connection errors:
curl https://svelte-ttt-websocket.YOUR_USERNAME.workers.dev/health
Players can't join:
Feature | Local Development | Production |
---|---|---|
Player joining | Detected via polling (2s delay) | Instant WebSocket notification |
Move updates | Detected via polling (2s delay) | Instant WebSocket notification |
Setup complexity | Simple (npm run dev ) |
Automatic via Cloudflare Pages |
WebSocket worker | Uses deployed version | Uses deployed version |
npm run dev
MIT License - feel free to use this project as a starting point for your own real-time multiplayer games!