Skip to main content

Photon Chat

Expected Output/Definition of Done

Able to send and receive chat

Key Solution

Importing SDK

  • Before doing this, make sure you’ve create a Photon Cloud: Chat from your Photon Dashboard.

    Untitled

  • To implement this Photon Chat, you need to download first the SDK here, it’s separated from Photon Fusion.

  • While importing the SDK, ignore these files if you’ve already used Fusion in your game to prevent conflict and Fusion is already used the latest SDK.

    Untitled

Implement Basic Chat Feature

  • To implement the basic chat feature from Photon, create a class that implements IChatClientListener.

  • Then it will give you these callbacks:

    public void DebugReturn(DebugLevel level, string message) { }
    public void OnChatStateChange(ChatState state) { }
    public void OnConnected() { }
    public void OnDisconnected() { }
    public void OnGetMessages(string channelName, string[] senders, object[] messages) { }
    public void OnPrivateMessage(string sender, object message, string channelName) { }
    public void OnStatusUpdate(string user, int status, bool gotMessage, object message) { }
    public void OnSubscribed(string[] channels, bool[] results) { }
    public void OnUnsubscribed(string[] channels) { }
    public void OnUserSubscribed(string channel, string user) { }
    public void OnUserUnsubscribed(string channel, string user) { }
    
  • The first thing you must do is initializing the ChatClient as I do below:

    private void Start()
    {
        _chat = new ChatClient(this);
        _chat.MessageLimit = _messageLimit;
        _chat.ChatRegion = region;
        _currentChannel = channel;
    
        _chat.Connect(
    				ChatSettings.Instance.AppId,
    				PhotonAppSettings.Instance.AppSettings.AppVersion, // Only if you already used Fusion
    				null
    		);
    }
    

    If you already used Fusion, you should use the same channel (or session in Fusion), region, version as you used in Fusion. This is just my recommendation, you can do your own.

  • After you’ve been connected (you can check it on _chat.State), you should subscribe a channel to receive all the incoming chat there. And you’ve must been connected first to subscribe a channel, otherwise it won’t worked. So i called the subscribe function in the OnConnected callback.

    public void OnConnected()
    {
        _chat.Subscribe(_currentChannel);
    }
    
  • Also one thing you must do that you need to call _chat.Service in your Update() based on your needs. This function is used to make the chat feature work, to refresh the state, incoming chat, etc. So, i called it like this:

    private void Update()
    {
        if (_chat == null)
        {
            return;
        }
    
        if (!_chat.CanChat)
        {
            _chat.Service(); // I use this much when we can't chat yet, because i want to update and make the chat feature is ready as fast as possible
            return;
        }
    
        _chatDelayCounter -= Time.deltaTime;
        if (_chatDelayCounter < 0f)
        {
            _chat.Service();
            _chatDelayCounter = _chatDelay; // I use 0.5f for the _chatDelay
        }
    }
    
  • Then, to receive all the incoming chat for you, just implement the OnGetMessages function.

    public void OnGetMessages(string channelName, string[] senders, object[] messages)
    {
    		// This was just an example from me
        for (int i = 0; i < messages.Length; i++)
        {
            if (senders[i] == _chat.UserId)
            {
                continue;
            }
    
            string sender = senders[i];
            string message = messages[i].ToString();
            OnChatReceived?.Invoke(sender, message);
        }
    }
    

    Note that you will also receive your own chat that sent to public channel, so i prevent my own chat to be called by the OnChatReceived event.

  • You should’ve ready to receive the chat now, and to send a message you just need to call this:

    public void SendChat(string chat)
    {
        if (_chat == null || !_chat.CanChat || string.IsNullOrWhiteSpace(chat))
        {
            return;
        }
    
        _chat.PublishMessage(_currentChannel, chat);
        OnChatReceived?.Invoke(_chat.UserId, chat); // You don't need to follow this, it is connected to receiving function that i used above
    }
    

Pros & Cons Using Photon Chat

Pros

  • You can directly used the feature after you create a Photon Cloud: Chat, Dedicated Server could be an alternative (optional).
  • Already supported for channel or private message, you can also get its history.

Cons

  • Last update is 2020, i don’t know if Photon Team is still focused on this Photon Chat or not.