Threads enable users to create a separate conversation from a specific message within a chat group to keep the main chat uncluttered, as shown in the following illustration:
This page shows how to use the Agora Chat SDK to create and manage threads in your app.
The Agora Chat SDK 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 onChatThreadChange
callback triggered by the create
event. In a multi-device scenario, all the other devices receive the onMultiDeviceEvent
callback triggered by the chatThreadCreate
event.
The following code sample shows how to create a thread in a chat group:
// parentId: The ID of a chat group where a thread resides.
// threadName: The name of a thread. The maximum length of a thread name is 64 characters.
// messageId: The ID of a message, from which a thread is created.
conn.createChatThread({parentId: 'parentId',name: 'threadName',messageId: 'messageId'})
// Listen for the creating thread callback.
conn.addEventHandler('THREAD',{
onChatThreadChange:(threadMsg) =>{
console.log(threadMsg)
},
});
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 onChatThreadChange
callback triggered by the destroy
event. In a multi-device scenario, all the other devices receive the onMultiDeviceEvent
callback triggered by the chatThreadDestroy
event.
The following code sample shows how to destroy a thread:
// chatThreadId: The ID of a thread that you want to destroy.
conn.destroyChatThread({chatThreadId: 'chatThreadId'})
// Listen for the destroying thread callback.
conn.addEventHandler('THREAD',{
onChatThreadChange:(threadMsg) =>{
console.log(threadMsg)
},
});
All chat group members can refer to the following steps to join a thread:
getChatThreads
, and locate the ID of the thread that you want to join.onChatThreadChange
callback 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 onMultiDeviceEvent
callback triggered by the chatThreadJoin
event.
The following code sample shows how to join a thread:
// chatThreadId: The ID of a thread that you want to join.
conn.joinChatThread({chatThreadId: 'chatThreadId'});
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 onMultiDeviceEvent
callback triggered by the chatThreadLeave
event.
The following code sample shows how to leave a thread:
// chatThreadId: The ID of a thread that you want to leave.
conn.leaveChatThread({chatThreadId: 'chatThreadId'});
Only the chat group owner and admins can call removeChatThreadMember
to remove the specified member from a thread.
Once a member is removed from a thread, they receive the onChatThreadChange
callback triggered by the userRemove
event and can no longer receive the thread messages.
The following code sample shows how to remove a member from a thread:
// chatThreadId: The ID of a thread.
// username: The ID of the user to be removed from a thread.
conn.removeChatThreadMember({chatThreadId: 'chatThreadId',username:'username'});
Only the chat group owner, chat group admins, and thread creator can call changeChatThreadName
to update a thread name.
Once a thread name is updated, all chat group members receive the onChatThreadChange
callback triggered by the update
event. In a multi-device scenario, all the other devices receive the onMultiDeviceEvent
callback triggered by the chatThreadNameUpdate
event.
The following code sample shows how to update a thread name:
// chatThreadId: The ID of a thread.
// name: The updated thread name. The maximum length of a thread name is 64 characters.
conn.changeChatThreadName({chatThreadId: 'chatThreadId',name: 'name'})
// Listen for the updating thread name callback.
conn.addEventHandler('THREAD',{
onChatThreadChange:(threadMsg) =>{
console.log(threadMsg)
},
});
All chat group members can call getChatThreadDetail
to retrieve the thread attributes from the server.
The following code sample shows how to retrieve the thread attributes:
// chatThreadID: The thread ID.
conn.getChatThreadDetail({chatThreadId: 'chatThreadId'}).then((res)=>{
console.log(res)
});
All chat group members can call getChatThreadMembers
to retrieve a paginated member list of a thread from the server, as shown in the following code sample:
// chatThreadId: The thread ID.
// 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.
conn.getChatThreadMembers({chatThreadId: 'chatThreadId ',pageSize:20,cursor:'cursor'}).then((res)=>{
console.log(res)
});
Users can call getJoinedChatThreads
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.
conn.getJoinedChatThreads({cursor: 'cursor',pageSize: 20}).then((res)=>{
console.log(res)
});
Users can call getJoinedChatThreads
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.
conn.getJoinedChatThreads({parentId: 'parentId',cursor: 'cursor',pageSize: 20}).then((res)=>{
console.log(res)
});
Users can also call getChatThreads
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.
conn.getChatThreads({parentId: 'parentId', cursor:'cursor', pageSize: 20}).then((res)=>{
console.log(res)
});
Users can call getChatThreadLastMessage
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.
conn.getChatThreadLastMessage({chatThreadIds: ['chatThreadId1','chatThreadId2']}).then((res)=>{
console.log(res)
});