Search code examples
javascripttypescriptagora.ioagora-web-sdk-ng

Agora Web check network quality


I am using agora for video call and streaming services. I need to check the netwok quality and notify users for slow connection.For these i wrote these functions for checking uplink and downlink network quality.this is the reference

https://docs.agora.io/en/All/API%20Reference/web/v2.6/interfaces/agorartc.networkqualitystats.html

    const rtcClient = this.client;
    
const checkQualityDrop = (
              quality: string,
              currentQuality: number,
            ): { newNetworkQuality: number; qualityDroppedNow: boolean } => {
              const newNetworkQuality = Number.parseInt(quality, 10);
              const qualityDroppedNow = currentQuality < 4
                && newNetworkQuality >= 4;
              return { newNetworkQuality, qualityDroppedNow };
            };
        
            rtcClient.on('network-quality', (quality) => {
              if (quality.uplinkNetworkQuality) {
                const {
                  newNetworkQuality,
                  qualityDroppedNow,
                } = checkQualityDrop(quality.uplinkNetworkQuality, this.uplinkNetworkQuality);
        
                if (qualityDroppedNow) {
                  this._log('network-drop', { type: 'uplink', newNetworkQuality });
                }
        
                this.uplinkNetworkQuality = newNetworkQuality;
              }
        
              if (quality.downlinkNetworkQuality) {
                const {
                  newNetworkQuality,
                  qualityDroppedNow,
                } = checkQualityDrop(quality.downlinkNetworkQuality, this.downlinkNetworkQuality);
        
                if (qualityDroppedNow) {
                  this._log('network-drop', { type: 'downlink', quality: newNetworkQuality });
                }
        
                this.downlinkNetworkQuality = newNetworkQuality;
              }
            });

Anything greater than 4 in the uplink and downlink should notify users for slow network.But here its not working for slow network.Anyway i can rewrite or improve these functions?


Solution

  • I'm not sure your conditional will evaluate true when the quality drops:

    const qualityDroppedNow = currentQuality < 4 && newNetworkQuality >= 4;
    

    You're checking if the previous quality was low and the new quality is improved/recovered.