/ SDKs / JavaScript
SDKs
Chat SDKs JavaScript v4
Chat SDKs JavaScript
Chat SDKs
JavaScript
Version 4

Manage channel metacounters

Copy link

Using channel metacounter, you can store additional information to channels such as the tracking number of likes on a message. A channel metacounter is an object that doesn't belong to a Channel object and can be fetched or rendered into the UI.

A channel metacounter is primarily used to track and update discrete indicators in a channel. Use channel metacounter instead of channel metadata when you need an integer with increasing and decreasing atomic operations.


Create a metacounter

Copy link

To store a metacounter into a channel, create a dictionary of key-value items where the key is a string and the value is an integer. Then pass the dictionary as an argument to a parameter when calling the createMetaCounters() method. You can store multiple key-value items in the dictionary.

JavaScriptTypeScript
const counters = {
    key1: 1,
    key2: 2,
};
await channel.createMetaCounters(counters);

Update a metacounter

Copy link

The procedure of updating a channel metacounter is the same as creating a channel metacounter. Values of existing keys are updated and values of new keys are added.

JavaScriptTypeScript
const counters = {
    key1: 3, // Update 1 to 3.
    key2: 4, // Update 2 to 4.
    key3: 0, // Add a new key-value item.
};
const upsertIfKeyNotExist = true;
await channel.updateMetaCounters(counters, upsertIfKeyNotExist);

Increase a metacounter

Copy link

You can increase values in a channel metacounter by passing an object of keys as an argument to a parameter in the increaseMetaCounters() method. The values of corresponding keys in the metacounter are increased by the specified number.

JavaScriptTypeScript
const counters = {
    key1: 2, // Increase by 2.
    key2: 3, // Increase by 3.
};
await channel.increaseMetaCounters(counters);

Decrease a metacounter

Copy link

You can decrease values in a channel metacounter by passing an object of keys as an argument to a parameter in the decreaseMetaCounters() method. The values of corresponding keys in the metacounter are decreased by the specified number.

JavaScriptTypeScript
const counters = {
    key1: 3, // Decrease by 3.
    key2: 4, // Decrease by 4.
};
await channel.decreaseMetaCounters(counters);

Retrieve a metacounter

Copy link

You can retrieve channel metacounter by creating a collection of keys to retrieve and passing it as an argument to a parameter in the getMetaCounters() method. A MetaCounter object is returned through the callback function with the corresponding key-value items.

JavaScriptTypeScript
const keys = ['key1', 'key2'];
const metaCounters = await channel.getMetaCounters(keys);

Delete a metacounter

Copy link

You can delete a channel metacounter as shown below.

JavaScriptTypeScript
await channel.deleteMetaCounter('key1');