Class: ChatClient

Photon.Chat.ChatClient(protocol, appId, appVersion)

Implements the Photon Chat API workflow.
This class should be extended to handle system or custom events and operation responses.

Constructor

new ChatClient(protocol, appId, appVersion)

Parameters:
Name Type Description
protocol Photon.ConnectionProtocol Connecton protocol.
appId string Cloud application ID.
appVersion string Cloud application version.

Members

(static, readonly) ChatPeerErrorCode

Enum for client peers error codes.

Properties:
Name Type Description
Ok number No Error.
FrontEndError number General FrontEnd server peer error.
FrontEndConnectFailed number FrontEnd server connection error.
FrontEndConnectClosed number Disconnected from FrontEnd server.
FrontEndTimeout number Disconnected from FrontEnd server for timeout.
FrontEndEncryptionEstablishError number FrontEnd server encryption establishing failed.
FrontEndAuthenticationFailed number FrontEnd server authentication failed.
NameServerError number General NameServer peer error.
NameServerConnectFailed number NameServer connection error.
NameServerConnectClosed number Disconnected from NameServer.
NameServerTimeout number Disconnected from NameServer for timeout.
NameServerEncryptionEstablishError number NameServer encryption establishing failed.
NameServerAuthenticationFailed number NameServer authentication failed.

(static, readonly) ChatState

Enum for client states.

Properties:
Name Type Description
Error number Critical error occurred.
Uninitialized number Client is created but not used yet.
ConnectingToNameServer number Connecting to NameServer.
ConnectedToNameServer number Connected to NameServer.
ConnectingToFrontEnd number Connecting to FrontEnd server.
ConnectedToFrontEnd number Connected to FrontEnd server.
Disconnected number The client is no longer connected (to any server).

Methods

connectToNameServer(optionsopt) → {boolean}

Starts connection to NameServer.

Parameters:
Name Type Attributes Description
options object <optional>
Additional options
Properties:
Name Type Description
options object Additional options
Properties
Name Type Attributes Description
region string <optional>
If specified, Connect to region master after succesfull connection to name server
Returns:
True if current client state allows connection.
Type
boolean

connectToRegionFrontEnd(region) → {boolean}

Connects to a specific region's Master server, using the NameServer to find the IP. Override onWebRpcResult to handle request results.

Parameters:
Name Type Description
region string Region connect to Master server of.
Returns:
True if current client state allows connection.
Type
boolean

disconnect()

Disconnects from all servers.

getRegions()

Requests NameServer for regions list.
Override onGetRegionsResult to handle request results.

isConnectedToFrontEnd() → {boolean}

Returns true if client connected to Front End.When connected, client can send messages, subscribe to channels and so on.

Returns:
True if connected.
Type
boolean

isConnectedToNameServer() → {boolean}

Checks if client is connected to NameServer server.

Returns:
True if client is connected to NameServer server.
Type
boolean

onChatMessages(channelName, messages)

Called when new chat messages received.
Override to handle messages receive event.

Parameters:
Name Type Description
channelName string Chat channel name.
messages Array.<Photon.Chat.Message> Array of received messages.

onPrivateMessage(channelName, message)

Called when new private message received.
Override to handle message receive event.

Parameters:
Name Type Description
channelName string Private channel name(counterpart user id).
message Photon.Chat.Message Received message.

onStateChange(state)

Called on client state change. Override to handle it.

Parameters:
Name Type Description
state Photon.Chat.ChatClient.ChatState New client state.

onSubscribeResult(results)

Called when subscribe request completed.
Override to handle request results.

Parameters:
Name Type Description
results object Object with channel names as keys and boolean results as values.

onUnsubscribeResult(results)

Called when unsubscribe request completed.
Override to handle request results.

Parameters:
Name Type Description
results object Object with channel names as keys and boolean results as values.

onUserStatusUpdate(userId, status, gotMessage, statusMessage)

Called when user from friend list changes state.
Override to handle change state event.

Parameters:
Name Type Description
userId string User id.
status number New User status. Predefined Constants.UserStatus or custom.
gotMessage boolean True if status message updated.
statusMessage string Optional status message (may be null even if gotMessage = true).

onUserSubscribe(channelName, userId)

A user has subscribed to a public chat channel

Parameters:
Name Type Description
channelName string Chat channel name.
userId string User id.

onUserUnsubscribe(channelName, userId)

A user has unsubscribed from a public chat channel

Parameters:
Name Type Description
channelName string Chat channel name.
userId string User id.

publishMessage(channelName, content, optionsopt) → {boolean}

Sends a message to a public channel.
Channel should be subscribed before publishing to it. Everyone in that channel will get the message.

Parameters:
Name Type Attributes Description
channelName string Channel name to send message to.
content any Text string or arbitrary data to send.
options object <optional>
Additional options
Properties:
Name Type Description
options object Additional options
Properties
Name Type Attributes Description
webForward boolean <optional>
Optionally, private messages can be forwarded as webhooks. Configure webhooks for your Chat app to use this.
Returns:
True if message sent.
Type
boolean

sendPrivateMessage(userId, content, optionsopt) → {boolean}

Sends a private message to a single target user.

Parameters:
Name Type Attributes Description
userId string User id to send this message to.
content any Text string or arbitrary data to send.
options object <optional>
Additional options
Properties:
Name Type Description
options object Additional options
Properties
Name Type Attributes Description
webForward boolean <optional>
Optionally, private messages can be forwarded as webhooks. Configure webhooks for your Chat app to use this.
Returns:
True if message sent.
Type
boolean

setLogLevel(level)

Sets client log level

Parameters:
Name Type Description
level Photon.LogLevel Log level.

setUserStatus(status, messageopt, skipMessageopt) → {boolean}

Sets the user's status (pre-defined or custom) and an optional message.
The predefined status values can be found in Constants.UserStatus.
State UserStatus.Invisible will make you offline for everyone and send no message.

Parameters:
Name Type Attributes Default Description
status number User status to set.
message string <optional>
null State message.
skipMessage boolean <optional>
false If true { client does not send state message.
Returns:
True if command sent.
Type
boolean

subscribe(channelNames, optionsopt, createOptionsopt) → {boolean}

Sends operation to subscribe to a list of channels by name.
Override onSubscribeResult to handle request results.

Parameters:
Name Type Attributes Description
channelNames Array.<string> Array of channel names to subscribe to.
options object <optional>
Additional options
createOptions object <optional>
Room options for creation
Properties:
Name Type Description
options object Additional options
Properties
Name Type Attributes Description
historyLength number <optional>
Controls messages history sent on subscription. Not specified or 0: no history. 1 and higher: number of messages in history. -1: all history.
lastIds Array.<number> <optional>
Array of IDs of last messages received per channel. Useful when resubscribing to receive only messages we missed.
createOptions object Room options for creation
Properties
Name Type Attributes Default Description
publishSubscribers boolean <optional>
false Whether or not the channel to be created will allow client to keep a list of users.
maxSubscribers number <optional>
0 Limit of the number of users subscribed to the channel to be created.
Returns:
True if operation sent.
Type
boolean

unsubscribe(channelNames) → {boolean}

Sends operation to unsubscribe from a list of channels by name.
Override onUnsubscribeResult to handle request results.

Parameters:
Name Type Description
channelNames Array.<string> Array of channel names to unsubscribe from.
Returns:
True if operation sent.
Type
boolean

(static) StateToName(state) → {string}

Converts ChatState element to string name.

Parameters:
Name Type Description
state Photon.Chat.ChatClient.ChatState Client state.
Returns:
Specified element name or undefined if not found.
Type
string