For up-to-date product documentation, see the IBM MobileFirst Foundation Developer Center.
Develop 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 } );
- Implementing JavaScript HTTP adapters
Learn to develop a JavaScript HTTP adapter.- Implementing JavaScript SQL adapters
Learn to develop a JavaScript SQL adapter.- JavaScript server-side API
JavaScript adapters can use the IBM MobileFirst Platform Server JavaScript API to perform server-related operations such as: calling other adapters, logging adapter activity, getting values of configuration properties, reporting activities to IBM MobileFirst Analytics, and getting the identity of the request issuer.- Calling Java code from a JavaScript adapter
Follow these instructions to instantiate Java™ objects and call their methods from JavaScript code in your adapter.
Parent topic: MobileFirst JavaScript adapters