A production-ready multiplayer game template that enables developers to quickly deploy and customize real-time multiplayer games using ReactJS, Node.js, and Socket.IO.
- Features
- Architecture Overview
- Getting Started
- Creating Your Own Game
- Deployment on Railway
- Advanced Customization
- Troubleshooting
- File Structure Guide
- Modular Game Engine: Abstract base classes for game logic with easy extension
- Dynamic Component Registry: Frontend components are loaded based on game type
- Game Server: Node.js backend with Socket.IO for real-time communication
- Game Client: React-based frontend with responsive design
- Room System: Support for multiple concurrent game rooms
- Basic Authentication: Username-based login with session persistence
- Game Lobby: Room listing, creation, and joining functionality
- In-Game Chat: Text chat functionality within game rooms
- Reconnection Logic: Ability to rejoin ongoing games after disconnection
- Turn-Based Gameplay: Simple card/board game mechanics (Tic-tac-toe)
- Enhanced authentication with user accounts
- Matchmaking system
- Game history and statistics
- Advanced game mechanics options
- Spectator mode
- Tournament functionality
- AI opponents
- Custom game creation with rule modifications
The template follows a client-server architecture with clear separation of concerns:
- Game Engine Abstraction: The
GameEnginebase class defines the interface for all game engines - Game Registry: The
GameRegistrymanages game types and creates game engine instances - Socket Handlers: Handle real-time communication between clients and server
- Room Management: Manages game rooms, players, and game state
- React Components: UI components for lobby, game room, and game boards
- Context API: Manages global state for authentication and socket connections
- Game Component Registry: Maps game types to their corresponding React components
- Socket.IO Client: Handles real-time communication with the server
- Node.js (v14+)
- npm or yarn
- Git
- Clone the repository
- Install dependencies:
# Install server dependencies cd server npm install # Install client dependencies cd ../client npm install - Set up environment variables (copy
.env.exampleto.envin both client and server directories) - Start the development servers:
# Start server cd server npm run dev # Start client in another terminal cd client npm start - Open your browser and navigate to
http://localhost:3000
The template is designed to make it easy to add new games. Here's how to create your own game:
- Create a new file in
server/src/game/(e.g.,MyGameEngine.js) - Extend the
GameEnginebase class and implement required methods:
const GameEngine = require('./GameEngine');
class MyGameEngine extends GameEngine {
constructor(config = {}) {
super({
// Default configuration
...config
});
this.type = 'my-game'; // Unique identifier for your game
}
// Initialize a new game
initializeGame(players) {
// Create initial game state
return {
type: this.type,
// Your game-specific state
players: players.map((player, index) => ({
...player,
// Add game-specific player properties
})),
currentTurn: players[0].userId,
status: 'playing',
// Other game-specific properties
startedAt: new Date().toISOString()
};
}
// Process a move
processMove(gameState, move, playerId) {
// Validate and process the move
// Return updated game state
}
// Check if the game is over
checkGameOver(gameState) {
// Check win/loss conditions
// Return winner information
}
// Get valid moves for a player
getValidMoves(gameState, playerId) {
// Return array of valid moves
}
// Validate a move
isValidMove(gameState, move, playerId) {
// Return true if move is valid
}
// Get the next player's turn
getNextTurn(gameState, currentPlayerId) {
// Return ID of next player
}
}
module.exports = MyGameEngine;Add your game engine to the GameRegistry in server/src/game/GameRegistry.js:
// In the constructor
constructor() {
this.engines = new Map();
this.configs = new Map();
// Register default games
this.registerGame('tic-tac-toe', TicTacToeEngine, {
displayName: 'Tic-tac-toe',
description: 'Classic 3x3 tic-tac-toe game',
minPlayers: 2,
maxPlayers: 2,
boardSize: 3,
winCondition: 3
});
// Register your game
this.registerGame('my-game', MyGameEngine, {
displayName: 'My Game',
description: 'Description of my game',
minPlayers: 2,
maxPlayers: 4, // Adjust as needed
// Game-specific configuration
});
}- Create a new file in
client/src/components/games/(e.g.,MyGameBoard.js) - Implement your game board component:
import React from 'react';
import styled from 'styled-components';
// Styled components for your game
const GameContainer = styled.div`
// Your styling
`;
// Your game board component
const MyGameBoard = ({ game, onMove, currentPlayer }) => {
// Game rendering and interaction logic
const handleMove = (moveData) => {
// Call onMove with move data
onMove(moveData);
};
return (
<GameContainer>
{/* Your game UI */}
</GameContainer>
);
};
export default MyGameBoard;Add your component to the game component registry in client/src/components/games/index.js:
import TicTacToeBoard from './TicTacToeBoard';
import MyGameBoard from './MyGameBoard';
/**
* Game component registry
* Maps game types to their respective React components
*/
const gameComponents = {
'tic-tac-toe': TicTacToeBoard,
'my-game': MyGameBoard,
};- Start both the client and server
- Create a new room with your game type
- Join the room and test your game functionality
The template is configured for easy deployment on Railway:
-
Create a Railway account at railway.app
-
Install the Railway CLI:
npm i -g @railway/cli
-
Login to Railway:
railway login
-
Initialize your project:
railway init
-
Add environment variables in the Railway dashboard
-
Deploy your application:
railway up
-
Open your deployed application:
railway open
The Lobby component (client/src/components/Lobby.js) can be customized to add additional features:
- Game filtering
- Room search
- Private rooms
- Custom room settings
The template uses a simple username-based authentication system. To add more robust authentication:
- Modify the
AuthContext.jsfile to integrate with your authentication provider - Update the server's session handling in
server/src/socket/index.js - Add authentication middleware to protect routes
The template uses in-memory storage for rooms and sessions. To add persistence:
- Add a database connection (MongoDB, PostgreSQL, etc.)
- Create models for rooms, games, and users
- Update the socket handlers to use the database instead of in-memory maps
- Check that the server is running
- Verify that the client is connecting to the correct server URL
- Check for CORS issues in the server configuration
- Ensure environment variables are properly set
- Ensure that game state updates are properly broadcasted to all clients
- Check that the game engine is correctly processing moves
- Verify that the client is correctly handling game state updates
- Check for race conditions in socket event handling
- Check environment variables in Railway dashboard
- Verify that the build process is successful
- Check for port conflicts
- Review Railway logs for any errors
- Use
console.logstatements to debug server-side issues - Use React DevTools to inspect component state
- Check the browser console for client-side errors
- Use Socket.IO's debug mode to trace socket events
Here's a guide to the key files in the template and whether they should be modified when creating your own game:
| File | Purpose | Modify? |
|---|---|---|
server/src/index.js |
Server entry point | No |
server/src/socket/index.js |
Socket.IO event handlers | No |
server/src/socket/gameHandlers.js |
Game event handlers | No |
server/src/game/GameEngine.js |
Base game engine class | No |
server/src/game/GameRegistry.js |
Game registry | Yes (to register new games) |
server/src/game/TicTacToeEngine.js |
Tic-tac-toe implementation | Use as reference |
server/src/game/YourGameEngine.js |
Your custom game | Create new |
| File | Purpose | Modify? |
|---|---|---|
client/src/App.js |
Main application component | No |
client/src/contexts/AuthContext.js |
Authentication context | No |
client/src/contexts/SocketContext.js |
Socket connection context | No |
client/src/components/Lobby.js |
Lobby component | No |
client/src/components/GameRoom.js |
Game room component | No |
client/src/components/ChatBox.js |
Chat component | No |
client/src/components/games/index.js |
Game component registry | Yes (to register new games) |
client/src/components/games/TicTacToeBoard.js |
Tic-tac-toe board component | Use as reference |
client/src/components/games/YourGameBoard.js |
Your custom game board | Create new |
multiplayer-game-template/
├── client/ # React frontend
│ ├── public/ # Static assets
│ └── src/ # Source code
│ ├── components/ # UI components
│ │ ├── games/ # Game-specific components
│ │ │ ├── index.js # Game component registry
│ │ │ └── TicTacToeBoard.js # Tic-tac-toe implementation
│ │ ├── ChatBox.js # In-game chat component
│ │ ├── GameBoard.js # Generic game board wrapper
│ │ ├── GameRoom.js # Game room component
│ │ ├── Home.js # Home/landing page
│ │ └── Lobby.js # Game lobby component
│ ├── contexts/ # React contexts
│ │ ├── AuthContext.js # Authentication context
│ │ └── SocketContext.js # Socket.IO context
│ ├── App.js # Main application component
│ └── index.js # Entry point
└── server/ # Node.js backend
└── src/
├── game/ # Game logic
│ ├── GameEngine.js # Abstract base class
│ ├── GameRegistry.js # Game type registry
│ └── TicTacToeEngine.js # Tic-tac-toe implementation
├── socket/ # Socket.IO handlers
│ ├── gameHandlers.js # Game-specific socket handlers
│ └── index.js # Main socket handler
└── index.js # Server entry point
The template is designed to provide an excellent developer experience:
- Modular Architecture: Clear separation of concerns makes it easy to understand and extend
- Hot Reloading: Changes to both client and server code are automatically reloaded during development
- Typed Documentation: Comprehensive JSDoc comments help understand the codebase
- Consistent Patterns: Similar patterns are used throughout the codebase for consistency
- Error Handling: Robust error handling with clear error messages
- Responsive Design: The UI works well on both desktop and mobile devices
MIT
Contributions are welcome! Please feel free to submit a Pull Request.