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
| Parameter | Type | Required | Description |
|---|---|---|---|
invitationID | string | Yes | Invitation code from the room creator |
onJoinedRoom | function | Yes | Callback when successfully joined |
onMessageReceived | function | Yes | Callback for incoming messages |
onPeerJoined | function | Yes | Callback when other players join |
onPeerLeft | function | Yes | Callback when players leave |
onLeftRoom | function | Yes | Callback when you leave the room |
Examples
Basic Room Join
Join a room using an invitation code:
- JavaScript
- TypeScript
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);
});
import MoitribeSDK from '@veniso/moitribe-js';
import type { Room, RTMStandardParams } from '@veniso/moitribe-js';
const invitationCode = 'ABC123XYZ'; // Get this from the room creator
const params: RTMStandardParams = {
invitationID: invitationCode,
onJoinedRoom: (status: boolean, room: 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: ArrayBuffer, senderID: string, isReliable: boolean) => {
console.log('Message received from', senderID);
// Handle incoming game data
},
onPeerJoined: (room: Room, participantList: string[]) => {
console.log('New player joined. Total:', participantList.length);
// Update UI to show new player
},
onPeerLeft: (room: Room, participantList: string[]) => {
console.log('Player left. Total:', participantList.length);
// Handle player departure
},
onLeftRoom: (status: boolean, roomID: string) => {
console.log('You left the room:', roomID);
// Return to lobby or main menu
}
};
MoitribeSDK('my-game-id', 'joinstandardroominvcode', params, (result) => {
console.log('Join room request sent:', result);
});
Join with Error Handling
Join a room with comprehensive error handling:
- JavaScript
- TypeScript
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');
import MoitribeSDK from '@veniso/moitribe-js';
import type { Room, RTMStandardParams, RoomStatus } from '@veniso/moitribe-js';
function joinRoomWithCode(invitationCode: string): void {
if (!invitationCode || invitationCode.trim() === '') {
console.error('Invalid invitation code');
return;
}
const params: RTMStandardParams = {
invitationID: invitationCode.trim(),
onJoinedRoom: (status: boolean, room: 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 === 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: ArrayBuffer, senderID: string, isReliable: boolean) => {
handleGameMessage(messageData, senderID);
},
onPeerJoined: (room: Room, participantList: string[]) => {
updatePlayerList(participantList);
checkRoomReady(room);
},
onPeerLeft: (room: Room, participantList: string[]) => {
updatePlayerList(participantList);
if (participantList.length < 2) {
pauseGameplay('Not enough players');
}
},
onLeftRoom: (status: boolean, roomID: string) => {
console.log('Left room:', roomID);
returnToLobby();
}
};
MoitribeSDK('my-game-id', 'joinstandardroominvcode', params, (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:
| Error | Cause | Solution |
|---|---|---|
Invalid invitation code | Code is malformed or doesn't exist | Verify the code with the room creator |
Room is full | Maximum players reached | Try joining a different room |
Room not found | Room was deleted or expired | Get a fresh invitation code |
Player not authenticated | User not logged in | Authenticate 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