Join Live Stream - iOS
Once the live stream is configured, the next step is to join it by calling the join() method. If you have not initialized the live stream yet, you can follow the guide here.
join()
- To join the live stream you can call the
join()method of theRoomclass. - This method can be called after the
Roomis created withVideoSDK.createRoom(). join()isasync throws, so call it withtry awaitinside aTask.
Events associated with the join() method
Following events are received when a participant successfully joins the livestream.
- Local Participant will receive an
onRoomJoinedevent, when successfully joined. - Remote Participant will receive an
onParticipantJoinedevent with the newly joinedParticipantobject from the event callback.
- Swift
func handleJoinStream() {
Task {
do {
try await room?.join()
} catch {
print("Failed to join the livestream: \(error)")
}
}
}
extension LiveStreamViewController: RoomEventListener {
// Event to know the room is joined
func onRoomJoined() {
print("You just joined the livestream")
}
// Event to know some other participant joined
func onParticipantJoined(_ participant: Participant) {
print("A new remote participant \(participant.displayName) just joined the livestream")
}
}
Keep these in mind when you call join():
- A second
join()throwsERROR_OPERATION_IN_PROGRESSwhile a join is running, andERROR_ALREADY_IN_REQUESTED_STATEonce you are in the live stream. - If the join fails, the room state changes to
FAILEDandjoin()throws the error. The failure is usually also sent toonError, so handle it in one place. - If you call
leave()while the join is still running, the join is cancelled andjoin()returns without throwing. TreatonRoomJoinedas the sign that you are in the live stream. - For a host,
join()returns before the mic and camera frommicEnabledandcamEnabledstart. Wait foronStreamEnabledbefore you treat local media as on.
leave()
- To leave the live stream you can call the
leave()method of theRoomclass. leave()isasync throws, so call it withtry awaitinside aTask.- It throws
ERROR_ALREADY_IN_REQUESTED_STATEif you have already left or the live stream has ended, andERROR_ACTION_PERFORMED_BEFORE_ROOM_JOINEDif you have not joined. These errors are not sent toonError.
This method can be called after the live stream is joined successfully. If you call it while join() is still running, it cancels the join.
Events associated with the leave() method
Following callbacks are received when a participant leaves the live stream.
- The Local Participant will receive the
onRoomLeftevent, once they have successfully left the livestream. - All the Remote Participants—including all the hosts and audience members—will receive the
onParticipantLeftevent with theParticipantobject of the user who left the livestream and theLeaveReason.
- Swift
func handleLeaveStream() {
Task {
do {
try await room?.leave()
} catch {
// Already left, or never joined
print("Failed to leave the livestream: \(error)")
}
}
}
extension LiveStreamViewController: RoomEventListener {
func onRoomLeft(reason: LeaveReason) {
// remove listeners
room?.localParticipant.removeEventListener(self)
room?.removeEventListener(self)
print("You left the livestream: \(reason)")
}
func onParticipantLeft(_ participant: Participant, reason: LeaveReason) {
// remove listener
participant.removeEventListener(self)
print(participant.displayName, "just left the livestream")
}
}
You should call the leave() method when your main live stream screen is closed, so that the live stream is left once the screen goes away.
API Reference
The API references for all the methods and events utilized in this guide are provided below.
Got a Question? Ask us on discord

