100ms Logo

100ms

Docs

Search docs
/

Join Room

Overview

To join and interact with others in audio or video call, the user needs to join a room.

When a user indicates that they want to join the room, your app should have -

  1. User Name - The name which should be displayed to other peers in the room.
  2. Auth Token - The Client side Authentication Token generated by the Token Service.

You can also optionally pass these fields -

  1. Track Settings - Such as joining a Room with Muted Audio or Video using the HMSTrackSetting object. More information available here

  2. User Metadata - This can be used to pass any additional metadata associated with the user using metadata of HMSConfig object. For Eg: user-id mapping at the application side. More information is available here.

Join a Room

This section will take you through a sample code to join a room and receive room updates:

class Meeting implements HMSUpdateListener{ late HMSSDK hmsSDK; Meeting() { initHMSSDK(); } void initHMSSDK() async { hmsSDK = HMSSDK(); await hmsSDK.build(); // ensure to await while invoking the `build` method hmsSDK.addUpdateListener(this); HMSConfig config = HMSConfig(authToken: 'eyJH5c', // client-side token generated from your token service userName: 'John Appleseed'); hmsSDK.join(config: config); } void onJoin({required HMSRoom room}) async { // User Joined successfully. } void onRoomUpdate({required HMSRoom room, required HMSRoomUpdate update}) { //Room updates: https://www.100ms.live/docs/flutter/v2/features/update-listener-enums#hms-room-update } void onPeerUpdate( {required HMSPeer peer, required HMSPeerUpdate update}) async { //Peer updates: https://www.100ms.live/docs/flutter/v2/features/update-listener-enums#hms-peer-update } void onTrackUpdate( {required HMSTrack track, required HMSTrackUpdate trackUpdate, required HMSPeer peer}) { //Track updates: https://www.100ms.live/docs/flutter/v2/features/update-listener-enums#hms-track-update } void onHMSError({required HMSException error}) { //Error updates: https://www.100ms.live/docs/flutter/v2/features/error-handling#hms-exception } void onMessage({required HMSMessage message}) { //Message updates: https://www.100ms.live/docs/flutter/v2/features/chat#receiving-chat-messages } void onRoleChangeRequest({required HMSRoleChangeRequest roleChangeRequest}) { //role change request: https://www.100ms.live/docs/flutter/v2/features/change-role#accept-role-change-request } void onUpdateSpeakers({required List<HMSSpeaker> updateSpeakers}) { //This is triggered every second with a list of speakers who are currently speaking. } void onReconnecting() { //Reconnection callback: https://www.100ms.live/docs/flutter/v2/features/reconnection-handling#reconnecting-and-reconnected-callbacks } void onReconnected() { //Reconnection callback: https://www.100ms.live/docs/flutter/v2/features/reconnection-handling#reconnecting-and-reconnected-callbacks } void onChangeTrackStateRequest({required HMSTrackChangeRequest hmsTrackChangeRequest}) { //Getting mute-unmute callback: https://www.100ms.live/docs/flutter/v2/features/remote-mute-unmute#handling-an-unmute-callback } void onRemovedFromRoom({required HMSPeerRemovedFromPeer hmsPeerRemovedFromPeer}) { //Handle peer removal case: https://www.100ms.live/docs/flutter/v2/features/remove-peer#handling-the-remove-peer-callback } void onAudioDeviceChanged( {HMSAudioDevice? currentAudioDevice, List<HMSAudioDevice>? availableAudioDevice}) { //Get notified when the audio device is changed: https://www.100ms.live/docs/flutter/v2/features/audio-output-routing#adding-audio-device-change-event-listener-android-only } }

Step-by-step guide

This section will take you through the join journey step by step :

Create a class implementing HMSUpdateListener

Let's create a class that implements HMSUpdateListener and acts as a data source for our UI

class Meeting implements HMSUpdateListener{}

Create HMSSDK instance

Now, create an instance of HMSSDK using the build method. The build should always be called with await to ensure correct initialization & setup of the 100ms SDK.

HMSSDK hmsSDK = HMSSDK(); await hmsSDK.build(); // ensure to await while invoking the `build` method

Attach HMSUpdateListener

The methods of HMSUpdateListener are invoked to notify updates happening in the room like as soon as join is successful we get onJoin callback. So, to get these updates we need to attach the listener as:

//`this` value corresponds to the instance implementing HMSUpdateListener hmsSDK.addUpdateListener(this);

Create HMSConfig object

Next, create an object of HMSConfig class using the available join configurations.

HMSConfig config = HMSConfig(authToken: 'eyJH5c', // client-side token generated from your token service userName: 'John Appleseed');

Invoke Join

Now, we are primed to join the room. All you have to do is call join by passing the config object.

hmsSDK.join(config: config);

The methods of HMSUpdateListener are invoked to notify updates happening in the room like a peer joins/leaves, a track mute/unmute, etc.

After calling join your app will be provided an update from the 100ms SDK.

If successful, the onJoin({required HMSRoom room}) method of HMSUpdateListener will be invoked with information about the room encapsulated in the HMSRoom object.

If failure, the onHMSError({required HMSException error}) method will be invoked with exact failure reason.

Once you get onJoin callback you have joined a room successfully 🥳

Join with Muted Audio / Video

Joining with Muted Audio / Video is a customization that sets the microphone and camera state before joining the room.

By default HMSSDK keeps the microphone and camera ON but by using this feature you can decide their state according to your use case.

This can be achieved using the hmsVideoTrackInitState property for Video and hmsAudioTrackInitState for Audio in the hmsTrackSetting parameter of the HMSSDK constructor.

Here trackInitialState property of HMSVideoTrackSetting and HMSAudioTrackSetting is an Enum of type HMSTrackInitState which has the possible values as:

enum HMSTrackInitState { //If the track needs to be kept mute while joining MUTED, //If the track needs to be kept unmute while joining UNMUTED, }

Let's see how this can be achieved in the following steps.

  1. First, for joining a room with muted audio/video, these values need to be set in the hmsTrackSetting property as -
HMSTrackSetting trackSettings = HMSTrackSetting( // This is for joining with muted audio(mic off) and muted video(camera off) audioTrackSetting: HMSAudioTrackSetting(trackInitialState: HMSTrackInitState.MUTED), videoTrackSetting: HMSVideoTrackSetting(trackInitialState: HMSTrackInitState.MUTED));
  1. Now, create the HMSSDK object by passing the HMSTrackSetting object created above :
class Meeting implements HMSUpdateListener{ late HMSSDK hmsSDK; Meeting(){ // pass the trackSettings while constructing the HMSSDK instance ̶h̶m̶s̶S̶D̶K̶ ̶=̶ ̶H̶M̶S̶S̶D̶K̶(̶)̶;̶ HMSSDK hmsSDK = HMSSDK(hmsTrackSetting: trackSettings); await hmsSDK.build(); // ensure to await while invoking the `build` method hmsSDK.addUpdateListener(this); HMSConfig config = HMSConfig(authToken: 'eyJH5c', // client-side token generated from your token service userName: 'John Appleseed'); hmsSDK.join(config: config); } }

Rest all the steps are same as above.

Now, the user joins the room with the microphone and the camera turned off.


Have a suggestion? Recommend changes ->

Was this helpful?

1234