Skip to main content

Multiplayer Games Design

I'm building fun, multi-player games that players can play together competitively on-chain, or off-chain by connecting P2P directly through WebRTC without a server.

WebRTC Multiplayer​

In the off-chain mode, players can start immediately and seamlessly, just by connecting and playing with family and friends directly via WebRTC.

WebRTC Game Architecture

In this setup, one client acts as both the server (or 'host') and a player. This host is responsible for the entire game state and logic. All other clients connect directly to this host. The host's GameLogic component receives actions from all players (including itself), computes the next game state, and then broadcasts the update to everyone.

The server client (the host) will have three main components: Network, GameLogic, and GameUI. In contrast, the other clients only need the Network and GameUI components.

Alt text

  • Network: Sends and receives player actions and game state updates.
  • GameLogic (Host-only): Processes incoming actions to compute the next game state.
  • GameUI: Renders the current game state and captures player input (actions).

Here are the implementation logic, 1 codebase handle both host and client cases.

Alt text

This design applies on the current Bomb game. My next games will follow this closely.

WebRTC Over Central Server​

Below are the pros and cons of using WebRTC over a central server for implementing multiplayer games.

Pros

  • Single codebase: fast implement, test and deploy.
  • Anyone can host a game for others, with almost no cost, unlimited scalability.
  • Low latency (especially on the same local network).
  • Supports live streaming the game to millions via P2P connections.

Cons

  • Some network may not allow P2P connection.
  • Player may be able to cheat on the leaderboard.

Onchain Multiplayer​

I'm exploring some ideas for storing gameplay and results on-chain.

  • Smart contract game: Leverage session keys / sub-accounts for seamless on-chain experiences.

  • WebRTC multiplayer game: Only allow self-hosted gameplay and results for storing on-chain.