All Collections
Portal-X Pages
Design | Cloud Code | Scripts
Design | Cloud Code | Scripts

Create and edit JavaScript-based Cloud Code scripts.

Jason Liang avatar
Written by Jason Liang
Updated over a week ago

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 can manage their cloud code scripts 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 2 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 powered by Monaco Editor, located in the Portal under Design | Cloud Code | Scripts page.

Set JavaScript level

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

Create and Manage scripts

  • Navigate to the Design | Cloud Code | Scripts page. From the Explorer menu located at the top script list panel, select the + [Create Folder] option to access the Create Folder pop-up window. This will allow you to designate a new folder for the creation of your script.

  • Click the corresponding Action icon, vertical ellipses, to manipulate folders and scripts after creation.

  • Upon selecting a folder and clicking on the Create Script option from the action icon, or by using the + icon located in the top menu bar, the Create Script pop-up window will be displayed.

  • You can go back to edit these script meta-data and parameters by clicking the edit icon on the DETAILS bar.

  • To start writing your script, please select the "EDIT" button at the upper-right corner of the script edit window.

  • 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.

  • After configuring the meta-data for your script and clicking the CREATE button, a new tab for the script will be opened. You can write a script utilizing the integrated autocomplete functionality of Intellisense.

    • Double-clicking the scripts from the left script nav panel can open multiple tabs for the script edit window. Including open API docs for referencing code during the writing process

    • Use the Fullscreen toggle icon to switch between the fullscreen edit window and the normal edit window.

    • Utilizing the function icons, namely Explorer, Search, Run and Debug, and Settings, situated at the upper-left corner of the screen, toggle between editing and normal viewing mode for the layout.

Run and debug your script

  • Click the DEBUG button to switch the editor layout into Run and Debug mode.

  • Click the [Quick Authenticate] button to log in as the current portal login user, and click Execute to test your script. View your debug Log, Request, and Response on the right side of the screen, logs section.

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 is located in.

For further details on writing a cloud code script, refer to our docs here.

Did this answer your question?