How can I use Network Awareness within my app to improve performance ?

Document created by Akhil Jayaprakash Employee on Feb 16, 2016Last modified by Akhil Jayaprakash Employee on Feb 16, 2016
Version 2Show Document
  • View in full screen mode

The Akamai SDK provides you with three values 0,1 and 2.

 

0 = Congestion is High

1 = Congestion is Medium

2 = No Congestion

 

You can use these values to signal Akamai or your Origin server or your App to perform network aware optimization.

 

How to signal Akamai to compress images based on client side congestion

Pass the congestion info the SDK provides you as a header to your requests going to Akamai. The header name is "aic-quality", so if the congestion is really high you set the aic-quality to 10 and if the congestion is really low/no congestion then the aic-quality is set to 100.

 

Below is the code snippet we used within our Shopify demo app.


- (void) updateNSURLSessionConfig:(NSURLSessionConfiguration *)config withCongestionStatus:(int)congestionStatus
{
 // ensure requests are sent serially
  config.HTTPMaximumConnectionsPerHost = 1;

 NSMutableDictionary *newHeaders =
  [@{@"User-Agent": [NSString stringWithFormat:@"Mobile Buy SDK iOS/%@", BUYVersionString]} mutableCopy];

 // Add 'Cookie = "aic-quality=100"'.
 // Create actual cookie below in case we want to store it later.
 // In the future the SDK may internally send this cookie.

 // Until then, hard-coding congestion levels as a hack to avoid an SDK dependency.
 int qualityLevel;
 switch (congestionStatus) {
  case 0: // VocCongestionLevelHigh
  qualityLevel = 10;
  break;
  case 1: // VocCongestionLevelMedium
  qualityLevel = 50;
  break;
  case 2: // VocCongestionLevelNoCongestion
  default:
  qualityLevel = 100;
  break;
  }
 NSString *qualityString = [NSString stringWithFormat:@"%d", qualityLevel];
 NSDictionary *cookieProps = [NSDictionary dictionaryWithObjectsAndKeys:
   @".test.edgekey.net", NSHTTPCookieDomain,
   @"/", NSHTTPCookiePath,
   @"aic-quality", NSHTTPCookieName,
  qualityString, NSHTTPCookieValue,
  [NSDate dateWithTimeIntervalSinceNow:344453720], NSHTTPCookieExpires, //10 years
   nil];
 NSHTTPCookie *cookie = [NSHTTPCookie cookieWithProperties:cookieProps];
 if (cookie) {
  NSDictionary *headers = [NSHTTPCookie requestHeaderFieldsWithCookies:@[cookie]];
  for (NSString *key in headers) {
  NSString *val = [headers objectForKey:key];
  [newHeaders setValue:val forKey:key];
  }
  }

  config.HTTPAdditionalHeaders = [newHeaders copy];
 self.session = [NSURLSession sessionWithConfiguration:config delegate:self delegateQueue:nil];
}



 

 

This document was generated from the following discussion: How can I use Network Awareness within my app to improve performance ?

Attachments

    Outcomes