Switch Live Stream - iOS
Overview
The Switch Live Stream feature moves users from one live stream to another with a single call, without leaving the current live stream and joining the next one.
switchTo()
Parameters
- roomId (
String, required): ID of the new liveStream to join - token (
String?, optional): VideoSDK token for the new liveStream. Passnilto reuse the current liveStream's token
You keep your current mode in the new liveStream. To change it, call changeMode(_:) after the switch.
switchTo() is async throws, so call it with try await inside a Task. It throws a VideoSDKError when the switch fails, for example:
ERROR_INVALID_PARAMETERwhenroomIdis empty.ERROR_ACTION_PERFORMED_BEFORE_ROOM_JOINEDbefore you join the current liveStream, or after you leave it.ERROR_ROOM_RECONNECTINGwhile the current liveStream is reconnecting. Call it again after it reconnects.
@IBAction func switchToLiveStream(_ sender: UIButton) {
Task {
do {
try await room?.switchTo(roomId: "ROOM_ID", token: "YOUR_TOKEN")
} catch let error as VideoSDKError {
print("Switch failed: \(error.name) (\(error.code)): \(error.message)")
} catch {
print("Switch failed: \(error)")
}
}
}
Events associated with switchTo()
- When the switch completes,
onRoomJoined(switchRoomId:)is called with the ID of the new liveStream. On a regular join,switchRoomIdisnil. - If you implement
onRoomJoined(switchRoomId:), the SDK calls it instead ofonRoomJoined()for every join.
extension LiveStreamViewController: RoomEventListener {
func onRoomJoined(switchRoomId: String?) {
if let switchRoomId {
print("Switched to liveStream: \(switchRoomId)")
}
}
}
Important Considerations
Room Status
- The destination liveStream must be active before attempting to switch.
- If the destination liveStream is inactive or doesn't exist, the switch will fail.
Token Management
- Token Expiry: Be careful with short-lived tokens when using the switch feature.
- If you're using short-lived tokens for security reasons, always provide a new token in the
switchTomethod. - Tokens that expire during a liveStream can cause unexpected disconnections.
- If you're using short-lived tokens for security reasons, always provide a new token in the
- Best Practice: For frequent switches, use tokens with reasonable expiry times or implement a token refresh mechanism.
API Reference
Got a Question? Ask us on discord

