Chat
Chat is a new service that enables real-time communication between end-users. It is available to apps that have brainCloud RTT enabled.
Key concepts
- Chat messages are directed to channels, not users
- Channels have two important relationships with users:
- members (subscribers) -- persistent members of the channel. i.e. those who can access messages, and are allowed to listen to real-time chat
- listeners -- online members who are listening right now! Chat messages are "pushed" only to active listeners
- There are different types of channels. Channel types determine by how members and listeners are managed. See Channel Types section below.
- A rolling history of chat messages is maintained per channel
- Chat messages are simple json objects (see format below)
- There are both member and system chat messages
- Member chat messages are from an end-user who is a member of the channel. Member messages have the user's
profileId
in thefromId
field - System chat messages are from the system (i.e. the app or game) itself. Those message will have
null
in thefromId
field
- Member chat messages are from an end-user who is a member of the channel. Member messages have the user's
- Every chat message is assigned an id. The message id (
msgId
) must be combined with the channel id (chId
) to fully reference the message
Chat message format
{
"chId": "<channel id>",
"msgId": "<guid>",
"date": 1530069895070,
"ver": 1,
"from": {
"id": "<profileId>",
"name": "<name>",
"pic": "<pictureUrl>"
},
"content": {
"text": "simple message",
"rich": {}
}
}
Chat message fields
Field | Description |
---|---|
chId | The channel id |
msgId | The chat message id (used for editing / deleting posted messages) |
date | The time the message was posted (in utc millis) |
ver | The version of the message |
from | Information about who the chat message is from. Includes name and picture url if available |
content | The content of the chat message. Contents are up to the developer - but for visibility in tools, should have a text field for plain-text message content |
Channel Types
brainCloud currently supports four types of channels:
Channel Type | Membership | Creation | Description |
---|---|---|---|
global | All users of the app | Via portal | All users of an app can access global channels. By default, an app can have up to 10 channels. Contact us with your use case for more. The global channel id is formed as appId:gl:channelCode . |
group | All members of a Group | Auto | A channel is automatically created for each group in the system. Any group member can access it. The group channel id is formed as appId:gr:groupId . |
dynamic | All users of the app | Auto | Dynamic chat channels are special channels that any user can create and/or join. They exist only for as long as there is activity. Once the channel inactivity timeout expires (typically 30 days), the history (and the channel itself) are automatically deleted and cleaned up. The dynamic channel id is formed as appId:dy:channelCode . |
system | Use for s2s system channel | Auto | A channel is designed for s2s connenction. The system channel id is formed as appId:sy:channelCode . |
Pro-tip - if you are interested in one-to-one chat, see our Messaging api...
API Summary
Channel lookup and management
Retrieving chat messages
Posting chat messages
Updating chat messages
System chat API (cloud-code only)
These API calls bypass channel membership / message ownership checks - and allow you to post messages from the system (i.e. app) instead of from another end-user:
- SysCreateChannel
- SysDeleteChannel
- SysDeleteChatMessage
- SysGetChannelInfo
- SysGetChatMessage
- SysGetRecentChatMessages
- SysPostChatMessage
- SysPostChatMessageSimple
- SysUpdateChatMessage
📄️ ChannelConnect
Registers a handler for incoming events from the specified channelId. Also returns a list of that channel's recent messages (from history).
📄️ ChannelDisconnect
Disconnects from the specified channel, unregistering the handler.
📄️ DeleteChatMessage
Delete the specified chat message. Message must have been from this user. Version must match (or pass -1 to bypass version enforcement).
📄️ GetChannelId
Retrieves a channel id given the specified lookup parameters.
📄️ GetChannelInfo
Returns description info and activity stats for the specified channel. Note that numMembers, numListeners and listeners are only returned for non-global groups. Only callable by members of the channel.
📄️ GetChatMessage
Retrieves a chat message object from history. If the message cannot be found, that data section returned will be empty.
📄️ GetRecentChatMessages
Returns a list of max \ messages from history.
📄️ GetSubscribedChannels
Returns a list of the channels of the specified type that the user has access to, with description info. Channel type can be one of "gl", "gr" or "all".
📄️ PostChatMessage
Sends a potentially richer member chat message. By convention, content should contain a field named text for plain-text content. Returns the id of the message created.
📄️ PostChatMessageSimple
Sends a text-only member chat message on behalf of the user. This method provides convenience by simplifying message construction. Returns the id of the message that was created.
📄️ SysCreateChannel
Initializes a dynamic or system chat channel. Returns the channelId. Channel type can be "dy" or "sy".
📄️ SysDeleteChannel
Deletes a dynamic chat channel. Channel type can be "dy" or "sy". This will delete the chat history as well.
📄️ SysDeleteChatMessage
This method is available in Cloud Code scripts only.
📄️ SysGetChannelInfo
This method is available in Cloud Code scripts only.
📄️ SysGetChatMessage
This method is available in Cloud Code scripts only.
📄️ SysGetRecentChatMessages
This method is available in Cloud Code scripts only.
📄️ SysPostChatMessage
This method is available in Cloud Code scripts only.
📄️ SysPostChatMessageSimple
This method is available in Cloud Code scripts only.
📄️ SysUpdateChatMessage
This method is available in Cloud Code scripts only.
📄️ UpdateChatMessage
Update the specified chat message. Message must have been from this user. Version provided must match (or pass -1 to bypass version enforcement).