setGroupCounters
, increaseGroupCounter
, and decreaseGroupCounter
APIs together can be called by a logged-in user up to 20 times every five seconds in the SDK, and the 2996 error code will be returned if the limit is exceeded.getGroupCounters
API can be called by a logged-in user up to 20 times every five seconds in the SDK, and the 2996 error code will be returned if the limit is exceeded.setGroupCounters
API to set multiple group counters. After group counters are set, the TencentCloudChat.EVENT.GROUP_COUNTER_UPDATED
event will be triggered.key
of the group counter to be set already exists, the value of key
is updated directly. Otherwise, a key-value pair will be added.chat.setGroupCounters(options);
options
parameter is of the Object
type. It contains the following attribute values:Name | Type | Description |
groupID | String | Group ID. |
counters | Object | Group counter key-value pair. |
Promise
// Set the values of counters `key1` and `key2` to 0.let promise = tim.setGroupCounters({groupID: 'group1',counters: { key1: 0, key2: 0 }});promise.then(function(imResponse) { // Set successfully.console.log(imResponse.data.counters); // Group attributes set successfully.}).catch(function(imError) { // Setting failed.console.warn('setGroupCounters error:', imError); // Error information.});
increaseGroupCounter
API to increase the value of a group counter. After the value of the group counter is increased, the TencentCloudChat.EVENT.GROUP_COUNTER_UPDATED
event will be triggered.value
is the change value. Each time when the API is called, the current value of value
is increased by the value passed in.key
of the group counter to be set already exists, the current value of value
is directly increased by the value passed in. Otherwise, a key
will be added, and the default value (0) of value
is increased by the value passed in.chat.increaseGroupCounter(options);
options
parameter is of the Object
type. It contains the following attribute values:Name | Type | Description |
groupID | String | Group ID. |
key | String | Group counter key. |
value | Number | Increment value of the group counter key. |
Promise
// Assume that the value of counter `key1` is 8.// After you call the `increaseGroupCounter` API to pass in an increment value of 2// the value of `key1` becomes 10.let promise = chat.increaseGroupCounter({groupID: 'group1',key: 'key1',value: 2,});promise.then(function(imResponse) { // Group counter increased successfully.console.log(imResponse.data);const { groupID, key, value } = imResponse.data;}).catch(function(imError) { // Failed to increase the group counter.console.warn('increaseGroupCounter error:', imError);});
decreaseGroupCounter
API to decrease the value of a group counter. After the value of the group counter is decreased, the TencentCloudChat.EVENT.GROUP_COUNTER_UPDATED
event will be triggered. value
is the change value. Each time when the API is called, the current value of value
is decreased by the value passed in.key
of the group counter to be set already exists, the current value of value
is directly decreased by the value passed in. Otherwise, a key
will be added, and the default value (0) of value
is decreased by the value passed in.chat.decreaseGroupCounter(options);
options
parameter is of the Object
type. It contains the following attribute values:Name | Type | Description |
groupID | String | Group ID. |
key | String | Group counter key. |
value | Number | Decrement value of the group counter key. |
Promise
// Assume that the value of counter `key1` is 8.// After you call the `decreaseGroupCounter` API to pass in a decrement value of 2// the value of `key1` becomes 6.let promise = chat.decreaseGroupCounter({groupID: 'group1',key: 'key1',value: 2});promise.then(function(imResponse) { // Group counter decreased successfully.console.log(imResponse.data);const { groupID, key, value } = imResponse.data;}).catch(function(imError) { // Setting failed.console.warn('decreaseGroupCounter error:', imError);});
getGroupCounters
API to pass in a set of keys to get the information of the corresponding group counters. The API will return all key-value pairs matching the keys that are passed in.chat.getGroupCounter(options);
options
parameter is of the Object
type. It contains the following attribute values:Name | Type | Description |
groupID | String | Group ID. |
keyList | Array | undefined | List of group counter keys. |
Promise
// Get the values of counters `key1` and `key2`.let promise = chat.getGroupCounters({groupID: 'group1',keyList: ['key1', 'key2']});promise.then(function(imResponse) { // Got successfully.console.log(imResponse.data.counters);}).catch(function(imError){console.warn('getGroupCounters error:', imError); // Error information.});
// Get the values of all counters in a group.let promise = chat.getGroupCounters({groupID: 'group1'});promise.then(function(imResponse) { // Got successfully.console.log(imResponse.data.counters);}).catch(function(imError){console.warn('getGroupCounters error:', imError); // Error information.});
setGroupCounters
, increaseGroupCounter
, or decreaseGroupCounter
API to modify group counters, the TencentCloudChat.EVENT.GROUP_COUNTER_UPDATED
event will be triggered and the updated values will be returned.on
API to register for group counter change events.let onGroupCounterUpdated = function(event) {const { groupID, key, value } = event.data;// groupID - Group ID.// key - Group counter key.// value - Value of the group counter key.};chat.on(TencentCloudChat
.EVENT.GROUP_COUNTER_UPDATED, onGroupCounterUpdated);
Was this page helpful?