Threads enable users to create a separate conversation from a specific message within a chat group to keep the main chat uncluttered.
This page shows how to use the Agora Chat SDK to send, receive, recall, and retrieve thread messages in your app.
The Agora Chat SDK provides the AgoraChatManager
, AgoraChatMessage
, and AgoraChatThread
classes for thread messages, which allows you to implement the following features:
The following figure shows the workflow of how clients send and receive peer-to-peer messages.
As shown in the figure, the workflow of peer-to-peer messaging is as follows:
Before proceeding, ensure that you meet the following requirements:
This section describes how to call the APIs provided by the Agora Chat SDK to implement thread features.
Sending a thread message is similar to sending a message in a chat group. The difference lies in the isChatThread
field, as shown in the following code sample:
// Calls `initWithConversationID` to create a text message.
// Sets `*message` to the content of the text message.
// Sets `*to` to the ID of a thread that receives the text message.
NSString *from = [[AgoraChatClient sharedClient] currentUsername];
NSString *to = self.currentConversation.conversationId;
AgoraChatMessage *message = [[AgoraChatMessage alloc] initWithConversationID:to from:from to:to body:aBody ext:aExt];
// Specifies whether a read receipt is required for this text message.
if([aExt objectForKey:MSG_EXT_READ_RECEIPT]) {
message.isNeedGroupAck = YES;
}
// Sets `chatType` to `AgoraChatTypeGroupChat` as a thread that belongs to a chat group.
message.chatType = (AgoraChatType)self.conversationType;
// Sets `isChatThread` to `YES` to mark this message as a thread message.
message.isChatThreadMessage = self.isChatThread;
// Calls `sendMessage` to send the text message.
[[AgoraChatClient sharedClient].chatManager sendMessage:message progress:nil completion:^(AgoraChatMessage *message, AgoraChatError *error) {
}];
For more information about sending a message, see Send Messages.
Once a thread has a new message, all chat group members receive the AgoraChatThreadManagerDelegate#onChatThreadUpdated
callback. Thread members can also listen for the AgoraChatManagerDelegate#messagesDidReceive
callback to receive thread messages, as shown in the following code sample:
// The SDK triggers the `messagesDidReceive` callback when it receives a message.
// After receiving this callback, the SDK parses the message and displays it.
- (void)messagesDidReceive:(NSArray *)aMessages
{
// You can implement subsequent settings in this callback.
}
// Calls `addDelegate` to add a message listener.
[[AgoraChatClient sharedClient].chatManager addDelegate:self delegateQueue:nil];
// Calls `removeDelegate` to remove the message listener.
[[AgoraChatClient sharedClient].chatManager removeDelegate:self];
For more information about receiving a message, see Receive Messages.
For details about how to recall a message, refer to Recall Messages.
Once a message is recalled in a thread, all chat group members receive the AgoraChatThreadManagerDelegate#onChatThreadUpdated
callback. Thread members can also listen for the AgoraChatManagerDelegate#messagesInfoDidRecall
callback, as shown in the following code sample:
// The SDK triggers the `messagesInfoDidRecall` callback when it recalls a message.
// After receiving this callback, the SDK parses the message and updates its display.
- (void)messagesInfoDidRecall:(NSArray<EMRecallMessageInfo *> *)aRecallMessagesInfo
{}
For details about how to retrieve messages from the server, see Retrieve Historical Messages.