This example will demonstrate how to use peer service as a global module that can be shared across multiple peer client apps.

STEP 1: Peer service app setting

Create an app as a peer service app from brainCloud portal Team | Manage | Apps page.

  • Click [+New App] button, fill in a name for App Name field from pop-up window, click Save.

  • Download the SharedLibrary configuration data file from this link to your local storage.

  • Navigate to Design | Core App Info | Admin Tools page, click Import from Configuration Data section on this page. Uncheck all the overwrite options and click [Select Configuration File (bcconfig)] and attach the file you just downloaded from the last step. Click Upload.

  • Go to Design | Cloud Code | Scripts page, you will find some scripts there (i.e. "listAssets", "getAsset", "updateAsset" and "createAsset").

  • Go to Design | Cloud Data | Custom Entities page, you will find an entity type called "asset" there.

(The asset fields include:

  • assetId - string name, will be auto incremented when creating a new asset.

  • assetType - string used to categorize an asset - "audio", "video", "model", "image", etc.

  • assetUrl - string containing an URL for retrieving the asset.

  • assetDesc - string description of the asset

We will use them as parameters from client apps when calling peer service asset later)

  • After importing the configuration file successfully, navigate to Design | Core App Info | Peer Publish page, click [Edit] to start configuring the peer setting.

  1. Check the Enable As Peer Service option.

  2. Select Private for service visibility.

  3. Select Sponsored for API usage.

  4. Leave Client Config and Hooks tabs as the default setting,

  5. On the Directory tab, add "SharedLibrary" in the Service Name field.

STEP 2: Peer client app setting

  • Create an app with name "LibraryClient" or whatever you like. Then navigate to Design | Integrations | Peer Services page, find your peer service named "SharedLibrary" from More Services section and click the Plus icon in Actions column. Give Service Code with the same name and save it.

  • Download a client app configuration file from here, and import it from the app configuration data section as you did for the peer service app.

STEP 3: Testing

Test from brainCould Portal:

  • Navigate to Design | Cloud Code | API Explorer page, after authentication, select "PeerScript - SharedLibrary" from Service dropdown menu and "createAsset" from Operation dropdown menu, hit Run. (calling "createAsset" to create some assets first. then you good to call the other methods -- listAssets, updateAsset and getAsset)

  • You can also select Sevice as ScriptService and Operation as RunPeerScript, put proper parameters and hit Run.

Note: with createAsset and updateAsset, the methods allow you to put zero or up to all the pre-set parameter(s) as input, you can update this restriction as your design. Look into these scripts code for the detail.

  • All done, you set a shared library and its assets can be shared across any client apps. As you can see, only the peer service app needs a plus plan with deep data usage (custom entity), but all the client apps can access those data without upgrading to a higher plan.

Test from a client app (Unity):

  • Download the Unity test example from here. Unzip it and open with Unity Hub.

  • Or, you can create your own Unity project, install brainCloud latest Unity Plugin to your Unity Engine. Write some code with initializing the app as the peer client app. And call the method like below:

using System;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using BrainCloud;
using BrainCloud.JsonFx.Json;
public class BCinterface : MonoBehaviour
{
public BrainCloudWrapper _bc;
public BrainCloudClient bclient;
Text bcreturn;
InputField id;
InputField url;
InputField desc;
InputField type;
//private Camera mainCamera;
// Start is called before the first frame update
void Start()
{
bcreturn = GameObject.Find("bcreturn").GetComponent<Text>();
id = GameObject.Find("assetId").GetComponent<InputField>();
url = GameObject.Find("assetUrl").GetComponent<InputField>();
desc = GameObject.Find("assetDesc").GetComponent<InputField>();
type = GameObject.Find("assetType").GetComponent<InputField>();
}
// Update is called once per frame
void Update()
{ }
private void Awake()
{
DontDestroyOnLoad(gameObject);
_bc = gameObject.AddComponent<BrainCloudWrapper>();
_bc.WrapperName = gameObject.name;
_bc.Init();
}
//click authentication button
public void AuthenticateBC()
{
_bc.AuthenticateEmailPassword("unityuse111r@unity.com", "unitytest111", true, authSuccess_BCcall, authError_BCcall);
}
public void authSuccess_BCcall(string responseData, object cbObject)
{
bcreturn.GetComponent<Text>().text = "authenticate success \n " + responseData;
}
public void authError_BCcall(int statusCode, int reasonCode, string statusMessage, object cbObject)
{
bcreturn.GetComponent<Text>().text = "authenticate fail \n " + statusMessage;
}
//click listAsset Button
public void ListPeerLibraryAssets()
{
_bc.ScriptService.RunPeerScript("listAssets", "{}", "SharedLibrary", peercSuccess_BCcall, peercError_BCcall);
}
//click createAsset Button
public void CreatePeerLibraryAsset()
{
_bc.ScriptService.RunPeerScript("createAsset", "{}", "SharedLibrary", peercSuccess_BCcall, peercError_BCcall);
}
//click createAsset Button
public void GetPeerLibraryAsset()
{
_bc.ScriptService.RunPeerScript("getAsset", "{\"assetId\":\"" + id.GetComponent<InputField>().text + "\"}", "SharedLibrary", peercSuccess_BCcall, peercError_BCcall);
}
//click createAsset Button
public void UpdatePeerLibraryAsset()
{
_bc.ScriptService.RunPeerScript("updateAsset", "{\"assetId\":\"" + id.GetComponent<InputField>().text + "\",\"assetDesc\":\"" +desc.GetComponent<InputField>().text + "\", \"assetUrl\":\"" +url.GetComponent<InputField>().text + "\", \"assetType\":\"" + type.GetComponent<InputField>().text + "\"}", "SharedLibrary", peercSuccess_BCcall, peercError_BCcall);
}
public void peercSuccess_BCcall(string responseData, object cbObject)
{
Debug.Log("bc pee success call back");
bcreturn.GetComponent<Text>().text = "peer success \n " + responseData;
}
public void peercError_BCcall(int statusCode, int reasonCode, string statusMessage, object cbObject)
{
Debug.Log("bc peer error call back");
bcreturn.GetComponent<Text>().text = "peer fail \n " + statusMessage;
}
}
  • Test from the editor as below or build it into any platform fits your test device:

Did this answer your question?