Skip to main content

Real-Time Multiplayer Overview

Moitribe's Real-Time Multiplayer (RTM) system enables you to build engaging multiplayer games with low-latency communication, automatic matchmaking, and flexible room management. The system uses MQTT for reliable real-time messaging and supports both fixed and variable player count rooms.

Architecture Overview

The RTM system is built on a room-based architecture where players join virtual rooms to communicate and play together. Each room maintains its own state and handles message routing between participants.

Core Components

  • MQTT Client: Handles real-time message delivery with guaranteed ordering
  • Room Management: Creates and manages multiplayer sessions
  • Message System: Supports both reliable and unreliable message delivery
  • Participant Management: Tracks player connections and room membership
  • Auto-Matching: Automatically matches players based on game requirements

Room Types

Moitribe RTM supports two distinct room types, each designed for different multiplayer scenarios:

Standard Rooms

Fixed player count rooms ideal for structured gameplay sessions:

  • Player Count: Fixed number of players (2-4 typical)
  • Matchmaking: Auto-matching with min/max player requirements
  • Session Lifecycle: Clear start and end points
  • Use Cases: Chess, card games, fighting games, puzzle games

Endless Rooms

Variable player count rooms for continuous gameplay:

  • Player Count: Dynamic, players can join/leave anytime
  • Drop-in/Drop-out: No session boundaries
  • Persistent State: Game continues regardless of player changes
  • Use Cases: MMO-style games, battle royale, persistent worlds

Message System

Message Types

The RTM system supports different message delivery modes:

  • Reliable Messages: Guaranteed delivery and ordering
  • Unreliable Messages: Faster delivery, no guarantees (ideal for position updates)
  • Server Messages: Communication with game server logic

Message Format

All game data is transmitted as ArrayBuffer for maximum performance:

// Message received callback signature
onMessageReceived?: (
messageData: ArrayBuffer,
senderParticipantID: string,
isReliable: boolean
) => void;

Room Lifecycle

Room States

Rooms progress through several states during their lifecycle:

  1. AUTO_MATCHING (1): Waiting for players to join
  2. CONNECTING (2): Establishing connections between players
  3. ACTIVE (3): Room is fully connected and ready for gameplay

Connection Flow

graph TD
A[Create/Join Room] --> B[AUTO_MATCHING]
B --> C[Players Joining]
C --> D[CONNECTING]
D --> E[P2P Connections]
E --> F[ACTIVE]
F --> G[Gameplay]
G --> H[Leave Room]

Participant Management

Each room tracks participants with detailed information:

interface Participant {
participantID: string; // Room-specific ID
playerID: string; // Global player ID
name: string; // Display name
iconImageURL: string; // Profile picture
status: ParticipantStatus; // Connection state
isConnected: boolean; // Current connection status
}

Participant Status

  • NOT_INVITED_YET (0): Participant not yet invited
  • INVITED (1): Invitation sent, awaiting response
  • JOINED (2): Participant successfully joined
  • DECLINED (3): Participant declined invitation
  • LEFT (4): Participant left the room
  • FINISHED (5): Game session completed
  • UNRESPONSIVE (6): Participant connection lost

Network Optimization

Latency Considerations

  • MQTT Protocol: Optimized for low-latency message delivery
  • Message Batching: Combine small messages to reduce overhead
  • Reliable vs Unreliable: Choose appropriate delivery for each message type
  • Connection Monitoring: Automatic reconnection with configurable timeouts

Performance Tips

  • Use unreliable messages for frequent updates (positions, animations)
  • Reserve reliable messages for critical game events (scores, game state changes)
  • Minimize message size by using efficient data encoding
  • Implement client-side prediction to reduce perceived latency

Callback System

The RTM system uses callbacks to notify your game of important events:

Room Events

  • onRoomCreated: Room successfully created
  • onJoinedRoom: Successfully joined a room
  • onLeftRoom: Left a room
  • onRoomConnected: All players connected

Participant Events

  • onPeerJoined: New player joined the room
  • onPeerLeft: Player left the room
  • onPeerConnected: Player's connection established
  • onPeerDisconnected: Player's connection lost

Message Events

  • onMessageReceived: New message received from another player

Quick Example

Here's a basic example of setting up RTM functionality:

// Initialize RTM with callbacks
const rtmParams = {
variant: 1,
max_automatch_players: 2,

onRoomCreated: (status, room) => {
console.log('Room created:', room.roomID);
},

onJoinedRoom: (status, room) => {
console.log('Joined room:', room.roomID);
},

onMessageReceived: (messageData, senderID, isReliable) => {
// Handle incoming game data
const data = new Uint8Array(messageData);
console.log('Message from', senderID, data);
},

onPeerJoined: (room, participantList) => {
console.log('New player joined:', participantList);
}
};

// Create a standard room
MoitribeSDK('my-game', 'createstandardroom', {
maxplayers: 2,
isprivate: false,
roomname: 'Quick Match'
}, rtmParams);

Next Steps

Best Practice

Start with Standard Rooms for turn-based or session-based games, and use Endless Rooms for continuous multiplayer experiences.