Skip to main content
Version: 5.x.x

Switch Live Stream - Flutter

Overview​

switchTo() moves the participant to another live stream without leave() and a new join.

switchTo()​

Parameters​

  • roomId (String, required): ID of the new liveStream to join
  • token (String, optional, named): VideoSDK token for the new liveStream. Without it, the SDK uses the token you joined with.

switchTo() returns a Future that throws a VideoSDKError when the SDK refuses the switch, for example 3073 (ERROR_INVALID_PARAMETER) for an empty id. When the move to the new liveStream completes:

  • Events.roomLeft fires with LeaveReason.SWITCH_ROOM for the liveStream you left.
  • Events.roomJoined fires again, with a Map whose switchRoomId is the id of the liveStream you switched to.
import 'package:flutter/material.dart';
import 'package:videosdk/videosdk.dart';

class LiveStreamView extends StatefulWidget {
final Room livestream;
final String token;

const LiveStreamView({
Key? key,
required this.livestream,
required this.token,
}) : super(key: key);

@override
State<LiveStreamView> createState() => _LiveStreamViewState();
}

class _LiveStreamViewState extends State<LiveStreamView> {
late Room _room;

@override
void initState() {
super.initState();
_room = widget.livestream;
registerEvents();
}

void registerEvents() {
_room.on(Events.roomLeft, _onRoomLeft);
_room.on(Events.roomJoined, _onRoomJoined);
}

void _onRoomLeft(LeaveReason? reason) {
// A switch also fires roomLeft, so keep the liveStream screen open.
if (reason == LeaveReason.SWITCH_ROOM) return;
// Close the liveStream screen here for any other reason.
}

void _onRoomJoined(Map<String, dynamic>? switchInfo) {
if (switchInfo != null) {
print("Switched to liveStream ${switchInfo['switchRoomId']}");
}
}

Future<void> _handleSwitchRoom() async {
try {
// token is optional
await _room.switchTo("<Desired - livestream-id>", token: widget.token);
} on VideoSDKError catch (e) {
print("Switch failed: ${e.code} ${e.name}");
}
}

@override
void dispose() {
_room.off(Events.roomLeft, _onRoomLeft);
_room.off(Events.roomJoined, _onRoomJoined);
super.dispose();
}

@override
Widget build(BuildContext context) {
return ElevatedButton(
child: const Text('Switch livestream'),
onPressed: () => _handleSwitchRoom(),
);
}
}

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.

Leave Handling​

  • Events.roomLeft also fires when you switch, with LeaveReason.SWITCH_ROOM. If your roomLeft handler closes the liveStream screen, skip it for this reason, as the example does. The room stays usable after a switch.

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 switchTo method.
    • Tokens that expire during a liveStream can cause unexpected disconnections.
  • 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