This repository is a companion to the DevTools & DevCools: Cloudflare Edition livestream. If you missed the stream or want to catch it again, you can watch it here!.
In this project, we built a multiplayer Tic Tac Toe game using Cloudflare Workers and Durable Objects to manage game state across clients. Players can create or join a room and play Tic Tac Toe in real-time, with game data synchronized between the players through WebSocket communication.
- Real-Time Multiplayer: Players can create or join rooms and play Tic Tac Toe in real time.
- Durable Game State: Game state is persisted and synchronized using Cloudflare Durable Objects.
- WebSocket Communication: WebSocket is used for efficient real-time communication between clients.
- Responsive UI: Built with Tailwind CSS for a simple and responsive interface.
-
Cloudflare Workers: These are serverless functions that run on Cloudflare's global network. In this project, they serve as the backend, handling requests to create or join game rooms and managing real-time communication via WebSockets.
-
Durable Objects: These are stateful objects that allow data to be persisted and synchronized across clients. In this project, each game room has its own Durable Object instance, which stores the game state (board, player turns) and ensures it stays consistent between players in real time.
-
Create or Join Room:
- A player can create a new room, generating a unique room code.
- Another player can join the room using the room code.
-
Game Start:
- Once both players are in the room, the game board becomes visible, and the game starts.
-
Gameplay:
- Players take turns marking cells on the board (X and O).
- The game detects win conditions and displays the result (win or draw).
-
Restart Game:
- A "Restart" button allows players to reset the game board and start a new round.
To deploy this project, follow these steps:
- Install Wrangler CLI to manage Cloudflare Workers.
- A Cloudflare account with Workers and Durable Objects enabled.
-
Clone the repository:
git clone https://github.com/DevCycleHQ-Sandbox/cloudflare-tictactoe.git cd cloudflare-tictactoe
-
Install dependencies:
Install the necessary dependencies by running:
npm install
-
Configure Wrangler:
Ensure your
wrangler.toml
is set up correctly for Durable Objects, assets, and other configurations (this is provided in the repository). -
Run the project locally:
Start the development server:
npm run dev
This will run the Workers project locally, allowing you to test the application at
http://localhost:8787/
. -
Deploy to Cloudflare Workers:
Once you're ready to deploy, use:
npm run deploy
This will push your code to Cloudflare and make it available at your worker's URL.
Contributions are welcome! If you would like to contribute to this project, please fork the repository and submit a pull request.
This project is licensed under the MIT License. See the LICENSE file for more details.