Chat Webhooks

Use webhooks with Photon Cloud to have more control over your chat applications. You can persist channel history, filter messages, cancel channel creation and even prohibit certain players from subscribing to chat rooms.

Photon Chat webhooks are event-driven HTTP POST requests sent by Photon Cloud to specific URLs. Each Photon Chat webhook is defined by its own triggers, data and destination path.

Content

Setup

Basic Settings

  • BaseUrl (required)
    The URL for the service hosting your hooks. It must not end with a forward slash. Callbacks are received at the relative path URIs of the configured hooks.
  • CustomHttpHeaders
    JSON object that contains key/value pairs that should be set as HTTP headers in any request made to the configured web service.

Paths

Configure each path to receive events at the URIs each identifies on your host. Any path that is left empty will not be hit, you will not receive any callbacks on the affected webhook.

  • PathChannelCreate
    Called when a new channel is created or when its state needs to be loaded from external service.
  • PathChannelDestroy
    Called when a channel is removed from Photon servers memory. If IsPersistent is set to true the channel state is sent.
  • PathChannelSubscribe
    Called when a player subscribes to an already created channel.
  • PathChannelUnsubscribe
    Called when a player unsubscribes from a channel.
  • PathPublishMessage
    Called when the client publishes a message in a channel or sends a private message. The client should explicitly choose to HTTP forward the message as a webhook.

Options

Fine-tune the behaviour of your chat webhooks configuration with these options. An option is considered not configured when not set up or if its value is left empty.

  • FailIfUnavailable
    If set to true, channel creation, subscription and publish message operations will fail when the corresponding hook's configured endpoint is not available. An endpoint is considered unavailable when an error occurs during the process of its HTTP request or when the HTTP response is not successful. Default is false.
  • IsPersistent
    If set to true, Photon Cloud sends the channel state before destroying it. This option is only valid when both PathChannelCreate and PathChannelDestory are properly configured and MaxChannelHistory is greater than 0. For more information please read more about ChannelCreate and ChannelDestroy webhooks. Default is false.
  • MaxChannelHistory Maximum number of messages to be persisted per channel. It should be between 1 and 100. Default is 100.
  • HasErrorInfo If set to true, Chat clients will be notified with an error message when a hook endpoint (PathUnsubscribe or PathChannelDestory excluded) is unavailable but FailIfUnavailable is set to false. Default is false.
  • SkipPostCreationFailure If set to true, PathUnsubscribe and PathChannelDestory webhooks will not be sent after a channel creation failure. Default is false.

Common Request Arguments

AppId: AppId of your application as set from the game client and found in the dashboard.

AppVersion: Version of your application as set from the game client.

Region: Has the region to which the game client is connected to and to which the room in question belongs to.

ChannelType: The channel type: "Public" for public channels and "Private" for private channels.

UserId: UserID of the player triggering the hook. (Except ChannelDestory)

HistoryCount: The current count of messages in the channel history. (Except ChannelDestory)

ChannelName: The name of the channel in which the hook is triggered. (Only public channels)

UsersPair: Array of two strings containing the UserIDs of the two ends of the private channel. (Only private channels)

Request Arguments List

This table gives you what arguments are available for each webhook for public channels:

Argument ChannelCreate ChannelDestroy ChannelSubscribe ChannelUnsubscribe PublishMessage
AppId
AppVersion
Region
ChannelType
ChannelName
HistoryLen X X X X
HistoryCount X
UserId x
ChannelState x ✓ (conditions apply) x x x

This table gives you what arguments are available for each webhook for private channels:

Argument PublishMessage
AppId
AppVersion
Region
ChannelType
UsersPair
HistoryCount
UserId

Paths in Details

ChannelCreate

This webhook is sent when a channel is about to be created on Photon servers. If you have a previously saved the ChannelState, you need to return it in the webhooks response to load its history. See "Return Values" for an example.

Specific Arguments

HistoryLen: It forwards the parameter with same name set by client in subscribe operation. It contains the number of messages from the history requested by the client.

Sample Call

{
   "AppId":"00000000-0000-0000-0000-000000000000",
   "AppVersion":"1.0",
   "Region":"EU",
   "ChannelType":"Public",
   "ChannelName":"PersistentChannel",
   "HistoryLen":10,
   "UserId":"testClient1"
}

