Skip to main content

Chatroom Attribute Management (KV)

The chatroom attribute (KV) management interface is used to set custom attributes in a specified chatroom.

In voice live streaming chatroom scenarios, this feature can be used to record the attributes of each microphone position in the chatroom. In card game scenarios like Werewolf, it can be used to record user roles and game statuses.

Limitations

tip
  • Once a chatroom is destroyed, its custom attributes are also destroyed.
  • Each chatroom can store up to 100 attributes in Key-Value pairs.
  • The client SDK does not limit the frequency of KV operations in chatrooms. It is recommended to keep the frequency of Key-Value operations below 100 times per second per chatroom (one operation involving 100 KVs is equivalent to 100 operations).

Enabling the Service

To use the chatroom attribute (KV) interface, you need to enable the Chatroom Custom Attributes service. You can activate this service on the Basic Features page in the Console.

If a server-side callback URL is configured, the RC server will synchronize chatroom attribute changes (such as setting, deleting, or clearing all attributes) to the specified callback address. For more details, refer to the server-side documentation Chatroom Attribute Synchronization (KV).

Adding Chatroom KV

Method


addChatRoomEntry(
targetId: string,
key: string,
value: string,
deleteWhenLeft: boolean,
overwrite: boolean,
callback: IRCIMIWAddChatRoomEntryCallback
): Promise<number>;

Parameter Description

ParameterTypeDescription
targetIdstringThe chatroom conversation ID
keystringThe chatroom attribute name. The key can include uppercase and lowercase letters, numbers, and special characters + = - _, with a maximum length of 128 characters
valuestringThe value corresponding to the chatroom attribute, with a maximum length of 4096 characters
deleteWhenLeftbooleanWhether to automatically delete the Key-Value pair when the user disconnects or leaves
overwritebooleanWhether to overwrite if the key already exists
callbackIRCIMIWAddChatRoomEntryCallbackCallback for the interface result.

Return Value

Return ValueDescription
numberThe status code of the current operation. 0 indicates success, and the specific result is returned via the callback. Non-zero values indicate failure, and no callback will be triggered. Refer to the error codes for details.

Code Example


const callback = {
onChatRoomEntryAdded: (code: number) => {
//...
},
};
let code = await engine.addChatRoomEntry(targetId, key, value, deleteWhenLeft, overwrite, callback);

Adding Multiple Chatroom KVs

Method


addChatRoomEntries(
targetId: string,
entries: Map<string, string>,
deleteWhenLeft: boolean,
overwrite: boolean,
callback: IRCIMIWAddChatRoomEntriesCallback
): Promise<number>;

Parameter Description

ParameterTypeDescription
targetIdstringThe chatroom conversation ID
entriesMap<string, string>The chatroom attributes
deleteWhenLeftbooleanWhether to automatically delete the Key-Value pairs when the user disconnects or leaves
overwritebooleanWhether to force overwrite
callbackIRCIMIWAddChatRoomEntriesCallbackCallback for the interface result.

Return Value

Return ValueDescription
numberThe status code of the current operation. 0 indicates success, and the specific result is returned via the callback. Non-zero values indicate failure, and no callback will be triggered. Refer to the error codes for details.

Code Example


const callback = {
onChatRoomEntriesAdded: (code: number, errors: Map<string, number>) => {
//...
},
};
let code = await engine.addChatRoomEntries(targetId, entries, deleteWhenLeft, overwrite, callback);

Loading Chatroom KV

Method


getChatRoomEntry(
targetId: string,
key: string,
callback: IRCIMIWGetChatRoomEntryCallback
): Promise<number>;

Parameter Description

ParameterTypeDescription
targetIdstringThe chatroom conversation ID
keystringThe chatroom attribute key
callbackIRCIMIWGetChatRoomEntryCallbackCallback for the interface result.

Return Value

Return ValueDescription
numberThe status code of the current operation. 0 indicates success, and the specific result is returned via the callback. Non-zero values indicate failure, and no callback will be triggered. Refer to the error codes for details.

