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 Chat SDK provides the ChatManager
, ChatMessage
, and ChatThread
classes for thread messages, which 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 Get Started with Android.
- 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
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:
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 ChatThreadChangeListener#onChatThreadUpdated
callback. Thread members can also listen for the MessageListener#onMessageReceived
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
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 ChatThreadChangeListener#onChatThreadUpdated
callback. Thread members can also listen for the MessageListener#onMessageRecalled
callback, as shown in the following code sample:
Retrieve thread messages
Whether to retrieve thread messages from the server or local database depends on your production context.
When you join a thread, messages are displayed in chronological order by default. In this case, Agora recommends that you retrieve the historical messages of the thread from the server. When you recall a thread message, Agora recommend that you insert the recall notification in the local database.
Retrieve thread messages from the server
For details about how to retrieve messages from the server, see Retrieve Historical Messages.
Retrieve the conversation of a thread from the memory and local database
By calling getAllConversations
, you can only retrieve the conversations of one-to-one chats or group chats. To retrieve the conversation of a thread, refer to the following code sample: