In Photon, a player is identified using a unique UserID.
You can subscribe to a Photon channel using the same UserID from multiple clients. You will receive the same messages on each client. Also all clients connected with the same UserID will be subscribed to the same channels and have the same friends at any given time.
Generally, UserIDs are not intended to be displayed. Unlike usernames, displaynames or nicknames. A UserID does not have to be human-readable or very human-friendly. So you could, for instance, use a GUID as a UserID.
The advantages of keeping a unique UserID per player:
- You preserve your data between game sessions and across multiple devices. You can rejoin rooms and resume playing where you stopped.
- You can become known to all players you meet and easily identifiable by everyone. You can play with your friends, send them invitations and challenges, make online parties, form teams and guilds, etc. You can add user profiles (e.g. experience, statistics, achievements, levels, etc.) and make games more challenging (also using tournaments and leaderboards).
- You could make use of another service to bind Photon UserID to an external unique identifier. For instance, Photon UserID could be set to Facebook ID, Google ID, Steam ID, PlayFab ID, etc.
- You can prohibit malicious users from connecting to your applications by keeping a blacklist of their UserIDs and making use of .
Once authenticated, a Photon client will keep the same UserID until disconnected. The UserID for a client can be set in three ways:
- Client sends its UserID before connecting by setting
AuthenticationValues.UserId. This option is useful when you do not use and want to set a UserID.
- An external authentication provider returns the UserID on successful authentication. . It will override any value sent by the client.
- Photon Server will assign GUIDs as IDs for users that did not get UserIDs using 1 or 2. So even anonymous users will have UserIDs.
Matchmaking Slot Reservation
Sometimes, a player joins a room, knowing that a friend should join as well.
With Slot Reservation, Photon can block a slot for specific users and take that into account for matchmaking.
To reserve slots there is an
expectedUsers parameter in the methods that get you in a room (
When you know someone should join, pass an array of UserIDs.
JoinRandomRoom, the server will attempt to find a room with enough slots for you and your expected players (plus all active and expected players already in the room).
The server will update clients in a room with the current
expectedUsers, should they change.
To support Slot Reservation, you need to enable publishing UserIDs inside rooms.
Example Use Case: Teams Matchmaking
You can use this to support teams in matchmaking. The leader of a team does the actual matchmaking. He/She can join a room and reserve slots for all members:
Try to find a random room:
Create a new one if none found:
The others don't have to do any matchmaking but instead repeatedly call:
When the leader arrives in a room, the
FindFriends operation will reveal that room's name and everyone can join it:
Photon Chat keeps your friends list in memory as long as you are connected. However this list is not persisted between sessions. You may need an external service for that.
Adding and Removing Friends
You can add friends on Photon Chat and subscribe to his/her status updates. If you do so, you will receive an event every time that same friend changes his/her status or goes offline.
You can add or remove up to 512 friends at once.
To add friends to Photon Chat:
When you no longer need to listen to those events you could remove that friend and unsubscribe from his/her status updates.
To remove friends from Photon Chat:
To receive status updates from your friends you should implement:
void IChatClientListener.OnStatusUpdate(string user, int status, bool gotMessage, object message);
You can update your status on Photon Chat and broadcast this to all your friends.
Here is how to do it:
You can find a list of predefined online status codes in
If you set your online status to
ChatUserStatus.Invisible then all your friend will see you as
You are free to add custom ones not available on that list and used by your application.
By design, Photon Chat does not implicitly broadcast
ChatUserStatus.Online or any other status when you connect.
That is why you need to explicitly set yourself to online as soon as you are connected if you want to have this behaviour.
Optionally you can add a message with the updated status code.
The message will not be sent in case of the two special status codes
In this case use the overload method that takes two parameters:
You could use the status update feature to exchange any type of data (that Photon can serialize) with your friends. It is not limited to strings only.