Create Endless Room
Create an endless room that allows players to join and leave dynamically. Endless rooms are perfect for casual games, persistent worlds, and drop-in/drop-out gameplay experiences.
Method
MoitribeSDK('game-id', 'createendlessroom', params, callback)
Parameters
| Parameter | Type | Required | Description |
|---|---|---|---|
variant | number | No | Room variant for different game modes |
max_players | number | No | Maximum players allowed (optional) |
invitedPlayerIDs | string[] | No | Array of player IDs to invite |
exclusive_bitmask | string | No | 8-bit mask for player roles |
onRoomCreated | function | Yes | Called when room is created |
onMessageReceived | function | Yes | Called when message is received |
onPeerJoined | function | Yes | Called when player joins |
onPeerLeft | function | Yes | Called when player leaves |
Response Format
{
success: boolean;
room?: Room;
msg?: string;
}
Examples
JavaScript Example
MoitribeSDK('my-game', 'createendlessroom', {
variant: 1,
max_players: 20,
onRoomCreated: (status, room) => {
if (status) {
console.log('Endless room created:', room.roomID);
console.log('Share this invitation code:', room.roomID);
} else {
console.error('Failed to create room');
}
},
onMessageReceived: (messageData, senderParticipantID, isReliable) => {
console.log('Message received from:', senderParticipantID);
// Handle incoming game data
},
onPeerJoined: (room, participantList) => {
console.log('Player joined. Total players:', participantList.length);
// Update UI with new player count
},
onPeerLeft: (room, participantList) => {
console.log('Player left. Total players:', participantList.length);
// Update UI with new player count
}
}, (result) => {
console.log('Create room request sent:', result);
});
TypeScript Example
import MoitribeSDK from '@veniso/moitribe-js';
MoitribeSDK('my-game', 'createendlessroom', {
variant: 1,
max_players: 20,
onRoomCreated: (status: boolean, room: Room) => {
if (status) {
console.log('Endless room created:', room.roomID);
console.log('Share this invitation code:', room.roomID);
} else {
console.error('Failed to create room');
}
},
onMessageReceived: (messageData: ArrayBuffer, senderParticipantID: string, isReliable: boolean) => {
console.log('Message received from:', senderParticipantID);
// Handle incoming game data
},
onPeerJoined: (room: Room, participantList: string[]) => {
console.log('Player joined. Total players:', participantList.length);
// Update UI with new player count
},
onPeerLeft: (room: Room, participantList: string[]) => {
console.log('Player left. Total players:', participantList.length);
// Update UI with new player count
}
}, (result: any) => {
console.log('Create room request sent:', result);
});
Room Configuration
Variants
Use variants to create different game modes within the same endless room type:
// Different game modes
const GAME_MODES = {
CASUAL: 1,
COMPETITIVE: 2,
TEAM_PLAY: 3
};
MoitribeSDK('my-game', 'createendlessroom', {
variant: GAME_MODES.CASUAL,
// ... other params
});
Player Roles
Use exclusive bitmask to assign specific roles to players:
// 8-bit mask for different roles
const ROLES = {
ADMIN: 1 << 0, // Bit 0
MODERATOR: 1 << 1, // Bit 1
PLAYER: 1 << 2 // Bit 2
};
MoitribeSDK('my-game', 'createendlessroom', {
exclusive_bitmask: ROLES.ADMIN.toString(),
// ... other params
});
Best Practices
Room Naming
While endless rooms don't require names, you can use custom data for identification:
MoitribeSDK('my-game', 'createendlessroom', {
variant: 1,
customdata: JSON.stringify({
gameMode: 'battle-royale',
map: 'island',
maxPlayers: 50
}),
// ... callbacks
});
Invitation System
The room ID serves as the invitation code. Share it with other players:
onRoomCreated: (status, room) => {
if (status) {
// Display invitation code to players
showInvitationCode(room.roomID);
// Or generate a shareable link
const shareLink = `https://mygame.com/join/${room.roomID}`;
showShareLink(shareLink);
}
}
Error Handling
Common errors and their solutions:
// Handle creation failures
onRoomCreated: (status, room) => {
if (!status) {
console.error('Room creation failed');
// Retry with different parameters
setTimeout(() => createRoomWithRetry(), 5000);
}
}
Important
Always implement proper error handling for room creation failures. Network issues or invalid parameters can cause creation to fail.
Next Steps
- Join Room - Learn how players join your room
- Send Messages - Implement real-time communication
- Room Callbacks - Handle all room events
- Leave Room - Properly exit rooms