A real-time multiplayer tic-tac-toe game built with SvelteKit and Cloudflare infrastructure. Features instant WebSocket updates, persistent game storage, and global scalability. Play it!
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:
cd websocket-worker
npx wrangler dev --local --port 8787
Then in project root, run npm run dev
This will connect to the local webSocket worker. It does not use Cloudflare, and it's very fast. If for some reason it does not connect (like if the worker is not running), it will fall back to polling every 2 seconds to check for game updates.
npm run test
First-time setup (one-time only):
npx playwright install
Run E2E tests: This is good for running in CI build. After starting the worker separate terminal, run this:
npm run test:e2e
Watch tests run in browser (recommended) Pops up browser windows so you can see what is being tested.
npx playwright test --headed
Run specific test file
npx playwright test practical-tests.test.ts --headed
The E2E tests will automatically start the development server, run tests against real browser instances, and shut down when complete. Tests include:
There are two ways to do it. The preferred way is to just integrate github with Cloudflare. Then, any commit to a branch will automatically deploy to preview, and any commit to master, will deploy to production.
Alternatively, manually deploy your main SvelteKit app to Cloudflare Pages with these commands:
npm run build
npm run deploy
Your app will be available at your Cloudflare Pages URL with instant real-time updates.
# 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" }
]
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!