CC Tutorial – Background


brainCloud uses the Mozilla Rhino engine to allow user-developed JavaScript-based scripts to be run on brainCloud’s servers. Cloud Code scripts run within a secure sandbox that provides access to brainCloud’s API methods, while at the same time ensuring that the scripts are limited to accessing the app’s own private data, and cannot affect the processing of other brainCloud-based applications.

Key Concepts

  • Script parameters are sent via a global “data” JSON object. You retrieve the value of the parameter by simply retrieving it from the data object – i.e. myParm = data[“myParm”];
  • You don’t technically “return” from a Cloud Code script. The last line of the script will instead simply declare the final object to be evaluated as the script’s return — this will be clearer after you view an example or two.
  • The portal includes a built-in cloud editor. We’re working to make it more convenient to write and manage your Cloud Code scripts from your desktop, but for now, the simplest is to write it in our editor. The good news is that the editor is syntax-aware and really does help you to write good JavaScript. (Note – the editor does allow import/export from the desktop)
  • Scripts can be quickly tested via the API Explorer. The API and S2S Explorers make it very easy to test your Cloud Code. Simply go to Design | Cloud Code | API Explorer, then authenticate a session, and then call your script using the Script service and the appropriate Run Script operation. The explorer will even pull your example parameters from the script definitions, so it’s pretty quick and simple to execute.
  • Scripts are versioned. Every time you save a new copy of a script we backup the older version. You can revert to an older script at any time.
  • Scripts have a “watchdog timer” that ensures that they don’t run too long. This is essential to protect the performance of the overall system from run-away scripts. The timeouts for scripts are set individually. The default timeout for a script is 10 seconds but can be set to up to 60 seconds in the timeout dropdown. Note, brainCloud does allow for running longer-term background scripts – just reach-out to us to enable the setting of longer timeouts.

Current Limitations

When writing Cloud Code scripts, there are some key limitations to keep in mind:

  • Each script is limited to a single entry point.  This means that essentially the “mainline” of a script is run whenever the script is called.
  • Access to external libraries is limited. We’re looking to add access to popular libraries like Moment.js in the future.