Writing Scripts
brainCloud allows developers to write custom code (in JavaScript) that resides on brainCloud's servers. These scripts can be triggered by:
- Apps - via the Client API
- Other Scripts - via Cloud Code
- Special Features - like Matchmaking
- Private Servers - via the S2S API
- Public Services - via WebHooks
- Other API calls - via the API Hook mechanism
- Time-based - via brainCloud's Scheduling Service
Cloud Code scripts can be created, edited, and tested from the Edit Scripts page of the brainCloud portal.
Pro-tip: Be sure to click on the Cloud Code tab (top-right) to view the examples in this section.
After reviewing these sections, be sure to check out our bridge for step-by-step instructions on how to create your own scripts!
📄️ Runtime Architecture
's Cloud Code service is implemented using an embedded Mozilla Rhino Javascript engine.
📄️ Basic Structure
The simplest scripts are composed of the following elements:
📄️ Functions
Functions are as you would expect in Javascript, with the added limitation that they should be defined before the inline portion of your script begins.
📄️ Calling APIs
To call a API from within a script, you must retrieve the appropriate service proxy from the bridge. You then call the desired method on the proxy.
📄️ Including Shared Scripts
supports shared scripts, which means you can include another script
📄️ Calling Other Scripts
To invoke a script from within another script, simply call the bridge.callScript() method, specifying the name and parameters for the script to call.
📄️ Script paths
now supports script folders, allowing developers to better organize their cloud code scripts.
📄️ Invoking via Client API
You use the Script service to invoke a Cloud Code script from a client application.
📄️ API Hooks
allows you to modify the default behaviour of most API methods by configuring a Cloud Code script to run immediately before (pre) or after (post) the built-in API code.
📄️ Matchmaking Filters
allows you to use Cloud Code scripts to filter matchmaking candidates.
📄️ S2S Scripts
Server-to-Server (S2S) Scripts are invoked not from the Client API, but rather from our Server-to-Server Interface. These scripts are intended to be called from custom (private) servers - and may be utilized for managing global datafill, content management, triggering engagement events, etc.
📄️ Scheduled Scripts
includes a scheduling system that allows you to defer running a script until a later time. You can schedule scripts via the ScheduleRunScriptUTC() and ScheduleRunScriptMinutes() API calls.
📄️ WebHooks
also allows S2S-style Cloud Code scripts to be called via WebHooks.