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 ChatThreadManager
, ChatMessage
, and ChatThread
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 isChatThreadMessage
field, as shown in the following code sample:
// Sets `targetGroup` to the ID of the chat group that receives the message.
// Sets `content` to the message content.
ChatMessage msg = ChatMessage.createTxtSendMessage(
targetId: targetGroup,
content: content,
);
// Sets `ChatType` to GroupChat as a thread that belongs to a chat group.
msg.chatType = ChatType.GroupChat;
// Sets `isChatThreadMessage` to `true` to mark this message as a thread message.
msg.isChatThreadMessage = true;
// Sends the message.
ChatClient.getInstance.chatManager.sendMessage(msg);
For more information about sending a message, see Send Messages.
Once a thread has a new message, all chat group members receive the ChatThreadEventHandler#onChatThreadUpdated
callback. Thread members can also listen for the ChatEventHandler#onMessagesReceived
callback to receive thread messages, as shown in the following code sample:
// Adds the chat event handler.
ChatClient.getInstance.chatManager.addEventHandler(
"UNIQUE_HANDLER_ID",
ChatEventHandler(
onMessagesReceived: (messages) {},
),
);
// Adds the thread event handler.
ChatClient.getInstance.chatThreadManager.addEventHandler(
"UNIQUE_HANDLER_ID",
ChatThreadEventHandler(
onChatThreadUpdate: (event) {},
),
);
...
// Removes the chat event handler.
ChatClient.getInstance.chatManager.removeEventHandler("UNIQUE_HANDLER_ID");
// Removes the chat thread event handler.
ChatClient.getInstance.chatThreadManager.removeEventHandler("UNIQUE_HANDLER_ID");
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 ChatThreadEventHandler#onChatThreadUpdated
callback. Thread members can also listen for the ChatEventHandler#onMessagesRecalled
callback, as shown in the following code sample:
// Adds the chat event handler.
ChatClient.getInstance.chatManager.addEventHandler(
"UNIQUE_HANDLER_ID",
ChatEventHandler(
onMessagesRecalled: (messages) {},
),
);
// Adds the thread event handler.
ChatClient.getInstance.chatThreadManager.addEventHandler(
"UNIQUE_HANDLER_ID",
ChatThreadEventHandler(
onChatThreadUpdate: (event) {},
),
);
...
// Removes the chat event handler.
ChatClient.getInstance.chatManager.removeEventHandler("UNIQUE_HANDLER_ID");
// Removes the chat thread event handler.
ChatClient.getInstance.chatThreadManager.removeEventHandler("UNIQUE_HANDLER_ID");
For details about how to retrieve messages from the server, see Retrieve Conversations and Messages from Server.