X

The Mobile & Digital Assistant Blog covers the latest in mobile and conversational AI development and engagement

JavaScript needs a Mobile Cloud SDK! Seriously.

Frank Nimphius
Master Principal Product Manager


Building on-device mobile applications and mobile web applications with JavaScript no longer is just the domain of the hip and cool but arguably is now mainstream. JavaScript as a language has gone a long way and finally arrived to what it originally was designed for: easy application development, even on mobile, and even in the enterprise!

Many parties including Gartner predict that the future of mobile application development is hybrid, which includes JavaScript and CSS on the client. These hybrid solutions focus on building responsive client side applications and single page applications (SPA) that you can do easily with today's JavaScript. In addition these apps are often extended with multi-tier architectures that include Mobile Backend as a Service (MBaaS) cloud solutions that provide amongst many mobile functions the ability to integrate with your backend business services via REST & JSON. 

JavaScript includes native support for REST calls and parsing JSON payloads, which at first would make it seem a good candidate for building mobile front ends to connect to an MBaaS; native syntax, what can be better than that?  But with the proliferation of REST APIs each with their own nuance of functionality, even JavaScript developers could do with a hand to reduce the code burden of writing yet another HTTP subroutine to call yet another API, or dealing with yet another JSON payload which is slightly different from the last.

In reducing this burden, Oracle's own MBaaS offering Oracle Mobile Cloud Service (MCS) includes SDKs for Android, iOS, Windows and specifically JavaScript. The JavaScript SDK provides asynchronous REST calls to Oracle MCS mobile backends on the developer's behalf. These calls allow you as a JavaScript developer to stay in your comfort zone as all you need to do in your application code is to deal with native JavaScript functions, properties, objects and arrays.

An Example of Using the MCS JavaScript SDK

To start using the Oracle MCS JavaScript SDK, we first download it as a zip file from the Oracle MCS administration dashboard. We then unzip the downloaded file and copy the mcs.min.js and oracle_mobile_cloud_config.js files to the application directory that holds our other JavaScript libraries. The oracle_mobile_cloud_config.js needs to be saved in the same directory as the mcs.min.js file.

From here in our code to make use of the SDK, the Oracle MCS JavaScript SDK provides a service proxy object for each MCS platform API as well as for calling custom mobile backend APIs. As it is common in JavaScript, proxy objects use asynchronous callbacks to notify the calling application about the success or failure of a MCS call along with a result set or error message depending on the outcome.

To access a proxy object, such as for example the "Analytics" platform API representation to send information about a mobile task to the Oracle MCS analytic engine, we first get a handle to the MobileBackendManager class. The MobileBackendManager  is a singleton object that grants you access to a configured MobileBackend object instance.

To access the MobileBackendManager, we use the mcs global variable name as shown below. Before we can access the MobileBackendManager we set a reference to the mobile backend configuration file to use:

mcs.MobileBackendManager.setConfig(mcs_config); 
var mobileBackend = mcs.MobileBackendManager.getMobileBackend('my_mbe'); 

The "mobileBackend" variable in the example now holds an object of MobileBackend type. The configuration for the MobileBackend we receive from the MobileBackendManager is defined declaratively in a JavaScript configuration object, the oracle_mobile_cloud_config.js file. As example for the named configuration 'my_mbe' above a possible configuration file is shown below:

var mcs_config = {
    mobileBackends: {
        my_mbe: {
            default: true,
            baseUrl: "http://<your mobile backend url>:7201",
            applicationKey: "a53534a3-c38a-4aa6-b685-71a40c2004f5",
            authorization: {
                basicAuth: {
                    backendId: "0bf9af19-a9c7-4f3c-1111-734b83fefcfa",
                    anonymousToken: "QkVOREVSX0ZSWV9NT0JJTEVfQU5P[...]"
                }
            }
        }         
    }
};

 

The configuration file may hold one or more named configuration sets for each mobile backend our app wants to consume like 'my_mbe'. Each mobile backend configuration set defines the Oracle MCS mobile backend base URL and the application container key for our relating MCS mobile backend, as well as information about the authentication options to be used, which can be HTTP Basic Authentication or OAuth. The configuration parameter are derived from the Oracle MCS administration dashboard for a specific mobile backend. 

Once we have access to the MobileBackend object in code, we can access the MCS platform API proxy classes and their functions which make working with MCS simpler. As an example for this article, lets first do an anonymous user authentication to Oracle MCS, then send analytic information about the user's current mobile task to MCS using the Analytics proxy.

