Manage chat rooms
Chat rooms enable real-time messaging among multiple users.
Chat rooms do not have a strict membership, and members do not retain any permanent relationship with each other. Once going offline, chat room members cannot receive any messages from the chat room and automatically leave the chat room after 2 minutes (members on the chat room allow list remain in the chat room even if they stay offline for 2 minutes or more). Chat rooms are widely applied in live broadcast use cases such as stream chat in Twitch.
This page shows how to use the Chat SDK to create and manage a chat room in your app.
Understand the tech
The Chat SDK provides the ChatManager
and ChatRoom
classes for chat room management, which allows you to implement the following features:
- Create and destroy a chat room
- Join and leave a chat room
- Retrieve the chat room list from the server
- Retrieve the attributes of a chat room
- Listen for chat room events
Prerequisites
Before proceeding, ensure that you meet the following requirements:
- You have initialized the Chat SDK. For details, see SDK quickstart.
- You understand the call frequency limit of the Chat APIs supported by different pricing plans as described in Limitations.
- You understand the number of chat rooms supported by different pricing plans as described in Pricing Plan Details.
- Only the app super admin has the privilege of creating a chat room. Ensure that you have added an app super admin by calling the super-admin RESTful API.
Implementation
This section introduces how to call the APIs provided by the Chat SDK to implement the features listed above.
Create and destroy a chat room
The app super admin can call createChatRoom
to create a chat room and set the chat room attributes such as the chat room subject, description, and the maximum number of members.
Join and leave a chat room
All the chat users can all joinChatRoom
to join the specified chat room.
By default, when you leave a chat room, the SDK removes all the chat room messages on the local device. If you do not want these messages removed, set setDeleteMessagesAdExitChatRoom
in ChatOptions
as false
.
Retrieve the chat room list and attributes
All chat users can get the chat room list from the server and retrieve the basic information of the specified chat room using the chat room ID.
Listen for chat room events
To monitor the chat room events, you can listen for the callbacks in the ChatRoomManager
class and add app logics accordingly. If you want to stop listening for the callback, make sure that you remove the listener to prevent memory leakage.