Using the Huddle 01 Flutter plugin you can build and get started with a simple p2p video chat application in minutes! Follow along this guide to know how:
In this documentation we use flutter_bloc package for state management.
4. Setup event listeners
Here we setup event emitters that are triggered at certain specific events from the package. These help with the global state management of the application.
_setupEventListeners({requiredConsumersBloc consumersBloc,requiredProducersBloc producersBloc,requiredPeersBloc peersBloc,requiredMeBloc meBloc, }) {// on adding a new consumer emitter.on('addConsumer', (consumer) { consumersBloc.add(ConsumerAdd(consumer: consumer)); });// on removing an existing consumer emitter.on('removeConsumer', (consumerId) { consumersBloc.add(ConsumerRemove(consumerId: consumerId)); });// adding a new producer emitter.on('addProducer', (producer) { producersBloc.add(ProducerAdd(producer: producer));if (producer.source =='webcam') { meBloc.add(MeSetWebcamInProgress(progress:true)); } });// disable and remove an existing producer emitter.on('removeProducer', (source) { producersBloc.add(ProducerRemove(source: source));if (source =='webcam') { meBloc.add(MeSetWebcamInProgress(progress:false)); } });// adding a new peer emitter.on('addPeer', (peer) { peersBloc.add(PeerAdd(newPeer: peer)); });// removing an existing peer emitter.on('removePeer', (peerId) { peersBloc.add(PeerRemove(peerId: peerId)); });// adding new consumer of the selected peer emitter.on('addPeerConsumer', (consumer) { peersBloc.add(PeerAddConsumer(peerId: consumer.peerId, consumerId: consumer.id)); });// removing an existing consumer of a selected peer emitter.on('removePeerConsumer', (peerId, consumerId) { peersBloc.add(PeerRemoveConsumer(peerId: peerId, consumerId: consumerId)); }); }
5. Initialising Client repository and using available methods:
Client repository initialisation using RepositoryProvider and using join() to join the room.
child:RepositoryProvider<HuddleClientRepository>(// provider to provide room client as an object to all the child widgets of this lazy:false, create: (context) {// setup event listeners_setupEventListeners( consumersBloc: context.read<ConsumersBloc>(), producersBloc: context.read<ProducersBloc>(), peersBloc: context.read<PeersBloc>(), meBloc: context.read<MeBloc>(), );// initialise blocsMediaDevicesBloc mediaDevicesBloc = context.read<MediaDevicesBloc>();String audioInputDeviceId = mediaDevicesBloc.state.selectedAudioInput!.deviceId;String videoInputDeviceId = mediaDevicesBloc.state.selectedVideoInput!.deviceId;final meState = context.read<MeBloc>().state;String displayName = meState.displayName;String id = meState.id;final roomState = context.read<RoomBloc>().state;// url for connection, uses Huddle 01's alpha server link in room stateString url = roomState.url!;Uri uri =Uri.parse(url);returnHuddleClientRepository( peerId: id, displayName: displayName, url: url !=null?'wss://${uri.host}:4443':'wss://alpha.huddle01.com:4443', roomId: uri.queryParameters['roomId'] ?? uri.queryParameters['roomid'] ??randomAlpha(8).toLowerCase(), audioInputDeviceId: audioInputDeviceId, videoInputDeviceId: videoInputDeviceId, )..join();// using join() to join room with the given config },// Room is the route of the application where the video chat takes place child:Room(),)
Other methods available:
close()
context.read<HuddleClientRepository>().close();
enableWebcam()
disableWebcam()
// enable webcam while initialising room to ask permissioncontext.read<HuddleClientRepository>().enableWebcam();
enableMic()
disableMic()
muteMic()
unmuteMic()
// enable mic while initialising room room to ask permissioncontext.read<HuddleClientRepository>().enableMic();// mute and unmute miccontext.read<HuddleClientRepository>().unmuteMic();
For any help, reach out to us on Slack. We are available 24*7 at: https://bit.ly/3AsIsT7.