Chat / JavaScript
Chat JavaScript v4
Chat JavaScript
Chat
JavaScript
Home
/
Chat
/
JavaScript
/
Channel
This is the new Docs for Chat SDK v4 for JavaScript. To see the previous Docs, click here.

Manage channel metacounter

Using channel metacounter, you can store additional information to channels such as the tracking number of likes. 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 metacounter

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.

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

Update metacounter

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

const counters: MetaCounter = {
    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 metacounter

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 will increase by the number you've specified.

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

Decrease metacounter

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 will decrease by the number you've specified.

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

Retrieve metacounter

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 will return through the callback function with the corresponding key-value items.

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

Delete metacounter

You can delete a channel metacounter as below.

await channel.deleteMetaCounter('key1');