Join Live Stream - Flutter
Once, the live stream is configured, the next step is to join it. 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 which is the part of theRoomclass of Flutter SDK. - This method can be called after the live stream is initialized from the
VideoSDKclass. join()returns aFuturethat completes once the live stream is joined. If it cannot be joined, it throws aVideoSDKError, for example4002for an invalid token or3073for an empty stream id. A room whosejoin()failed cannot join again, so create a new room to retry.- On Android, the SDK does not ask for camera or microphone permission. A host should call
VideoSDK.requestPermissions()before joining, or the camera and microphone stay off.
Events associated with Join
Following callbacks are received when a participant is successfully joined.
- Local Participant will receive a
Events.roomJoinedevent, when successfully joined. This event can arrive afterjoin()completes.room.localParticipantis set when it fires, so read it in this event's handler. - Remote Participant will receive a
Events.participantJoinedevent with the newly joinedParticipantobject from the event callback.
import 'dart:async' show unawaited;
import 'package:flutter/material.dart';
import 'package:videosdk/videosdk.dart';
class LiveStreamScreen extends StatefulWidget {
...
}
class _LiveStreamScreenState extends State<LiveStreamScreen> {
late Room _room;
@override
void initState() {
super.initState();
//Creating a new Room based on the passed livestreamId and token from the Joining Screen
// create room
_room = VideoSDK.createRoom(
roomId: widget.livestreamId,
token: widget.token,
participantName: "John Doe",
micEnabled: true,
camEnabled: true,
defaultCamera: FacingMode.FRONT, // Camera the webcam starts on
);
//Calling Join after the room object is created
unawaited(joinLiveStream());
setupLiveStreamEventListener();
}
Future<void> joinLiveStream() async {
// On Android, the SDK does not ask for camera or microphone permission.
await VideoSDK.requestPermissions();
try {
await _room.join();
} on VideoSDKError catch (error) {
// For example 4002 (invalid token) or 3073 (empty stream id).
print("Join failed: ${error.code} ${error.name}");
}
}
@override
Widget build(BuildContext context) {
return YourLiveStreamWidget();
}
void setupLiveStreamEventListener() {
_room.on(Events.roomJoined, (){
// Room/Livestream joined Successfully
Participant localParticipant = _room.localParticipant;
});
_room.on(Events.participantJoined, (Participant participant){
// New Participant joined the livestream
});
}
}
leave()
- To leave the live stream you can call the
leave()method which is the part of theRoomclass of Flutter SDK. leave()returns aFuturethat throws aVideoSDKErrorwhen the SDK refuses it. Calling it after the live stream has been left throws3054.- To end the live stream for everyone, call
end()instead. It also returns aFuturethat throws aVideoSDKError, for example3035when the live stream has already been left.
These methods can be called only after the live stream is joined successfully.
After you leave
A room is single-use. Once Events.roomLeft is delivered, the room removes every event handler and releases itself, so there is nothing to dispose. A room that has left cannot join again: join() throws a StateError. Create a new room with VideoSDK.createRoom() for the next live stream.
Events associated with the leave() method
Following callbacks are received when a participant leaves the livestream.
- Local Participant will receive a callback on
Events.roomLeft, with aLeaveReasonthat says why.leave()can complete before this event arrives, so close your livestream screen from its handler. - All remote participants will receive a callback
Events.participantLeftwith theParticipantwho left and aLeaveReason.
import 'dart:async' show unawaited;
import 'package:flutter/material.dart';
import 'package:videosdk/videosdk.dart';
class LiveStreamScreen extends StatefulWidget {
...
}
class _LiveStreamScreenState extends State<LiveStreamScreen> {
late Room _room;
@override
void initState() {
super.initState();
setupLiveStreamEventListener();
}
Future<void> leaveLiveStream() async {
try {
await _room.leave();
} on VideoSDKError catch (error) {
// 3054: the live stream has already been left.
if (error.code != 3054) {
print("Leave failed: ${error.code} ${error.name}");
}
}
}
Future<void> endLiveStream() async {
try {
await _room.end();
} on VideoSDKError catch (error) {
print("End failed: ${error.code} ${error.name}");
}
}
@override
Widget build(BuildContext context) {
return Column(
children:[
ElevatedButton(
onPressed: leaveLiveStream,
child: const Text("Leave Livestream"),
),
ElevatedButton(
onPressed: endLiveStream,
child: const Text("End Livestream"),
),
]
);
}
void setupLiveStreamEventListener() {
_room.on(Events.roomLeft, (LeaveReason? reason){
// Room/Livestream left. Close the livestream screen here.
});
_room.on(Events.participantLeft, (Participant participant, LeaveReason reason){
// Participant left the Livestream
});
}
}
You should also call the leave() method when the user closes your main livestream widget without tapping Leave Livestream, for example with the back button, so that the livestream is left once the widget is closed. The Interactive Live Streaming Quick Start does this from a PopScope callback.
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

