Thread management
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 Chat SDK to create and manage threads in your app.
Understand the tech
The Chat SDK for Unity provides the IChatThreadManager
, ChatThread
, ChatThreadEvent
, and IChatThreadManagerDelegate
classes for thread management, which allow you to implement the following features:
- Create and destroy a thread
- Join and leave a thread
- Remove a member from a thread
- Update the name of a thread
- Retrieve the attributes of a thread
- Retrieve the member list of a thread
- Retrieve a thread list
- Retrieve the latest message from multiple threads
- Listen for thread events
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.
Create a thread
All chat group members can call CreateThread
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 IChatThreadManagerDelegate#OnCreateThread
callback. In a multi-device scenario, all the other devices receive the IMultiDeviceDelegate#onThreadMultiDevicesEvent
callback triggered by the THREAD_CREATE
event.
The following sample code shows how to create a thread in a chat group:
Destroy a thread
Only the chat group owner and admins can call DestroyThread
to disband a thread in a chat group.
Once a thread is disbanded, all chat group members receive the IChatThreadManagerDelegate#onThreadNotifyChange
callback. In a multi-device scenario, all the other devices receive the IMultiDeviceDelegate#onThreadMultiDevicesEvent
callback triggered by the THREAD_DESTROY
event.
The following sample code shows how to destroy a thread:
Join a thread
All chat group members can call JoinThread
to join a thread as follows:
- Retrieve the thread ID from the
IChatThreadManagerDelegate#OnCreateThread
orIChatThreadManagerDelegate#onThreadNotifyChange
callback. Or retrieve the thread list in a chat group by callingFetchThreadListOfGroup
, and locate the ID of the thread that you want to join. - Call
JoinThread
to pass in the thread ID and join the specified thread.
In a multi-device scenario, all the other devices receive the IMultiDeviceDelegate#onThreadMultiDevicesEvent
callback triggered by the THREAD_JOIN
event.
The following sample code shows how to join a thread:
Leave a thread
All thread members can call LeaveThread
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 IMultiDeviceDelegate#onThreadMultiDevicesEvent
callback triggered by the THREAD_LEAVE
event.
The following sample code shows how to leave a thread:
Remove a member from a thread
Only the chat group owner and admins can call RemoveThreadMember
to remove the specified member from a thread.
Once a member is removed from a thread, they receive the IChatThreadManagerDelegate#OnUserKickOutOfChatThread
callback and can no longer receive the thread messages. In a multi-device scenario, all the other devices receive the IMultiDeviceDelegate#onThreadMultiDevicesEvent
callback triggered by the THREAD_KICK
event.
The following sample code shows how to remove a member from a thread:
Update the name of a thread
Only the chat group owner, chat group admins, and thread creator can call ChangeThreadSubject
to update a thread name.
Once a thread name is updated, all chat group members receive the IChatThreadManagerDelegate#OnChatThreadUpdate
callback. In a multi-device scenario, all the other devices receive the IMultiDeviceDelegate#onThreadMultiDevicesEvent
callback triggered by the THREAD_UPDATE
event.
The following sample code shows how to update a thread name:
Retrieve the attributes of a thread
All chat group members can call GetThreadDetail
to retrieve the thread attributes from the server.
The following sample code shows how to retrieve the thread attributes:
Retrieve the member list of a thread
All chat group members can call FetchThreadMembers
to retrieve a paginated member list of a thread from the server, as shown in the following sample code:
Retrieve a thread list
Users can call FetchMineJoinedThreadList
to retrieve a paginated list from the server of all the threads they have created and joined, as shown in the following sample code:
Besides, users can call FetchThreadListOfGroup
to retrieve a paginated list from the server of all the threads in a specified chat group, as shown in the following sample code:
Retrieve the latest message from multiple threads
Users can call GetLastMessageAccordingThreads
to retrieve the latest message from multiple threads.
The following sample code shows how to retrieve the latest message from multiple threads:
Listen for thread events
To monitor thread events, users can listen for the callbacks in the IChatThreadManagerDelegate
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 sample code to listen for thread events: