brainCloud allows developers to write custom Cloud Code routines in JavaScript, that reside and run on the brainCloud servers, allowing execution of code more securely and efficiently than if it were run client-side.

brainCloud also allows developers to organize cloud code scripts into folders, so developers are able to manage their cloud code script better. This feature becomes the foundation for:

  • greater script reuse

  • managing scripts offline in source repositories (i.e. git)

  • pushing script updates from offboard build servers (i.e. Jenkins)

brainCloud's Cloud Code service is implemented using an embedded Mozilla Rhino Javascript engine.

Communications with the brainCloud APIs are facilitated by a bridge object that is available from all scripts. The bridge allows you to retrieve references to service proxies that allow you to make API calls into brainCloud. These service proxies are organized in alignment with brainCloud's API Modules.

Advantages of Cloud Code scripts include:

  • Better performance when calling multiple API calls in a row

  • Lower brainCloud costs (the first 3 API calls are free, and each one after is 1/2 a count)

  • More secure

  • Ability to change logic server-side without a client update

  • Ability to call out to external web services

  • Ability to perform certain operations that are not enabled in the client API

Cloud Code scripts can be called from:

  • client apps – via the Script Service APIs

  • developer-operated servers – via the Server-to-Server API

  • third-party services – via brainCloud’s WebHooks interface

  • triggered via other operations (API Hooks) – scripts can be configured to be triggered automatically as pre- or post- conditions for other API operations

  • scheduled – scripts scheduled to execute in the future

Cloud Code scripts are written using brainCloud’s web-based script editor, located in the Portal under Design | Cloud Code | Scripts.

Set JavaScript level

  • Go to Design | Core App Info | Advanced Settings page, from Cloud Code section, select JavaScript level under the dropdown menu.

Create a new script

  • Go to Design | Cloud Code | Scripts page, click Create Folder option from the Actions dropdown menu at the right top corner to open Create Folder pop-up window if you want to put the creating script in a new folder.

  • Click the just created folder to get into this folder, then select Create Script option from Actions dropdown to open the script editor window.

  • Fill in a name for your script, check script callable option(s), set a script executing timeout from Timeout dropdown menu, add a description for your script and script test parameter accordingly.

Note: There are three types of callable options for script:

  • Client -- allows scripts to be called from client apps through scriptServiceProxy or from portal API Explorer page through script service.

  • S2S -- allows scripts to be called from client-operated servers or from portal S2S Explorer script service.

  • Peer -- This option only shows on the peer service apps, which allows scripts to be called from the peer client apps scriptServiceProxy or from portal API Explorer page through scriptService RunPeerScript() API. For more information about peer service, check out the document of peer service.

  • Switch to Editor tab, write your script code, when finished, click Save.

  • Switch to Run tab, click [Quick Authenticate] button to login as the current portal login user, click Run to test your script. Check your debug Log, Request, and Response on the right side.

Manage scripts

  • Click the caret down icon of Actions field of each folder or script on the server script list to manipulate this single folder or script.

  • Click Import/Export at the right top corner of the server script list to import or export all your scripts. (click Export All Scripts, you will get a zip file like Scripts_30070.zip, which you can import to other apps directly by click Import Scripts from other apps )

  • You can also click Move folder or script to move them to a new folder location.

Note: brainCloud cloud code supports share script, which means you can include another script in a script by simply using the new bridge.include(“script_path/scriptname.ccjs”) operation.

For example - say you have this script:

MathFunctions script

function sumNums( num1, num2 ) {
return (num1 + num2);
}

function prodNums( num1, num2 ) {
return (num1 * num2 );
}

You can include it from another cloud code script very simply:

"use strict"; 
bridge.include("MathFunctions.ccjs");

function main() {
var response = {};
response.answer = sumNums(data.number1, data.number2);
return response;
}
main();

Note: script path is a relative path, so if without specifying the path in your include operation, the system will only search the scripts in the same folder as the current script located in.

Did this answer your question?