Thread messages
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 Chat SDK to send, receive, recall, and retrieve thread messages in your app.
Understand the tech
The Agora Chat SDK allows you to implement the following features:
- Send a thread message
- Receive a thread message
- Recall a thread message
- Retrieve thread messages
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:
- Clients retrieve a token from your app server.
- Client A and Client B log in to Chat.
- Client A sends a message to Client B. The message is sent to the Chat server and the server delivers the message to Client B. When Client B receives the message, the SDK triggers an event. Client B listens for the event and gets the message.
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.
Implementation
This section describes how to call the APIs provided by the Chat SDK to implement thread features.
Send a thread message
Send a thread message is similar to send a message in a chat group. The difference lies in the IsThread
field, as shown in the following code sample:
For more information about sending a message, see Send Messages.
Receive a thread message
Once a thread has a new message, all chat group members receive the IChatThreadManagerDelegate#OnUpdateMyThread
callback. Thread members can also listen for the IChatManagerDelegate#OnMessagesReceived
callback to receive thread messages, as shown in the following code sample:
For more information about receiving a message, see Receive Messages.
Recall a thread message
Send a thread message is similar to send a message in a chat group. The difference lies in the isChatThread
field.
Once a message is recalled in a thread, all chat group members receive the IChatThreadManagerDelegate#OnUpdateMyThread
callback. Thread members can also listen for the IChatManagerDelegate#OnMessagesRecalled
callback, as shown in the following code sample:
For more information about recalling a message, see Recall Messages.
Retrieve thread messages
You can retrieve thread messages locally or from the server, depending on your production environment.
You can check Conversation#IsThread()
to determine whether the current conversation is a thread conversation.
Retrieve messages of a thread from the server
You can call ChatManager#FetchHistoryMessagesFromServer
to retrieve messages of a thread from the server. The only difference between retrieving messages of a thread from the server and retrieving group messages is that a thread ID needs to be passed in for the former and a group ID is required for the latter.
Retrieve messages of a thread locally
By calling ChatManager#LoadAllConversations
, you can only retrieve local one-to-one chat conversations and group conversations. To retrieve messages of a thread locally, refer to the following code sample: