Leave or End Room - iOS
A participant can choose to leave the room without removing the other participants. This is typically done by Leave Room.
Alternatively, a participant can choose End Room, which removes all other participants and ends the session for everyone.
leave()
To leave the room without removing the other participants, call the leave() method of the Room class.
- It also works while the room is reconnecting. If you call it while
join()is still running, it cancels the join. - It throws
ERROR_ALREADY_IN_REQUESTED_STATEif you have already left or the room has ended, andERROR_ACTION_PERFORMED_BEFORE_ROOM_JOINEDif you have not joined. These errors are not sent toonError.
end()
To leave the room and remove all the other participants, call the end() method of the Room class.
- It throws
ERROR_ACTION_PERFORMED_BEFORE_ROOM_JOINEDif you are not in the room. - If the room could not be ended, it throws and you stay in the room.
end() can be called after the room is joined. Both methods are async throws, so call them with try await inside a Task.
Example
- Swift
@IBAction func leaveRoomButtonTapped(_ sender: Any) {
Task {
do {
// leave room
try await self.room?.leave()
} catch {
// Already left, or never joined
print("Failed to leave the room: \(error)")
}
}
}
@IBAction func endRoomButtonTapped(_ sender: Any) {
Task {
do {
// end room for everyone
try await self.room?.end()
} catch {
// The room was not ended
print("Failed to end the room: \(error)")
}
}
}
Events associated with Leave
The following callbacks are received when a participant leaves the room.
- Local Participant will receive a callback
onRoomLeftwith theLeaveReason. - All remote participants will receive a callback
onParticipantLeftwith theParticipantobject and theLeaveReason.
Events associated with End
The following callbacks are received when a participant ends the room.
- All remote participants and local participant will receive a callback on
onRoomLeftofRoomEventListener.
- Swift
extension RoomViewController: RoomEventListener {
func onRoomLeft(reason: LeaveReason) {
// remove listeners
room?.localParticipant.removeEventListener(self)
room?.removeEventListener(self)
print("You left the room: \(reason.message)")
}
func onParticipantLeft(_ participant: Participant, reason: LeaveReason) {
// remove listener
participant.removeEventListener(self)
print(participant.displayName, "just left the room:", reason.message)
}
}
API Reference
The API references for all the methods and events utilised in this guide are provided below.
Got a Question? Ask us on discord

