Threads enable users to create a separate conversation from a specific message within a chat group to keep the main chat uncluttered.
The following illustration shows the implementation of creating a thread, a conversation in a thread, and the operations you can perform in a thread:
This page shows how to use the Agora Chat SDK to create and manage threads in your app.
The Agora Chat SDK provides the ChatManager
, ChatMessageThread
, ChatMessageEventListener
, and ChatMessageThreadEvent
classes for thread management, which allows you to implement the following features:
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.
All chat group members can call createChatThread
to create a thread from a specific message in a chat group.
Once a thread is created in a chat group, all chat group members receive the ChatMessageEventListener#onChatMessageThreadCreated
callback. In a multi-device scenario, all the other devices receive the ChatMultiDeviceEventListener#onThreadEvent
callback triggered by the THREAD_CREATE
event.
The following code sample shows how to create a thread in a chat group:
// name: The name of a thread. The maximum length of a thread name is 64 characters.
// msgId: The ID of a message, from which a thread is created.
// parentId: The ID of a chat group where a thread resides.
ChatClient.getInstance()
.chatManager.createChatThread(name, msgId, parentId)
.then((result) => {
console.log("success: ", result);
})
.catch((error) => {
console.log("fail: ", error);
});
Only the chat group owner and admins can call destroyChatThread
to disband a thread in a chat group.
Once a thread is disbanded, all chat group members receive the ChatMessageEventListener#onChatMessageThreadDestroyed
callback. In a multi-device scenario, all the other devices receive the ChatMultiDeviceEventListener#onThreadEvent
callback triggered by the THREAD_DESTROY
event.
The following code sample shows how to destroy a thread:
// chatThreadID: The ID of a thread.
ChatClient.getInstance()
.chatManager.destroyChatThread(chatThreadID)
.then((result) => {
console.log("success: ", result);
})
.catch((error) => {
console.log("fail: ", error);
});
All chat group members can refer to the following steps to join a thread:
fetchChatThreadWithParentFromServer
, and locate the ID of the thread that you want to join.ChatMessageEventListener#onChatMessageThreadCreated
and ChatMessageEventListener#onChatMessageThreadUpdated
callbacks that you receive.joinChatThread
to pass in the thread ID and join the specified thread.In a multi-device scenario, all the other devices receive the ChatMultiDeviceEventListener#onThreadEvent
callback triggered by the THREAD_JOIN
event.
The following code sample shows how to join a thread:
// chatThreadID: The ID of a thread.
ChatClient.getInstance()
.chatManager.joinChatThread(chatThreadID)
.then((result) => {
console.log("success: ", result);
})
.catch((error) => {
console.log("fail: ", error);
});
All thread members can call leaveChatThread
to leave a thread. Once a member leaves a thread, they can no longer receive the thread messages.
In a multi-device scenario, all the other devices receive the ChatMultiDeviceEventListener#onThreadEvent
callback triggered by the THREAD_LEAVE
event.
The following code sample shows how to leave a thread:
// chatThreadID: The ID of a thread.
ChatClient.getInstance()
.chatManager.leaveChatThread(chatThreadID)
.then((result) => {
console.log("success: ", result);
})
.catch((error) => {
console.log("fail: ", error);
});
Only the chat group owner and admins can call removeMemberWithChatThread
to remove the specified member from a thread.
Once a member is removed from a thread, they receive the ChatMessageEventListener#onUserRemoved
callback and can no longer receive the thread messages. In a multi-device scenario, all the other devices receive the ChatMultiDeviceEventListener#onThreadEvent
callback triggered by the THREAD_KICK
event.
The following code sample shows how to remove a member from a thread:
// chatThreadID: The ID of a thread.
// member: The ID of the user to be removed from a thread.
ChatClient.getInstance()
.chatManager.removeMemberWithChatThread(chatThreadID, memberId)
.then((result) => {
console.log("success: ", result);
})
.catch((error) => {
console.log("fail: ", error);
});
Only the chat group owner, chat group admins, and thread creator can call updateChatThreadName
to update a thread name.
Once a thread name is updated, all chat group members receive the ChatMessageEventListener#onChatMessageThreadUpdated
callback. In a multi-device scenario, all the other devices receive the ChatMultiDeviceEventListener#onThreadEvent
callback triggered by the THREAD_UPDATE
event.
The following code sample shows how to update a thread name:
// chatThreadID: The ID of a thread.
// newChatThreadName: The updated thread name. The maximum length of a thread name is 64 characters.
ChatClient.getInstance()
.chatManager.updateChatThreadName(chatThreadID, newName)
.then((result) => {
console.log("success: ", result);
})
.catch((error) => {
console.log("fail: ", error);
});
All chat group members can call fetchChatThreadFromServer
to retrieve the thread attributes from the server.
The following code sample shows how to retrieve the thread attributes:
// chatThreadID: The ID of a thread.
ChatClient.getInstance()
.chatManager.fetchChatThreadFromServer(chatThreadID)
.then((result) => {
console.log("success: ", result);
})
.catch((error) => {
console.log("fail: ", error);
});
All chat group members can call fetchMembersWithChatThreadFromServer
to retrieve a paginated member list of a thread from the server, as shown in the following code sample:
// chatThreadId: The ID of a thread.
// pageSize: The maximum number of members to retrieve per page. The range is [1, 50].
// cursor: The position from which to start getting data. Pass in `null` or an empty string at the first call.
ChatClient.getInstance()
.chatManager.fetchMembersWithChatThreadFromServer(
chatThreadID,
cursor,
pageSize
)
.then((result) => {
console.log("success: ", result);
})
.catch((error) => {
console.log("fail: ", error);
});
Users can call fetchJoinedChatThreadFromServer
to retrieve a paginated list from the server of all the threads they have joined, as shown in the following code sample:
// pageSize: The maximum number of threads to retrieve per page. The range is [1, 50].
// cursor: The position from which to start getting data. Pass in `null` or an empty string at the first call.
ChatClient.getInstance()
.chatManager.fetchJoinedChatThreadFromServer(cursor, pageSize)
.then((result) => {
console.log("success: ", result);
})
.catch((error) => {
console.log("fail: ", error);
});
Users can call fetchJoinedChatThreadWithParentFromServer
to retrieve a paginated list from the server of all the threads they have joined in a specified chat group, as shown in the following code sample:
// parentId: The chat group ID.
// pageSize: The maximum number of threads to retrieve per page. The range is [1, 50].
// cursor: The position from which to start getting data. Pass in `null` or an empty string at the first call.
ChatClient.getInstance()
.chatManager.fetchJoinedChatThreadWithParentFromServer(
parentId,
cursor,
pageSize
)
.then((result) => {
console.log("success: ", result);
})
.catch((error) => {
console.log("fail: ", error);
});
Users can also call fetchChatThreadWithParentFromServer
to retrieve a paginated list from the server of all the threads in a specified chat group, as shown in the following code sample:
// parentId: The chat group ID.
// pageSize: The maximum number of threads to retrieve per page. The range is [1, 50].
// cursor: The position from which to start getting data. Pass in `null` or an empty string at the first call.
ChatClient.getInstance()
.chatManager.fetchChatThreadWithParentFromServer(parentId, cursor, pageSize)
.then((result) => {
console.log("success: ", result);
})
.catch((error) => {
console.log("fail: ", error);
});
Users can call fetchLastMessageWithChatThread
to retrieve the latest message from multiple threads.
The following code sample shows how to retrieve the latest message from multiple threads:
// chatThreadIDs: The thread IDs. You can pass in a maximum of 20 thread IDs at each call.
ChatClient.getInstance()
.chatManager.fetchLastMessageWithChatThread(chatThreadIDs)
.then((result) => {
console.log("success: ", result);
})
.catch((error) => {
console.log("fail: ", error);
});
To monitor thread events, users can listen for the callbacks in the ChatManager
class and add app logics accordingly. If a user wants to stop listening for the callbacks, make sure that the user removes the listener to prevent memory leakage.
Refer to the following code sample to listen for thread events:
// Inherits and implements ChatMessageEventListener.
class ChatMessageEvent implements ChatMessageEventListener {
// Occurs when a thread is created.
onChatMessageThreadCreated(msgThread:
ChatMessageThreadEvent): void {
console.log(`onChatMessageThreadCreated: `, msgThread);
}
// Occurs when a thread has a new message, a thread name is updated, or a thread message is recalled.
onChatMessageThreadUpdated(msgThread: ChatMessageThreadEvent): void {
console.log(`onChatMessageThreadUpdated: `, msgThread);
}
// Occurs when a thread is destroyed.
onChatMessageThreadDestroyed(msgThread: ChatMessageThreadEvent): void {
console.log(`onChatMessageThreadDestroyed: `, msgThread);
}
// Occurs when a member is removed from a thread.
onChatMessageThreadUserRemoved(msgThread: ChatMessageThreadEvent): void {
console.log(`onChatMessageThreadUserRemoved: `, msgThread);
}
}
const listener = new ChatMessageEvent();
// Adds the message listener.
ChatClient.getInstance().chatManager.addMessageListener(listener);
// Removes the message listener.
ChatClient.getInstance().chatManager.removeMessageListener(listener);
// Removes all the message listeners.
ChatClient.getInstance().chatManager.removeAllMessageListener();