Developing JavaScript adapter code

Learn about implementing JavaScript adapters.

JavaScript adapters and global variables

The IBM MobileFirst™ Platform Server does not rely on HTTP sessions. In a deployment that involves multiple nodes, a client request can be processed by one server in a cluster and the next request by another. You should not rely on global variables to keep data from one request to the next.

Adapter response threshold

Adapter calls are not designed to return huge chunks of data because the adapter response is stored in MobileFirst Server memory as a string. Thus, data that exceeds the amount of available memory might cause an out-of-memory exception and the failure of the adapter invocation. To prevent such failure, you configure a threshold value from which the MobileFirst Server returns gzipped HTTP responses. The HTTP protocol has standard headers to support gzip compression. The client application must also be able to support gzip content in HTTP.

Server side
In the MobileFirst Operations Console, under Runtimes > Settings > GZIP compression threshold for adapter responses, set the desired threshold value and save. The default value is 20 KB.
Note: By saving the change in the MobileFirst Operations Console, the change is effective immediately in the runtime.
Client side
Ensure that you enable the client to parse a gzip response, by setting the value of the Accept-Encoding headers to gzip in every client request. Examples follow:
Android native apps
WLResourceRequest req = new WLResourceRequest(new URI("/adapters/sampleAdapter/procedure"), WLResourceRequest.GET);
req.addHeader("Accept-Encoding", "gzip");
req.send(myListener);
Javascript for Cordova apps
var request = new WLResourceRequest('/adapters/sampleAdapter/procedure', WLResourceRequest.GET);
 request.addHeader('Accept-Encoding',’gzip’);
 request.send().then(
         function(response) {
             // success flow, the result can be found in response.responseJSON
         },
         function(error) { 
             // failure flow 
             // the error code and description can be found in error.errorCode and error.errorMsg fields respectively 
         }
 );