First, the authentication call:

// Set authentication type to basic authentication. This will look for the basicAuth configuration
mobileBackend.setAuthenticationType("basicAuth");
mobileBackend.Authorization.authenticateAnonymous(
  function (response, data) {
    // Success callback
  }, 
   function (statusCode, data) {
    // Failure callback
  });

As you can see the code is simple, no REST calls, dealing with HTTP headers or status code, or forming and expanding JSON payloads, the JavaScript SDK has simplified our task substantially.

Next, we access the Analytic proxy to queue custom messages and to send an analytic session to MCS.

var analytics = mobileBackend.Analytics; 

Again thats it. We are ready to send custom analytic events to Oracle MCS.

Custom analytic events in Oracle MCS are defined as part of an analytic session JSON object. This object implicitly captures a session start and end datetime, information like the device's GPS coordinates, current time stamp and other information about the application runtime environment such as the operating system.  Also most importantly, it also contains one to many custom events that we define that capture how our mobile application is being used by the user or other business domain specific events we need analytics on.

As example for this article, lets send a custom event about the status of a repair that a technician has completed for an important customer who reported a broken dish washer:

var event = new AnalyticsEvent("repair_completed");                                    
event.properties['incidentId'] = "sr1001"
event.properties['technician'] = "joe@fixitFast;
event.properties['Comment'] = "replaced_parts";
analytics.logEvent(event); 

Once submitted to MCS, the custom event name, along with the custom event properties can be selected and viewed in the Oracle MCS analytic dashboard. Strings like "repair_completed" and "replaced_parts" simulate application codes we would use in our production mobile application to track business events resulting in success or failure. The analytics.logEvent(event); call queues the custom event to the analytic session, and we can add one or more analytic events to the queue if necessary.

Once we've completed gathering analytic events and are ready to send the analytic session and its associated queue to Oracle MCS we make a call to the flush() function that flushes all the queued events to MCS: 

analytics.flush(
   new function () {
      // Success callback
   },
   new function (statusCode, data) {
      // Error callback 
}); 

Again, there's no REST or JSON work for us to do as the JavaScript developer. The SDK completely shields the client side application developer from having to know in detail the platform REST APIs, the request headers, the response codes and JSON payloads, we just use a few simple JavaScript functions to do the bulk of the work. 

Seriously, JavaScript needs a SDK and Oracle MCS has it for us to use. Whether we're building a hybrid mobile application using JavaScript, or a mobile web application in JavaScript too, oh and let's not forget Android, iOS and Windows too, MCS and its SDKs have us covered.

More information is provided in the Oracle Mobile Cloud Services documentation and the SDK JavaScript documentation and MCS User Guide.

 

Image courtesy of stockphotos at FreeDigitalPhotos.net

Join the discussion

Comments ( 6 )
  • guest Tuesday, February 23, 2016

    Hello,

    Thank for this great post ! I have one question, i don't find in MCS any Javascript SDK dowload link. Where i can find this SDK?

    Thank in advance !


  • guest Tuesday, February 23, 2016

    Good point. The JavaScript SDK is part of the Mobile Cloud Service (MCS 1.1) rollout. If the instance you are on is not yet upgraded then you don't have the download link available. However, if it is a public instance, it will be migrated soon. If not and you are from Oracle, drop me a mail and I send you the SDK.

    Frank


  • Saif Sunday, August 6, 2017
    Hi Frank,

    This means we need to call the mcs api at each interaction point in our code like clicks or each business api call isn't it to get the analytics?

    Regards,
    Saif Kamaal
  • Frank Nimphius Monday, August 7, 2017
    In regards to adding SDK calls at each intersection point in your code. This should be considered on a logical business scope, not a per-click one. The custom analytics function in MCS is not meant a real time user experience measuring. Though you can send batches of events, the information you would gather from clicks are probably to fine detailed.
  • Saif Kamaal Sunday, August 13, 2017
    Thnks for the reply Frank.

    Also, can we call these analytics and use the actual users instead of authenticating Anonymous ?
  • Frank Nimphius Sunday, August 20, 2017
    Yes. You can also use these APIs with the authenticated named user.
Please enter your name.Please provide a valid email address.Please enter a comment.CAPTCHA challenge response provided was incorrect. Please try again.

Recent Content