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:
- AUTO_MATCHING (1): Waiting for players to join
- CONNECTING (2): Establishing connections between players
- 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 invitedINVITED(1): Invitation sent, awaiting responseJOINED(2): Participant successfully joinedDECLINED(3): Participant declined invitationLEFT(4): Participant left the roomFINISHED(5): Game session completedUNRESPONSIVE(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 createdonJoinedRoom: Successfully joined a roomonLeftRoom: Left a roomonRoomConnected: All players connected
Participant Events
onPeerJoined: New player joined the roomonPeerLeft: Player left the roomonPeerConnected: Player's connection establishedonPeerDisconnected: 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:
- JavaScript
- TypeScript
// 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);
import { RTMStandardParams, Room } from '@veniso/moitribe-js';
// Initialize RTM with typed callbacks
const rtmParams: RTMStandardParams = {
variant: 1,
max_automatch_players: 2,
onRoomCreated: (status: boolean, room: Room) => {
console.log('Room created:', room.roomID);
},
onJoinedRoom: (status: boolean, room: Room) => {
console.log('Joined room:', room.roomID);
},
onMessageReceived: (
messageData: ArrayBuffer,
senderID: string,
isReliable: boolean
) => {
// Handle incoming game data
const data = new Uint8Array(messageData);
console.log('Message from', senderID, data);
},
onPeerJoined: (room: Room, participantList: string[]) => {
console.log('New player joined:', participantList);
}
};
// Create a standard room
MoitribeSDK('my-game', 'createstandardroom', {
maxplayers: 2,
isprivate: false,
roomname: 'Quick Match'
}, rtmParams);
Next Steps
- Standard vs Endless Rooms - Choose the right room type for your game
- Standard Rooms - Learn about fixed player count rooms
- Endless Rooms - Explore dynamic player count rooms
- Messaging - Understand message delivery options
Start with Standard Rooms for turn-based or session-based games, and use Endless Rooms for continuous multiplayer experiences.