Join Endless Room
Join an existing endless room using an invitation code. This allows players to drop into ongoing game sessions and participate with other players who are already in the room.
Method
MoitribeSDK('game-id', 'joinendlessroominvcode', params, callback)
Parameters
| Parameter | Type | Required | Description |
|---|---|---|---|
invitationID | string | Yes | Room invitation code to join |
onJoinedRoom | function | Yes | Called when successfully joined |
onMessageReceived | function | Yes | Called when message is received |
onPeerJoined | function | Yes | Called when player joins |
onPeerLeft | function | Yes | Called when player leaves |
onConnectedToRoom | function | Yes | Called when connected to room |
Response Format
{
success: boolean;
room?: Room;
msg?: string;
}
Examples
JavaScript Example
MoitribeSDK('my-game', 'joinendlessroominvcode', {
invitationID: 'ABC123XYZ789',
onJoinedRoom: (status, room) => {
if (status) {
console.log('Successfully joined room:', room.roomID);
console.log('Current players:', room.participants.length);
console.log('Room variant:', room.variant);
} else {
console.error('Failed to join room');
}
},
onMessageReceived: (messageData, senderParticipantID, isReliable) => {
console.log('Message received from:', senderParticipantID);
// Process incoming game data
},
onPeerJoined: (room, participantList) => {
console.log('New player joined. Total:', participantList.length);
// Update player list UI
},
onPeerLeft: (room, participantList) => {
console.log('Player left. Total:', participantList.length);
// Update player list UI
},
onConnectedToRoom: (room) => {
console.log('Connected to room, ready to play');
// Enable game controls
}
}, (result) => {
console.log('Join room request sent:', result);
});
TypeScript Example
import MoitribeSDK from '@veniso/moitribe-js';
MoitribeSDK('my-game', 'joinendlessroominvcode', {
invitationID: 'ABC123XYZ789',
onJoinedRoom: (status: boolean, room: Room) => {
if (status) {
console.log('Successfully joined room:', room.roomID);
console.log('Current players:', room.participants.length);
console.log('Room variant:', room.variant);
} else {
console.error('Failed to join room');
}
},
onMessageReceived: (messageData: ArrayBuffer, senderParticipantID: string, isReliable: boolean) => {
console.log('Message received from:', senderParticipantID);
// Process incoming game data
},
onPeerJoined: (room: Room, participantList: string[]) => {
console.log('New player joined. Total:', participantList.length);
// Update player list UI
},
onPeerLeft: (room: Room, participantList: string[]) => {
console.log('Player left. Total:', participantList.length);
// Update player list UI
},
onConnectedToRoom: (room: Room) => {
console.log('Connected to room, ready to play');
// Enable game controls
}
}, (result: any) => {
console.log('Join room request sent:', result);
});
Invitation Code Format
Invitation codes are typically the room ID itself. They can be:
- Alphanumeric:
ABC123XYZ789 - Short codes:
XYZ789 - URL-friendly: Can be used in shareable links
// Example of creating a shareable link
function createShareableLink(invitationCode) {
return `https://mygame.com/join/${invitationCode}`;
}
// Usage
const link = createShareableLink('ABC123XYZ789');
console.log('Share this link:', link);
Join Flow
1. Validation
The SDK validates the invitation code before attempting to join:
// Invalid invitation code handling
MoitribeSDK('my-game', 'joinendlessroominvcode', {
invitationID: '', // Empty or invalid
onJoinedRoom: (status, room) => {
if (!status) {
console.error('Invalid invitation code');
showErrorMessage('Please check the invitation code');
}
}
});
2. Connection Process
onJoinedRoom: (status, room) => {
if (status) {
console.log('Join successful!');
console.log('Room details:', {
id: room.roomID,
creator: room.creatorID,
players: room.participants.length,
variant: room.variant
});
// Load current game state
syncWithRoomState(room);
}
}
3. Player Integration
onConnectedToRoom: (room) => {
console.log('Fully connected to room');
// Announce player arrival
sendMessageToAll({
type: 'player_joined',
player: getCurrentPlayerInfo()
});
// Request current game state
sendMessageToServer({
type: 'request_state'
});
}
Error Handling
Common Join Errors
onJoinedRoom: (status, room) => {
if (!status) {
// Handle different failure scenarios
if (room?.msg?.includes('not found')) {
showError('Room not found or expired');
} else if (room?.msg?.includes('full')) {
showError('Room is full');
} else {
showError('Failed to join room');
}
}
}
Retry Logic
function joinRoomWithRetry(invitationCode, maxRetries = 3) {
let attempts = 0;
function attemptJoin() {
attempts++;
MoitribeSDK('my-game', 'joinendlessroominvcode', {
invitationID: invitationCode,
onJoinedRoom: (status, room) => {
if (status) {
console.log('Successfully joined!');
return;
}
if (attempts < maxRetries) {
console.log(`Retrying... (${attempts}/${maxRetries})`);
setTimeout(attemptJoin, 2000);
} else {
console.error('Failed to join after max retries');
}
}
});
}
attemptJoin();
}
Best Practices
Pre-Join Validation
function validateInvitationCode(code) {
// Basic validation
if (!code || code.length < 6) {
return false;
}
// Format validation (adjust based on your code format)
return /^[A-Z0-9]{6,12}$/.test(code);
}
// Usage
const invitationCode = getUserInput();
if (validateInvitationCode(invitationCode)) {
joinRoom(invitationCode);
} else {
showError('Invalid invitation code format');
}
Loading States
function joinRoomWithLoading(invitationCode) {
showLoadingIndicator('Joining room...');
MoitribeSDK('my-game', 'joinendlessroominvcode', {
invitationID: invitationCode,
onJoinedRoom: (status, room) => {
hideLoadingIndicator();
if (status) {
showSuccessMessage('Joined room successfully!');
} else {
showErrorMessage('Failed to join room');
}
}
});
}
Next Steps
- Send Messages - Communicate with other players
- Receive Messages - Handle incoming communications
- Room Callbacks - Manage all room events
- Leave Room - Exit the room properly
Pro Tip
Store the invitation code locally to allow quick reconnection if the player disconnects accidentally.