Chat groups enable real-time messaging among multiple users.
This page shows how to use the Agora Chat SDK to manage the attributes of a chat group in your app.
The Agora Chat SDK provides the ChatGroup
, ChatGroupManager
, and ChatGroupEventListener
classes for chat group 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 chat group features.
The chat group owner and admins can call changeGroupName
to set and update the chat group name. The maximum length of a chat group name is 128 characters.
The following code sample shows how to update the chat group name:
ChatClient.getInstance()
.groupManager.changeGroupName(groupId, local_name)
.then(() => {
console.log("update group name success.");
})
.catch((reason) => {
console.log("update group name fail.", reason);
});
The chat group owner and admins can call changeGroupDescription
to set and update the chat group description. The maximum length of a chat group description is 512 characters.
The following code sample shows how to update the chat group description:
ChatClient.getInstance()
.groupManager.changeGroupDescription(groupId, desc)
.then(() => {
console.log("update group description success.");
})
.catch((reason) => {
console.log("update group description fail.", reason);
});
Only the chat group owner and admins can call updateGroupAnnouncement
to set and update the announcements. Once the chat group announcements are updated, all the other chat group members receive the IGroupManagerDelegate#OnAnnouncementChangedFromGroup
callback. The maximum total length of chat group announcements is 512 characters.
The following code sample shows how to update the chat group announcements:
ChatClient.getInstance()
.groupManager.updateGroupAnnouncement(groupId, announcement)
.then(() => {
console.log("update ann success.");
})
.catch((reason) => {
console.log("update ann fail.", reason);
});
All chat group members can call fetchAnnouncementFromServer
to retrieve the chat group announcements from the server.
The following code sample shows how to retrieve the chat group announcements:
ChatClient.getInstance()
.groupManager.fetchAnnouncementFromServer(groupId)
.then((ann) => {
console.log("get ann success.", ann);
})
.catch((reason) => {
console.log("get ann fail.", reason);
});
All chat group members can call uploadGroupSharedFile
to upload shared files to a chat group. The maximum file size is 10 MB. Once a shared file is uploaded, all the other chat group members receive the ChatGroupEventListener#onSharedFileAdded
callback.
The following code sample shows how to upload chat group shared files:
// Set the file status callback. This callback is triggered to notify the upload and download progress, and the success and failure state of operations on shared files.
const callback = new (class implements ChatGroupFileStatusCallback {
that: any;
constructor(t: any) {
this.that = t;
}
onProgress(groupId: string, filePath: string, progress: number): void {
console.log(`onProgress: `, groupId, filePath, progress);
}
onError(groupId: string, filePath: string, error: ChatError): void {
console.log(`onError: `, groupId, filePath, error);
}
onSuccess(groupId: string, filePath: string): void {
console.log(`onSuccess: `, groupId, filePath);
}
})(this);
ChatClient.getInstance()
.groupManager.uploadGroupSharedFile(groupId, filePath, callback)
.then(() => {
console.log("upload file success.");
})
.catch((reason) => {
console.log("upload file fail.", reason);
});
All chat group members can call removeGroupSharedFile
to delete shared files in a chat group. Once a shared file is deleted, all the other chat group members receive the ChatGroupEventListener#onSharedFileDeleted
callback. The chat group owner and chat group admins can delete all of the shared files, whereas chat group members can only delete the shared files that they have personally uploaded.
The following code sample shows how to delete chat group shared files:
ChatClient.getInstance()
.groupManager.removeGroupSharedFile(groupId, fileId)
.then(() => {
console.log("remove file success.");
})
.catch((reason) => {
console.log("remove file fail.", reason);
});
All chat group members can call fetchGroupFileListFromServer
to retrieve the list of shared files in a chat group from the server.
The following code sample shows how to retrieve the list of shared files in a chat group:
// Retrieve the shared files with pagination.
ChatClient.getInstance()
.groupManager.fetchGroupFileListFromServer(groupId, pageSize, pageNum)
.then(() => {
console.log("get file success.");
})
.catch((reason) => {
console.log("get file fail.", reason);
});
Only the chat group owner and admins can call updateGroupExtension
to update the extension fields of a chat group. The extension fields enable users to perform customized operations on a chat group. The maximum length of each extension field is 8 KB, and it must be in the JSON format.
The following code sample shows how to update the chat group extension fields:
// Set the extension fields.
const extension = { key: "value" };
ChatClient.getInstance()
.groupManager.updateGroupExtension(groupId, JSON.stringify(extension))
.then(() => {
console.log("update success.");
})
.catch((reason) => {
console.log("update fail.", reason);
});
For details, see Chat Group Events.