ChannelDestroy

This webhook is sent when a channel is about to be destroyed on Photon servers. This happens when an empty channel times out. A channel is considered empty when it does not contain any subscriber, because they left. Default empty channel timeout value is 5 seconds. If IsPersistent is set to true and PathChannelDestory is configured properly, ChannelState will be sent in the webhook body. In the ChannelState, the History object is just for debugging purposes. You may discard it. Photon servers use the BinaryHistory to persist data types. You should save it as is if you want to load channel history later.

Specific Arguments

ChannelState: A serializable channel state.

Sample Call

{
   "AppId":"00000000-0000-0000-0000-000000000000",
   "AppVersion":"1.0",
   "Region":"EU",
   "ChannelType":"Public",
   "ChannelName":"PersistentChannel",
   "HistoryCount":2,
   "ChannelState":{
      "ChannelHistoryCapacity":100,
      "BinaryHistory": "RGl6AAEAAAAAAAN6AANp..",
      "History":{
         "MessageIdBase":2,
         "Entries":[
            {
               "Message":"msg1",
               "Sender":"testClient1",
               "MsgId":1
            },
            {
               "Message":"msg2",
               "Sender":"testClient2",
               "MsgId":2
            }
         ]
      }
   }
}

ChannelSubscribe

This webhook is triggered by client subscription to an already created channel. The client is either:

  • not the first subscriber. This should be the most frequent case.
  • the first subscriber when the channel is empty and did not time out. This is unlikely because it is a small window.

Specific Arguments

HistoryLen: It forwards the parameter with same name set by client in subscribe operation. It contains the number of messages from the history requested by the client.

Sample Call

{
   "AppId":"00000000-0000-0000-0000-000000000000",
   "AppVersion":"1.0",
   "Region":"EU",
   "ChannelType":"Public",
   "ChannelName":"PersistentChannel",
   "HistoryLen":-1,
   "HistoryCount": 1,
   "UserId":"testClient2"
}

ChannelUnsubscribe

This webhook is triggered when a client disconnects from a channel, which can be caused by any of the following reasons:

  • explicit call to unsubscribe operation.
  • explicit disconnection.
  • timeout disconnection.
  • channel creation failure and SkipPostCreationFailure is set to false.
  • subscription failure.

Sample Call

{
   "AppId":"00000000-0000-0000-0000-000000000000",
   "AppVersion":"1.0",
   "Region":"EU",
   "ChannelType":"Public",
   "ChannelName":"PersistentChannel",
   "HistoryCount":2,
   "UserId":"testClient2"
}

PublishMessage

This webhook is triggered when a client publishes a message into the channel or sends a private message to a user. The client should explicitly choose to HTTP forward the message as a webhook using the appropriate WebFlag. If you want to replace the message to be published you can return the new one in Data parameter. The value of the returned webhook response's Data property will be published inside the channel instead of the original message content sent by the client. See "Return Values" for an example.

You can choose any type of data as long as it is supported by Photon serialization. One use case for this could be data polling from the web server. Also making use of the Data return parameter may result in a behavior that could be compared to Photon Realtime's WebRPC.

Specific Arguments

Message: Message to be published to all subscribers of the channel. Forwarded as sent from client.

Public Channel: Sample Call

{
   "AppId":"00000000-0000-0000-0000-000000000000",
   "AppVersion":"1.0",
   "Region":"EU",
   "ChannelType":"Public",
   "ChannelName":"PersistentChannel",
   "HistoryCount":1,
   "UserId":"testClient2",
   "Message":"msg2"
}

Private Channel: Sample Call

{
   "AppId":"00000000-0000-0000-0000-000000000000",
   "AppVersion":"1.0",
   "Region":"EU",
   "ChannelType":"Private",
   "UsersPair":["testClient1", "testClient2"],
   "HistoryCount":1,
   "UserId":"testClient2",
   "Message":"msg2"
}

Return Values

All webhooks expect a JSON object containing a ResultCode which can be:

  • 0 for success.

Sample Success Response

{
   "ResultCode":0,
   "DebugMessage":"OK"
}
  • any other integer for failure.

Sample Failure Response

{
   "ResultCode":1,
   "DebugMessage":"A nice self-explanatory error message"
}

