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 theRoom
, IRoomManager
, and IRoomManagerDelegate
classes for chat room management, which allow 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 a chat room
Only the app super admin can call CreateRoom
to create a chat room and set the chat room attributes such as the chat room name, description, and maximum number of members. Once a chat room is created, the super admin automatically becomes the chat room owner.
The following code sample shows how to create a chat room:
Destroy a chat room
Only the chat room owner can call DestroyRoom
to disband a chat room. Once a chat room is disbanded, all chat room members receive the OnDestroyedFromRoom
callback and are immediately removed from the chat room.
The following code sample shows how to destroy a chat room:
Join a chat room
Refer to the following steps to join a chat room:
-
Call
FetchPublicRoomsFromServer
to retrieve the list of chat rooms from the server and locate the ID of the chat room that you want to join. -
Call
JoinRoom
to pass in the chat room ID and join the specified chat room. Once a user joins a chat room, all the other chat room members receive theOnMemberJoinedFromRoom
callback.
The following code sample shows how to join a chat room:
Leave a chat room
All chat room members can call LeaveRoom
to leave the specified chat room. Once a member leaves the chat room, all the other chat room members receive the OnMemberExitedFromRoom
callback.
Note: Unlike chat group owners (who cannot leave their groups), a chat room owner can leave a chat room. After re-entering the chat room, this user remains the chat room owner.
The following code sample shows how to leave a chat room:
By default, after a user leaves a chat room, the Chat SDK removes all chat room messages on the local device. If you do not want these messages removed, set Options#DeleteMessagesAsExitRoom
to false
when initializing the SDK.
The following code sample shows how to retain the chat room messages after leaving a chat room:
Retrieve the chat room attributes
All chat room members can call FetchRoomInfoFromServer
to retrieve the attributes of the a chat room, including the chat room ID, name, description, announcements, owner, admin list, member list, block list, mute list, maximum number of members, and whether all members are muted.
The following code sample shows how to retrieve the chat room attributes:
Retrieve the chat room list from the server
Users can call FetchPublicRoomsFromServer
to get the chat room list from the server. You can get a maximum of 1,000 chat rooms at each call.
Listen for chat room events
To monitor the chat room events, you can listen for the callbacks in the IRoomManagerDelegate
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.
The following code sample shows how to add and remove the chat room listener:
Refer to the following code sample to listen for chat room events: