Photon Chat Intro
Get Started
Keep your users communicating while they are online using Photon Chat.
Photon Chat applications need a distinct application and AppId to connect to.
Get your AppId from the Chat Dashboard after
free signup
Connect
To get updates and messages, your application has to implement several methods that are used as callback.
C#
// In the C# SDKs, the callbacks are defined in the `IChatClientListener` interface.
// In the demos, we instantiate and use the ChatClient class to implement the IChatClientListener interface.
chatClient = new ChatClient( this );
// Set your favourite region. "EU", "US", and "ASIA" are currently supported.
chatClient.ChatRegion = "EU";
chatClient.Connect(chatAppId, chatAppVersion, new AuthenticationValues(userID));
C++
ChatListener chatListener; // replace 'ChatListener' with the name of your apps subclass of the abstract base class ExitGames::Chat::Listener or simply pass 'this' to the ExitGames::Chat::Client constructor if you prefer having one of your classes operating on ExitGames::Chat::Client AND inheriting from ExitGames::Chat::Listener
static const ExitGames::Common::JString appID = L"<no-app-id>"; // set your app id here
static const ExitGames::Common::JString appVersion = L"1.0";
ExitGames::Chat::Client chatClient(chatListener, appID, appVersion);
chatClient.setRegion(L"EU"); // Set your favorite region. "EU", "US", and "ASIA" are currently supported.
chatClient.connect(ExitGames::Chat::AuthenticationValues().setUserID(userID));
Aside AppId, Connect()
is passed the version, an arbitrary string, and the username, a unique name in best case.
Call Service
To keep the connection alive and to get incoming messages continuously call:
C#
chatClient.Service();
C++
chatClient.service();
Subscribe To Public Channels
With Photon Chat you use public channels to group users or topics.
Anyone who subscribes to a public channel gets all messages published within.
New channels are created upon first subscription.
C#
chatClient.Subscribe( new string[] { "channelA", "channelB" } );
C++
ExitGames::Common::JString channels[] = {L"channelA", L"channelB"};
chatClient.opSubscribe(ExitGames::Common::JVector<ExitGames::Common::JString>(channels, sizeof(channels)/sizeof(ExitGames::Common::JString)));
Pass an array of strings to Subscribe, for the channels to be subscribed.
Subscribing and unsubscribing will update ChatClient.PublicChannels.
Optionally, the ChatChannel instances get the list of subscribers, which updates ChatChannel.Subscribers.
Send
Public Messages
Prior to publishing, subscribe to the channel you want to publish messages in.
Publish messages to all subscribers of a channel with:
C#
chatClient.PublishMessage( "channelA", "So Long, and Thanks for All the Fish!" );
C++
chatClient.opPublishMessage(L"channelA", L"So Long, and Thanks for All the Fish!");
Other than plain strings Photon Chat allows you to define complex messages, e.g. for invitations.
Private Messages
Send a private message to any user with:
C#
chatClient.SendPrivateMessage( "Arthur", "2 x 3 x 7" );
C++
chatClient.opSendPrivateMessage(L"Arthur", L"2 x 3 x 7");
Receive
Public Messages
To handle incoming public messages from all subscribed public channels you need a proper chat listener callback:
C#
public class MyChatListner : IChatClientListener
{
public void OnGetMessages( string channelName, string[] senders, object[] messages )
{
string msgs = "";
for ( int i = 0; i < senders.Length; i++ )
{
msgs = string.Format("{0}{1}={2}, ", msgs, senders[i], messages[i]);
}
Console.WriteLine( "OnGetMessages: {0} ({1}) > {2}", channelName, senders.Length, msgs );
// All public messages are automatically cached in `Dictionary<string, ChatChannel> PublicChannels`.
// So you don't have to keep track of them.
// The channel name is the key for `PublicChannels`.
// In very long or active conversations, you might want to trim each channels history.
}
// ...
C++
void ChatListener::onGetMessages(const ExitGames::Common::JString& channelName, const ExitGames::Common::JVector<ExitGames::Common::JString>& senders, const ExitGames::Common::JVector<ExitGames::Common::Object>& messages)
{
for(unsigned int i=0; i<senders.getSize(); ++i)
std::wcout << L"[" + channelName + L": " + senders[i] + L"] >>> " + messages[i].toString() << std::endl;
}
Private Messages
To receive and parse private messages you implement the following chat listener callback:
C#
public class MyChatListner : IChatClientListener
{
public void OnPrivateMessage( string sender, object message, string channelName )
{
Console.WriteLine( "OnPrivateMessage: {0} ({1}) > {2}", channelName, sender, message );
// All private messages are automatically cached in `ChatClient.PrivateChannels`, so you don't have to keep track of them.
// A channel name is applied as key for `PrivateChannels`.
// Get a (remote) user's channel name with `ChatClient.GetPrivateChannelNameByUser(name)`.
// e.g. To get and show all messages of a private channel:
// ChatChannel ch = this.chatClient.PrivateChannels[ channelName ];
// foreach ( object msg in ch.Messages )
// {
// Console.WriteLine( msg );
// }
}
// ...
C++
void ChatListener::onPrivateMessage(const ExitGames::Common::JString& sender, const ExitGames::Common::Object& message, const ExitGames::Common::JString& channelName)
{
std::wcout << L"[Private '" + channelName + L"': " + sender + L"] >>> " + message.toString() << std::endl;
}
Online Status
Own Status
Set the online status for your players and an optional status message with:
C#
chatClient.SetOnlineStatus( UserStatus.Online, "Mostly Harmless" );
C++
chatClient.opSetOnlineStatus(ExitGames::Chat::UserStatus::ONLINE, L"Mostly Harmless");
Define your own statuses using integers.
Friends Status
Just send an array of usernames to Photon to get their future status updates.
C#
friends = new List<string>() { "Ford", "Zaphod", "Marvin", "Eddie" };
chatClient.AddFriends(friends.ToArray());
C++
ExitGames::Common::JString friends[] = {L"Ford", L"Zaphod", L"Marvin", L"Eddie"};
chatClient.opAddFriends(ExitGames::Common::JVector<ExitGames::Common::JString>(friends, sizeof(friends)/sizeof(ExitGames::Common::JString)));
You'll receive updates with the current status per friend to the implemented callback:
C#
void OnStatusUpdate( string user, int status, bool gotMessage, object message )
{
Console.WriteLine( "Status change for: {0} to: {1}", user, status );
}
C++
void ChatListener::onStatusUpdate(const ExitGames::Common::JString& user, int status, bool gotMessage, const ExitGames::Common::Object& message)
{
std::wcout << L"onStatusUpdate: " + user + L" has changed his/her status to: " + status + L" / " + (gotMessage?message.toString():L"[message skipped]") << std::endl;
}