"PublishMessage" webhook supports an extra property from the web server which is Data.

Sample "Full" PublishMessage Response

{
   "ResultCode":0,
   "DebugMessage":"Message Will Be Overridden By Server",
   "Data": "Anything you want and supported by Photon Serialization"
}

"ChannelCreate" webhook supports an extra property from web server which is the ChannelState.

Sample "Full" ChannelCreate Response

{
   "ResultCode":0,
   "DebugMessage":"ChannelState Loaded Successfully",
   "ChannelState":{
      "ChannelHistoryCapacity":100,
      "BinaryHistory": "RGl6AAEAAAAAAAN6AANp..",
      "History":{
         "MessageIdBase":2,
         "Entries":[
            {
               "DebugMessage":"msg1",
               "Sender":"testClient1",
               "MsgId":1
            },
            {
               "DebugMessage":"msg2",
               "Sender":"testClient2",
               "MsgId":2
            }
         ]
      }
   }
}

Since "History" is not required for loading the state as it's useful for debugging, you may discard it when saving the state or when loading it.

Sample "Stripped" ChannelCreate Response

{
   "ResultCode":0,
   "DebugMessage":"ChannelState Loaded Successfully",
   "ChannelState":{
      "ChannelHistoryCapacity":100,
      "BinaryHistory": "RGl6AAEAAAAAAAN6AANp.."
   }
}

It is considered a best practice to:

  • have a list of reserved ResultCode values per custom user error type.
  • return a DebugMessage string which can be useful for debugging.

Response Arguments List

This table gives you what response arguments are supported for each webhook for public channels:

Argument ChannelCreate ChannelDestroy ChannelSubscribe ChannelUnsubscribe PublishMessage
ResultCode
DebugMessage ✓ (optional) ✓ (optional) ✓ (optional) ✓ (optional) ✓ (optional)
Data X X X X ✓ (optional)
ChannelState ✓ (optional) X X X X

Cancelable Operations

The table below shows which chat operations could be canceled or aborted using webhooks. To do so you need to return a ResultCode other than 0 and optionally a DebugMessage that you can find in the OperationResponse's DebugMessage.

Webhook Can Be Canceled
ChannelCreate
ChannelDestory X
ChannelSubscribe
ChannelUnsubscribe X
PublishMessage

URL Tags

You can optionally set one or more "dynamic variables". Those variables will be replaced with their respective values in our backend before sending out any requests.

URL tags come in handy if you want to handle user segments of your application differently, each. Photon supports the following URL tags:

  • {AppVersion} will pass the application version as set by the client.
  • {AppId} will pass the ID of your application.
  • {Region} will pass the token for the cloud region the triggering client is connected to, e.g. "eu".
  • {Cloud} will pass the name of the cloud the triggering client is connected to, e.g. "public" or "enigmaticenterprise".

Examples of URL Tags use cases

  1. https://{Region}.mydomain.com/{AppId}?version={AppVersion}&cloud={Cloud} to e.g. route each region to different hosts, version and cloud passed as query parameters.
  2. https://mydomain.com/{Cloud}/{Region}/{AppId}/{AppVersion} passes all tags as well structured URI.

Data Types Conversion

In this section, only the type of data exchanged between Photon server and the web service is explained. For more information about data types between clients and Photon servers please refer to serialization in Photon page.

Photon Server -> Web Service

C# / .Net (Photon supported types) JavaScript / JSON
byte number
short
int
long
double
bool bool
string string
byte array (length < short.MaxValue) string (Base64 encoded)
array ([] of supported type, length < short.MaxValue) array
Hashtable (of supported types, count < short.MaxValue, preferably Photon implementation) object
Dictionary (keys and values of supported types, count < short.MaxValue) object
null null

Sample request data (types are concatenated)

As sent from Photon Server:

As read by Web Service:

Web Service -> Photon Server

Here is a table that matches each JavaScript/JSON type to its equivalent one in C#/.Net :

JavaScript / JSON C# / .Net
object Dictionary<string, object>
array object[]
number (integral) long
number (floating) double
string string
boolean bool
null (not a type) null
undefined (when sent) null

Sample response data (types are concatenated)

As sent from Web Service:

As read from Photon Server:

 To Document Top