PlayFab Integration
Introduction
In this document we help you integrate PlayFab with Photon.
With this approach, both systems will get used in parallel to their full potential.
Billing is separate for each service.
Read PlayFab's Photon Quickstart here.
Below are the setup instructions for Photon.
Custom Authentication
Dashboard Configuration
Here are the steps to setup custom authentication with PlayFab:
- Go to Photon dashbaord.
- Choose an application or create a new one.
- Click "Manage".
- Under "Authentication" section, click "Custom Server".
- [Required] Set Authentication URL to
https://{PlayFabTitleId}.playfabapi.com/photon/authenticate
.
Make sure to replace{PlayFabTitleId}
placeholder with your actual PlayFab TitleId.
Do not keep the open ({
) and closing (}
) braces or curly brackets characters in the URL.
Example: if your PlayFab TitleId isAB12
:https://AB12.playfabapi.com/photon/authenticate
. - Save by hitting "Create".
- [Recommended] Untick "Allow anonymous clients to connect, independently of configured providers".
Client Code
Client is expected to send a pair of key/values as credentials:
- PlayFab UserId of the logged in user.
- Photon token (retrieved using GetPhotonAuthenticationToken Client API method).
C#
lbClient.AuthValues = new AuthenticationValues();
lbClient.AuthValues.AuthType = CustomAuthenticationType.Custom;
lbClient.AuthValues.AddAuthParameter("username", PlayFabUserId);
lbClient.AuthValues.AddAuthParameter("token", PlayFabPhotonToken);
// do not set AuthValues.Token or authentication will fail
// connect
C++
ExitGames::Common::JString params = "username="+PlayFabUserId+"&token="+PlayFabPhotonToken;
ExitGames::LoadBalancing::AuthenticationValues playFabAuthenticationValues;
playFabAuthenticationValues.setType(ExitGames::LoadBalancing::CustomAuthenticationType::CUSTOM).setParameters(params);
// pass playFabAuthenticationValues as parameter on connect
JavaScript
var queryString = "username="+playFabUserId+"&token="+playFabPhotonToken;
loadBalancingClient.setCustomAuthentication(queryString, Photon.LoadBalancing.Constants.CustomAuthenticationType.Custom);
// connect
Realtime Webhooks and WebRPC Configuration
If you do not need Realtime Webhooks nor WebRPCs for your application, you can skip this part.
It is recommended to setup custom authentication first.
Realtime Webhooks and WebRPCs may not work otherwise.
Here is how to setup Realtime Webhooks and WebRPCs to work with your PlayFab title.
- Go to Photon dashbaord.
- Choose an application or create a new one.
- Click "Manage".
- Under "Webhooks" section, click "Create a new Webhook".
- From the dropdown list "Select Type" choose Webhooks v1.2.
- [Required] Set "BaseUrl" to
https://{PlayFabTitleId}.playfablogic.com/webhook/1/prod/{PhotonSecretKey}
.- Make sure to replace
{PlayFabTitleId}
placeholder with your actual PlayFab TitleId.
Do not keep the open ({
) and closing (}
) braces or curly brackets characters in the URL.
Example: if your PlayFab TitleId isAB12
and your Photon Secret Key is3z4ujpikyp4hbufno3jfm6fzcxsw5zxjaberfe4zkf4hzuen3
:
https://AB12.playfablogic.com/webhook/1/prod/3z4ujpikyp4hbufno3jfm6fzcxsw5zxjaberfe4zkf4hzuen3
. - The "Photon Secret Key" is a string that you find on PlayFab's Photon add-on page once you add a Photon Realtime application.
- You can replace
prod
withtest
if you want to target latest uploaded / pushed CloudScript revision as opposed to the latest deployed / active one.
- Make sure to replace
- [Optional] Configure the Webhooks Paths you need.
Remove those you do not need.
Read more here. - [Optional] Configure "IsPersistent", "HasErrorInfo" and "AsyncJoin".
Remove their keys if you want to keep default values.
Read more here.
Notes
"CustomHttpHeaders" setting is not supported as those are not exposed in the CloudScript handlers. Any value you set will not be useful.
PlayFab suggests the following names for Realtime Webhooks CloudScript handlers functions:
- "PathCreate": "RoomCreated"
- "PathClose": "RoomClosed" (do not change this handler name)
- "PathJoin": "RoomJoined"
- "PathLeave": "RoomLeft"
- "PathEvent": "RoomEventRaised"
- "PathGameProperties": "RoomPropertyUpdated"
Other than "PathClose", you are free to choose whatever handler name you want for the other paths.
PlayFab expects a valid PlayFabId as UserId argument in all Webhooks or WebRPCs handlers.
The only exception to this is "RoomClosed".All Photon Realtime Webhooks except "PathClose" and all WebRPCs should set PlayFab's CloudScript global variable "currentPlayerId" to the value of the "UserId" argument.
Configured Realtime Webhooks or WebRPCs called by client will not work unless there is an explicit CloudScript handler with the same name.
Examples:
If you configured "PathJoin" to "GameJoined", you need to have this function in the target CloudScript revision:JavaScript
handlers.GameJoined = function(args) { // your custom code goes here return { ResultCode : 0, Message: "Success" }; };
If client calls "foo" WebRPC method, you need to have this function in the target CloudScript revision:
JavaScript
handlers.foo = function(args) { // your custom code goes here return { ResultCode : 0, Message: "Success" }; };