Skip to main content

Join Room with Invitation Code

Join a Standard Room using an invitation code received from another player. This method is perfect for private matches where you want to play with specific friends or invited players.

Method Signature

MoitribeSDK(gameId, 'joinstandardroominvcode', params, callback)

Parameters

ParameterTypeRequiredDescription
invitationIDstringYesInvitation code from the room creator
onJoinedRoomfunctionYesCallback when successfully joined
onMessageReceivedfunctionYesCallback for incoming messages
onPeerJoinedfunctionYesCallback when other players join
onPeerLeftfunctionYesCallback when players leave
onLeftRoomfunctionYesCallback when you leave the room

Examples

Basic Room Join

Join a room using an invitation code:

const invitationCode = 'ABC123XYZ'; // Get this from the room creator

MoitribeSDK('my-game-id', 'joinstandardroominvcode', {
invitationID: invitationCode,
onJoinedRoom: (status, room) => {
if (status) {
console.log('Successfully joined room:', room.roomID);
console.log('Players in room:', room.participants.length);
// Start game logic here
} else {
console.error('Failed to join room');
}
},
onMessageReceived: (messageData, senderID, isReliable) => {
console.log('Message received from', senderID);
// Handle incoming game data
},
onPeerJoined: (room, participantList) => {
console.log('New player joined. Total:', participantList.length);
// Update UI to show new player
},
onPeerLeft: (room, participantList) => {
console.log('Player left. Total:', participantList.length);
// Handle player departure
},
onLeftRoom: (status, roomID) => {
console.log('You left the room:', roomID);
// Return to lobby or main menu
}
}, (result) => {
console.log('Join room request sent:', result);
});

Join with Error Handling

Join a room with comprehensive error handling:

function joinRoomWithCode(invitationCode) {
if (!invitationCode || invitationCode.trim() === '') {
console.error('Invalid invitation code');
return;
}

MoitribeSDK('my-game-id', 'joinstandardroominvcode', {
invitationID: invitationCode.trim(),
onJoinedRoom: (status, room) => {
if (status) {
console.log('✅ Joined room successfully');
console.log('Room ID:', room.roomID);
console.log('Creator:', room.creatorID);
console.log('Current players:', room.participants.length);

// Check if room is ready to start
if (room.status === 3) { // RoomStatus.ACTIVE
startGameplay(room);
} else {
showWaitingScreen(room);
}
} else {
console.error('❌ Failed to join room');
showErrorMessage('Unable to join the room. The invitation may be invalid or the room is full.');
}
},
onMessageReceived: (messageData, senderID, isReliable) => {
handleGameMessage(messageData, senderID);
},
onPeerJoined: (room, participantList) => {
updatePlayerList(participantList);
checkRoomReady(room);
},
onPeerLeft: (room, participantList) => {
updatePlayerList(participantList);
if (participantList.length < 2) {
pauseGameplay('Not enough players');
}
},
onLeftRoom: (status, roomID) => {
console.log('Left room:', roomID);
returnToLobby();
}
}, (result) => {
if (!result.success) {
console.error('Join request failed:', result.msg);
showErrorMessage(result.msg || 'Failed to join room');
}
});
}

// Usage
joinRoomWithCode('ABC123XYZ');

Response Format

The callback receives a response object:

{
success: boolean;
room?: Room; // Room object if successful
error?: string; // Error message if failed
}

Common Scenarios

Room Full

If the room is already at maximum capacity:

onJoinedRoom: (status, room) => {
if (!status) {
console.log('Room is full or no longer available');
// Show appropriate message to user
}
}

Invalid Invitation Code

If the invitation code is invalid or expired:

// In the main callback
(result) => {
if (!result.success) {
console.error('Invalid invitation code:', result.msg);
// Prompt user to check the code
}
}

Room Creator Left

If the room creator leaves after you join:

onPeerLeft: (room, participantList) => {
const creator = room.participants.find(p => p.playerID === room.creatorID);
if (!creator) {
console.log('Room creator left - room may close soon');
// Handle room closure
}
}

Error Handling

Common errors and their solutions:

ErrorCauseSolution
Invalid invitation codeCode is malformed or doesn't existVerify the code with the room creator
Room is fullMaximum players reachedTry joining a different room
Room not foundRoom was deleted or expiredGet a fresh invitation code
Player not authenticatedUser not logged inAuthenticate before joining
tip

Always validate the invitation code format before sending the join request. Most invitation codes are alphanumeric strings of 8-12 characters.

Best Practices

  • Validate Input: Check invitation code format before sending
  • Show Loading States: Display joining status while connecting
  • Handle All Callbacks: Implement all room event callbacks
  • Graceful Failures: Provide clear error messages for failed joins
  • Room State Management: Track room status changes appropriately
  • User Feedback: Keep users informed about connection status

Next Steps