Switch Live Stream - React Native
note
This page uses the following asynchronous methods. Refer to their API reference for the errors each Promise may reject with, and handle these rejections appropriately based on your use case.
Overview
switchTo() moves the participant to another live stream without calling leave() and joining again.
switchTo()
Parameters
- roomId (
string, required): ID of the new liveStream to join. Pass it as the first argument. - token (
string, optional): VideoSDK token for the new liveStream. Leave it out to reuse the token of the current liveStream.
When the switch completes, onRoomJoined of the useRoom hook fires again with { messages, switchRoomId }. switchRoomId is the ID of the liveStream switchTo() landed in, and null for an ordinary join.
If the server refuses the switch, the call rejects with SWITCH_ROOM_FAILED (4076).
import { useRoom } from "@videosdk.live/react-native-sdk";
import { View, TouchableOpacity, Text } from "react-native";
const LiveStreamView = () => {
//Getting the switchTo method from hook
const { switchTo } = useRoom({
onRoomJoined: ({ switchRoomId }) => {
if (switchRoomId) console.log("Switched to", switchRoomId);
},
});
let liveStreamId = "abcd-pqrs-xyzw";
// Token for the new liveStream, generated by your server. Optional: leave it out to reuse the current token.
let token = "<token-for-new-livestream>";
const handleSwitch = async () => {
// Switching to new liveStream
try {
await switchTo(liveStreamId, token);
} catch (err) {
console.error("switchTo failed:", err);
}
};
return (
<View>
<TouchableOpacity onPress={handleSwitch}>
<Text>Switch LiveStream</Text>
</TouchableOpacity>
</View>
);
};
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