Code Example


const callback = {
onSuccess: (t: Map<string, string>) => {
//...
},
onError: (code: number) => {
//...
},
};
let code = await engine.getChatRoomEntry(targetId, key, callback);

Loading All KVs in a Chatroom

Method


getChatRoomAllEntries(
targetId: string,
callback: IRCIMIWGetChatRoomAllEntriesCallback
): Promise<number>;

Parameter Description

ParameterTypeDescription
targetIdstringThe chatroom conversation ID
callbackIRCIMIWGetChatRoomAllEntriesCallbackCallback for the interface result.

Return Value

Return ValueDescription
numberThe status code of the current operation. 0 indicates success, and the specific result is returned via the callback. Non-zero values indicate failure, and no callback will be triggered. Refer to the error codes for details.

Code Example


const callback = {
onSuccess: (t: Map<string, string>) => {
//...
},
onError: (code: number) => {
//...
},
};
let code = await engine.getChatRoomAllEntries(targetId, callback);

Removing Chatroom KV

Method


removeChatRoomEntry(
targetId: string,
key: string,
force: boolean,
callback: IRCIMIWRemoveChatRoomEntryCallback
): Promise<number>;

Parameter Description

ParameterTypeDescription
targetIdstringThe chatroom conversation ID
keystringThe chatroom attribute key
forcebooleanWhether to force deletion
callbackIRCIMIWRemoveChatRoomEntryCallbackCallback for the interface result.

Return Value

Return ValueDescription
numberThe status code of the current operation. 0 indicates success, and the specific result is returned via the callback. Non-zero values indicate failure, and no callback will be triggered. Refer to the error codes for details.

Example Code


const callback = {
onChatRoomEntryRemoved: (code: number) => {
//...
},
};
let code = await engine.removeChatRoomEntry(targetId, key, force, callback);

Removing Multiple Chatroom KVs

Method


removeChatRoomEntries(
targetId: string,
keys: Array<string>,
force: boolean,
callback: IRCIMIWRemoveChatRoomEntriesCallback
): Promise<number>;

Parameter Description

ParameterTypeDescription
targetIdstringThe chatroom conversation ID
keysArray<string>The chatroom attributes
forcebooleanWhether to force overwrite
callbackIRCIMIWRemoveChatRoomEntriesCallbackCallback for the interface result.

Return Value

Return ValueDescription
numberThe status code of the current operation. 0 indicates success, and the specific result is returned via the callback. Non-zero values indicate failure, and no callback will be triggered. Refer to the error codes for details.

Code Example


const callback = {
onChatRoomEntriesRemoved: (code: number) => {
//...
},
};
let code = await engine.removeChatRoomEntries(targetId, keys, force, callback);

Callback When Chatroom KV Synchronization is Complete

This callback is triggered when KV synchronization is complete upon joining a chatroom.

Method

setOnChatRoomEntriesSyncedListener(listener?: (roomId: string) => void): void;

Parameter Description

ParameterTypeDescription
roomIdstringThe chatroom ID

Code Example


engine?.setOnChatRoomEntriesSyncedListener((roomId: string) => {
//...
});

Callback When Chatroom KV Changes

If KVs exist when joining a chatroom, this callback will return all KVs. Subsequent callbacks will be triggered when others set or modify KVs.

Method

setOnChatRoomEntriesChangedListener(listener?: (operationType: RCIMIWChatRoomEntriesOperationType, roomId: string, entries: Map<string, string>) => void): void;

Parameter Description

ParameterTypeDescription
operationTypeRCIMIWChatRoomEntriesOperationTypeThe type of operation
roomIdstringThe chatroom ID
entriesMap<string, string>The changed KVs

Code Example


engine?.setOnChatRoomEntriesChangedListener(
(operationType: RCIMIWChatRoomEntriesOperationType, roomId: string, entries: Map<string, string>) => {
//...
}
);