UserIDs and Friends
UserIDs
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.
Unique UserIDs
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 custom authentication.
Setting UserIDs
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 custom authentication and want to set a UserID. - An external authentication provider returns the UserID on successful authentication. See custom 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.
Friends
- Friends' UserIDs are case sensitive. Example: "mybestfriend" and "MyBestFriend" are two different UserIDs for two different friends.
- Only friends connected to the same AppID, the same Photon Cloud region and play the same Photon AppVersion can find each other no matter what device or platform they're using.
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.
Adding friends will result in receiving an initial status per friend and will trigger the respective callback.
You can add or remove up to 512 friends at once.
To add friends to Photon Chat:
chatClient.AddFriends(friendsUserIds);
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:
chatClient.RemoveFriends(friendsUserIds);
To receive status updates from your friends you should implement:
void IChatClientListener.OnStatusUpdate(string user, int status, bool gotMessage, object message);
Updating Status
You can update your status on Photon Chat and broadcast this to all your friends.
Here is how to do it:
chatClient.SetOnlineStatus(statusCode);
You can find a list of predefined online status codes in ChatUserStatus
class.
If you set your online status to ChatUserStatus.Invisible
then all your friend will see you as ChatUserStatus.Offline
instead.
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.
Also by design, if you did not explicitly set your status to ChatUserStatus.Invisible
or ChatUserStatus.Offline
, your friends will know when you are disconnected from Photon Chat.
They will receive a status update with status set to ChatUserStatus.Offline
.
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 ChatUserStatus.Offline
and ChatUserStatus.Invisible
.
In this case use the overload method that takes two parameters:
chatClient.SetOnlineStatus(statusCode, statusMessage);
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.