In this tutorial, we will guide you through building a multiplayer chess game using the SuperViz. Multiplayer games require real-time synchronization and interaction between players, making them ideal applications for SuperViz's capabilities. This tutorial will show you how to create a chess game where two players can play against each other in real-time, seeing each other's moves as they happen.
We'll demonstrate how to set up a chessboard using the react-chessboard
library, manage the game state with chess.js
, and synchronize player moves with SuperViz. This setup allows multiple participants to join a chess game, make moves, and experience a seamless and interactive chess game environment. Let's get started!
Prerequisite
To follow this tutorial, you will need a SuperViz account and a developer token. If you already have an account and a developer token, you can move on to the next step.
Create an account
To create an account, go to https://dashboard.superviz.com/register and create an account using either Google or an email/password. It's important to note that when using an email/password, you will receive a confirmation link that you'll need to click to verify your account.
Retrieving a Developer Token
To use the SDK, you’ll need to provide a developer token, as this token is essential for associating SDK requests with your account. You can retrieve both development and production SuperViz tokens from the dashboard..
Copy and save the developer token, as you will need it in the next steps of this tutorial.
Step 1: Set Up Your React Application
To begin, you'll need to set up a new React project where we will integrate SuperViz.
1. Create a New React Project
First, create a new React application using Vite with TypeScript.
1npm create vite@latest chess-game -- --template react-ts2cd chess-game
2. Install Required Libraries
Next, install the necessary libraries for our project:
1npm install @superviz/sdk @superviz/realtime react-chessboard chess.js uuid
- @superviz/sdk: SDK for integrating real-time collaboration features, including synchronization.
- @superviz/realtime: SuperViz Real-Time library for integrating real-time synchronization into your application.
- react-chessboard: A library for rendering a chessboard in React applications.
- chess.js: A library for managing chess game logic and rules.
- uuid: A library for generating unique identifiers, useful for creating unique participant IDs.
3. Configure tailwind
In this tutorial, we'll use the Tailwind css framework. First, install the tailwind package.
1npm install -D tailwindcss postcss autoprefixer2npx tailwindcss init -p
We then need to configure the template path. Open tailwind.config.js
in the root of the project and insert the following code.
1/** @type {import('tailwindcss').Config} */2export default {3content: [4"./index.html",5"./src/**/*.{js,ts,jsx,tsx}",6],7theme: {8extend: {},9},10plugins: [],11}
Then we need to add the tailwind directives to the global CSS file. (src/index.css)
1@tailwind base;2@tailwind components;3@tailwind utilities;
4. Set Up Environment Variables
Create a .env
file in your project root and add your SuperViz developer key. This key will be used to authenticate your application with SuperViz services.
1VITE_SUPERVIZ_API_KEY=YOUR_SUPERVIZ_DEVELOPER_KEY
Step 2: Implement the Main Application
In this step, we'll implement the main application logic to initialize SuperViz and handle real-time chess moves.
1. Implement the App Component
Open src/App.tsx
and set up the main application component using SuperViz to manage the collaborative environment.
1import { v4 as generateId } from "uuid";2import { useCallback, useEffect, useRef, useState } from "react";3import SuperVizRoom, { WhoIsOnline } from "@superviz/sdk";4import {5Realtime,6type Channel,7type RealtimeMessage,8} from "@superviz/realtime/client";9import { Chessboard } from "react-chessboard";10import { Chess, Square } from "chess.js";
Explanation:
- Imports: Import necessary components from React, SuperViz SDK,
react-chessboard
,chess.js
, and UUID for managing state, initializing SuperViz, rendering the chessboard, and generating unique identifiers.
2. Define Constants
Define constants for the API key, room ID, and player ID.
1const apiKey = import.meta.env.VITE_SUPERVIZ_API_KEY as string;2const ROOM_ID = 'chess-game';3const PLAYER_ID = generateId();
Explanation:
- apiKey: Retrieves the SuperViz API key from environment variables.
- ROOM_ID: Defines the room ID for the SuperViz session.
- PLAYER_ID: Generates a unique player ID using the
uuid
library.
3. Define Chess Message Type
Create a type for handling chess move messages.
1type ChessMessageUpdate = RealtimeMessage<{2data: {3sourceSquare: Square;4targetSquare: Square;5};6}>;
Explanation:
- ChessMessageUpdate: Instantiates a specialization of the
RealtimeMessage
to define the source and target squares for a chess move.
4. Create the App Component
Set up the main App
component and initialize state variables.
1export default function App() {2const initialized = useRef(false);3const [gameState, setGameState] = useState<Chess>(new Chess());4const [gameFen, setGameFen] = useState<string>(gameState.fen());56const channel = useRef<Channel | null>(null);
Explanation:
- initialized: A state variable to track whether the SuperViz environment has been set up.
- gameState: A state variable to manage the chess game state using the
chess.js
library. - gameFen: A state variable to store the FEN (Forsyth-Edwards Notation) string representing the current game position.
- channel: A ref to store the real-time communication channel.
5. Initialize SuperViz and Real-Time Components
Create an initialize
function to set up the SuperViz environment and configure real-time synchronization.
1const initialize = useCallback(async () => {2if (initialized.current) return;3initialized.current = true;45const superviz = await SuperVizRoom(apiKey, {6roomId: ROOM_ID,7participant: {8id: PLAYER_ID,9name: "player-name",10},11group: {12id: "chess-game",13name: "chess-game",14},15});1617const realtime = new Realtime(apiKey, {18participant: {19id: PLAYER_ID,20name: "player-name",21},22});23const whoIsOnline = new WhoIsOnline();2425superviz.addComponent(whoIsOnline);2627channel.current = await realtime.connect("move-topic");2829channel.current.subscribe<ChessMessageUpdate["data"]>(30"new-move",31handleRealtimeMessage32);33}, [handleRealtimeMessage, initialized]);
Explanation:
- initialize: An asynchronous function that initializes the SuperViz room and checks if it's already initialized to prevent duplicate setups.
- SuperVizRoom: Configures the room, participant, and group details for the session.
- Realtime Subscription: Connects to the
move-topic
channel and listens for new moves, updating the local state accordingly.
6. Handle Chess Moves
Create a function to handle chess moves and update the game state.
1const makeMove = useCallback((sourceSquare: Square, targetSquare: Square) => {2try {3const gameCopy = gameState;4gameCopy.move({ from: sourceSquare, to: targetSquare, promotion: 'q' });56setGameState(gameCopy);7setGameFen(gameCopy.fen());89return true;10} catch (error) {11console.log('Invalid Move', error);12return false;13}14}, [gameState]);
Explanation:
- makeMove: Attempts to make a move on the chessboard, updating the game state and FEN string if the move is valid.
- Promotion: Automatically promotes a pawn to a queen if it reaches the last rank.
7. Handle Piece Drop
Create a function to handle piece drop events on the chessboard.
1const onPieceDrop = (sourceSquare: Square, targetSquare: Square) => {2const result = makeMove(sourceSquare, targetSquare);34if (result) {5channel.current.publish('new-move', {6sourceSquare,7targetSquare,8});9}1011return result;12};
Explanation:
- onPieceDrop: Handles the logic for when a piece is dropped on a new square, making the move and publishing it to the SuperViz channel if valid.
8. Handle Real-Time Messages
Create a function to handle incoming real-time messages for moves made by other players.
1const handleRealtimeMessage = useCallback((message: ChessMessageUpdate) => {2if (message.participantId === PLAYER_ID) return;34const { sourceSquare, targetSquare } = message.data;5makeMove(sourceSquare, targetSquare);6}, [makeMove]);
Explanation:
- handleRealtimeMessage: Listens for incoming move messages and updates the game state if the move was made by another participant.
9. Use Effect Hook for Initialization
Use the useEffect
hook to trigger the initialize
function on component mount.
1useEffect(() => {2initialize();3}, [initialize]);
Explanation:
- useEffect: Calls the
initialize
function once when the component mounts, setting up the SuperViz environment and real-time synchronization.
10. Render the Application
Return the JSX structure for rendering the application, including the chessboard and collaboration features.
1return (2<div className='w-full h-full bg-gray-200 flex items-center justify-center flex-col'>3<header className='w-full p-5 bg-purple-400 flex items-center justify-between'>4<h1 className='text-white text-2xl font-bold'>SuperViz Chess Game</h1>5</header>6<main className='w-full h-full flex items-center justify-center'>7<div className='w-[500px] h-[500px] shadow-sm border-2 border-gray-300 rounded-md'>8<Chessboard position={gameFen} onPieceDrop={onPieceDrop} />9<div className='w-[500px] h-[50px] bg-gray-300 flex items-center justify-center'>10<p className='text-gray-800 text-2xl font-bold'>Turn: {gameState.turn() === 'b' ? 'Black' : 'White'}</p>11</div>12</div>13</main>14</div>15);
Explanation:
- Header: Displays the title of the application.
- Chessboard: Renders the chessboard using the
Chessboard
component, withgameFen
as the position andonPieceDrop
as the event handler for piece drops. - Turn Indicator: Displays the current player's turn (Black or White).
Step 3: Understanding the Project Structure
Here's a quick overview of how the project structure supports a multiplayer chess game:
App.tsx
- Initializes the SuperViz environment.
- Sets up participant information and room details.
- Handles real-time synchronization for chess moves.
- Chessboard
- Displays the chessboard and manages piece movements.
- Integrates real-time communication to synchronize moves between players.
- Chess Logic
- Uses
chess.js
to manage game rules and validate moves. - Updates the game state and FEN string to reflect the current board position.
- Uses
Step 4: Running the Application
1. Start the React Application
To run your application, use the following command in your project directory:
1npm run dev
This command will start the development server and open your application in the default web browser. You can interact with the chessboard and see moves in real-time as other participants join the session.
2. Test the Application
- Real-Time Chess Moves: Open the application in multiple browser windows or tabs to simulate multiple participants and verify that moves made by one player are reflected in real-time for others.
- Collaborative Interaction: Test the responsiveness of the application by making moves and observing how the game state updates for all participants.
Summary
In this tutorial, we built a multiplayer chess game using SuperViz for real-time synchronization. We configured a React application to handle chess moves, enabling multiple players to collaborate seamlessly on a shared chessboard. This setup can be extended and customized to fit various scenarios where game interaction is required.
Feel free to explore the full code and further examples in the GitHub repository for more details.