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 a chat room member goes offline, this member does not receive any push messages from the chat room and automatically leaves the chat room after 5 minutes. Chat rooms are widely applied in live broadcast use cases such as stream chat in Twitch.
This page shows how to use the Agora Chat SDK to create and manage a chat room in your app.
The Agora Chat SDK provides the IAgoraChatroomManager
, AgoraChatroomManagerDelegate
, and AgoraChatRoom
classes for chat room management, which allows you to implement the following features:
Before proceeding, ensure that you meet the following requirements:
This section introduces how to call the APIs provided by the Agora Chat SDK to implement the features listed above.
The app super admin can create a chat room and set the chat room attributes such as the chat room subject, description, and the maximum number of members. Once a chat room is created, the super admin automatically becomes the chat room owner.
Only the chat room owner can disband a chat room. Once a chat room is disbanded, all the chat room members receive the didDismissFromChatroom
callback and are immediately removed from the chat room.
// The super admin can call createChatroomWithSubject to create a chat room.
AgoraChatError *error = nil;
AgoraChatroom *retChatroom = [[AgoraChatClient sharedClient].roomManager createChatroomWithSubject:@"aSubject" description:@"aDescription" invitees:@[@"user1",@[user2]]message:@"aMessage" maxMembersCount:aMaxMembersCount error:&error];
// The chat room owner can call destroyChatroom to disband a chat room.
AgoraChatError *error = nil;
[[AgoraChatClient sharedClient].roomManager destroyChatroom:self.chatroom.chatroomId error:&error];
All chat users can call joinChatroom
to join the specified chat room. Once a chat user joins a chat room, all the other chat room members receive the userDidJoinChatroom
callback.
All chat room members can call leaveChatroom
to leave the specified chat room. Once a chat room member leaves a chat room, all the other members receive the userDidLeaveChatroom
callback and all the local data is deleted by default. To retain data on the local device, set the isDeleteMessagesWhenExitChatRoom
parameter of AgoraChatOptions
to NO
.
// All chat users can call joinChatroom to join the specified chat room.
AgoraChatError *error = nil;
[[AgoraChatClient sharedClient].roomManager joinChatroom:@"aChatroomId" error:&error];
// All chat room members can call leaveChatroom to leave the specified chat room.
AgoraChatError *error = nil;
[AgoraChatClient sharedClient].roomManager leaveChatroom:@"aChatroomId" error:&error];
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.
// Chat room members can call getChatroomsFromServerWithPage to retrieve the specified number of chat rooms from the server by page. The maximum value of pageSize is 1,000.
AgoraChatError *error = nil;
[[AgoraChatClient sharedClient].roomManager getChatroomsFromServerWithPage:1 pageSize:50 error:&error];
// Chat room members can call chatroomWithId to get the basic information of the specified chat room by passing the chat room ID.
AgoraChatroom *chatRoom = [AgoraChatroom chatroomWithId:@"chatroomId"];
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.
// Listen for the callback.
[[AgoraChatClient sharedClient].roomManager addDelegate:self delegateQueue:nil];
// Stop listening for the callback.
[[AgoraChatClient sharedClient].roomManager removeDelegate:self];
/**
* Occurs when a user joins a chat room.
* @param aChatroom The chat room ID
* @param aUsername The username of the new chat room member
*/
- (void)userDidJoinChatroom:(AgoraChatroom *)aChatroom
user:(NSString *)aUsername{
}
/**
* Occurs when a member leaves a chat room.
* @param aChatroom The chat room ID
* @param aUsername The username of the member that leaves the chat room
*/
- (void)userDidLeaveChatroom:(AgoraChatroom *)aChatroom
user:(NSString *)aUsername {
}
/**
* Occurs when a member is removed from a chat room.
* @param aChatroom The chat room ID
* @param aReason The reason why this member is removed
*/
- (void)didDismissFromChatroom:(AgoraChatroom *)aChatroom
reason:(AgoraChatroomBeKickedReason)aReason {
}
/**
* Occurs when a member is added to the chat room mute list.
* @param aChatroom The chat room ID
* @param aMutedMembers The username of the member added to the must list
* @param aMuteExpire The Unix timestamp when the mute expires. Not currently available.
*/
- (void)chatroomMuteListDidUpdate:(AgoraChatroom *)aChatroom
addedMutedMembers:(NSArray *)aMutes
muteExpire:(NSInteger)aMuteExpire {
}
/**
* Occurs when a member is removed from the chat room mute list.
* @param aChatroom The chat room ID
* @param aMutedMembers The username of the member removed from the mute list
*/
- (void)chatroomMuteListDidUpdate:(AgoraChatroom *)aChatroom
removedMutedMembers:(NSArray *)aMutes {
}
/**
* Occurs when a member is added to the chat room admin list.
* @param aChatroom The chat room ID
* @param aAdmin The username of the member added to the admin list
*/
- (void)chatroomAdminListDidUpdate:(AgoraChatroom *)aChatroom
addedAdmin:(NSString *)aAdmin {
}
/**
* Occurs when a member is removed from the chat room admin list.
* @param aChatroom The chat room ID
* @param aAdmin The username of the admin removed from the admin list
*/
- (void)chatroomAdminListDidUpdate:(AgoraChatroom *)aChatroom
removedAdmin:(NSString *)aAdmin {
}
/**
* Occurs when the chat room owner is changed.
* @param aChatroom The chat room ID.
* @param aNewOwner The username of the new chat room owner.
* @param aOldOwner The username of the original chat room owner.
*/
- (void)chatroomOwnerDidUpdate:(AgoraChatroom *)aChatroom
newOwner:(NSString *)aNewOwner
oldOwner:(NSString *)aOldOwner {
}