Documentation

Cloud Code

Publish events to Triggers

Run scripts, module functions, or webhooks through Triggers when your Cloud Code C# module publishes a custom event.
Read time 5 minutesLast updated 2 days ago

A Cloud Code C# module can publish its own events, called custom events, to the Triggers service. You choose the event name and payload, and a trigger on that event runs a Cloud Code script, a module function, or a webhook.
Custom events let a function move follow-up work to a separate call so the player doesn't wait for that work to finish. For example, a function that ends a match can publish a
match-ended
event. A trigger on that event can then grant rewards in a separate call.
Note
Players never receive events that you publish to Triggers. To send a message to connected players, use push messages.

Prerequisites

Your module needs one of the following, depending on how you build it:
  • For Unity Cloud Code modules (.ccmu), the Cloud Code package (
    com.unity.services.cloudcode
    ) version 3.1.0 or later.
  • For modules in a separate C# solution, for example a solution linked through a module reference file, the
    Com.Unity.Services.CloudCode.Apis
    package version 0.0.29 or later. To check the version, open the module's .csproj file.

Publish an event

Cloud Code provides the
ITriggersEventClient
interface, so you don't need to register the interface in your module setup.
When a function in a stateful module publishes an event, the event has the module's scope, such as
Player
or
MultiplayerSession
. For how scope affects which module functions a trigger can call, refer to Scoped trigger events.
To publish an event, follow these steps:
  1. Add an
    ITriggersEventClient
    parameter to a
    CloudCodeFunction
    method.
  2. Call
    PublishEventAsync
    with the execution context, a short event name, and an optional payload. For the rules that the name and payload follow, refer to Name the event and set the payload.
    The following example publishes a
    match-ended
    event with the match ID and score.
    using System.Collections.Generic;using System.Threading.Tasks;using Unity.Services.CloudCode.Apis.Triggers;using Unity.Services.CloudCode.Core;namespace MatchExample{ public class MatchModule { [CloudCodeFunction("EndMatch")] public async Task<string> EndMatch(IExecutionContext context, [CloudCodeService] ITriggersEventClient triggersEventClient, string matchId, int score) { var payload = new Dictionary<string, object> { { "matchId", matchId }, { "score", score } }; return await triggersEventClient.PublishEventAsync(context, "match-ended", payload); } }}
After you call
PublishEventAsync
, the returned task completes when Cloud Code accepts the event and returns the full event type, such as
com.unity.services.cloud-code.custom.match-ended.v1
. Triggers receives the event asynchronously, so a completed task doesn't mean a trigger has run.
If
PublishEventAsync
throws an
ArgumentException
, check the event name and payload against the rules. An
InvalidOperationException
means Cloud Code refuses or can't send the event, for example because the event exceeds the event chain limit.
Cloud Code builds every event type in the following format.
com.unity.services.cloud-code.custom.<event-name>.v1

Name the event and set the payload

The event name and payload follow these rules:
  • The event name can have up to 64 characters and can contain only lowercase letters from
    a
    to
    z
    , digits from
    0
    to
    9
    , hyphens (
    -
    ), and underscores (
    _
    ).
  • Pass the short event name only, such as
    match-ended
    . Cloud Code adds the namespace and version.
  • Cloud Code serializes the payload to JSON. The JSON can be up to 10 KB.
  • To publish an event without data, pass
    null
    as the payload or omit the payload.
A trigger that uses an event depends on the event's payload keys. You can add keys at any time. To remove or rename a key, publish the event under a new name.

Respond to the event

To run an action when a module publishes the event, create a trigger on the event's full event type.
The following trigger configuration runs the
GrantMatchRewards
function of the
MatchRewards
module each time a module publishes
match-ended
. For the fields and the ways to create a trigger, refer to Trigger structure and Define triggers.
{ "name": "grant-match-rewards", "eventType": "com.unity.services.cloud-code.custom.match-ended.v1", "actionType": "cloud-code", "actionUrn": "urn:ugs:cloud-code:MatchRewards/GrantMatchRewards"}
The Triggers service passes the payload to the action as parameters, so
GrantMatchRewards
receives
matchId
and
score
by name. A JavaScript script receives only the payload keys that it declares as parameters.
To run the trigger for some events only, add a filter on the payload. A filter fails on an event that doesn't contain the key that the filter reads, so check for the key first. Add the following
filter
field to the trigger configuration.
"filter": "'score' in data && data['score'] >= 100"
Warning
A function that a trigger runs can publish another event, and a trigger on that event can run another function. Each time an event runs a trigger, and each time a function calls another module or script, the call depth increases by one. Cloud Code limits the call depth to five, so a chain that starts from a player's call can run up to five triggers. When publishing an event goes past that depth,
PublishEventAsync
throws an
InvalidOperationException
. Design your triggers so that an event never leads back to the function that published the event.
After you create the trigger, the Triggers service runs
GrantMatchRewards
each time a module publishes
match-ended
with a score of 100 or more.
To create the trigger in the Unity Dashboard, refer to Add a Cloud Code custom event.

Test locally

From Cloud Code package version 3.1.1, the local Cloud Code server supports
ITriggersEventClient
. The local server checks each event the same way Cloud Code does, then logs the event instead of sending the event to Triggers.
To test that a function publishes a valid event, follow these steps:
  1. Run the module on the local server. For more information, refer to Execute modules on the local server.
  2. Call the function that publishes the event.
  3. Open the local server's logs. For more information, refer to Find the local server's logs and data.
After you open the logs, the log entries show the full event type, scope, and payload of each event that passes the checks. If the event isn't valid, the function throws the same exception that it throws in the cloud.
The local server doesn't run triggers. To test the trigger itself, use a cloud environment.

Additional resources