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

User & channel metadata

With metadata and metacounter which consist of key-value items, you can store additional information to users and channels. This page explains how to manage user metadata, channel metadata, and channel metacounter.

Note : User metadata, channel metadata, and channel metacounter can each have up to five key-value items. The key’s length must be no larger than 128 characters. For user metadata and channel metadata, the value must be a String and its length must be no larger than 190 characters. For channel metacounter, the value must be an Integer.


User metadata

You can store additional information to users such as phone number, email or a long description of a user with user metadata, which can be fetched or rendered into the UI. User metadata is an object and it can be stored into a User object.

Create

To store a user metadata into a User object, create a Dictionary of key-value items, and then pass it as an argument to a parameter when calling the createMetaData() method. You can put multiple key-value items in the dictionary.

const data: MetaData = {
    'key1': 'value1',
    'key2': 'value2'
};
await sb.currentUser.createMetaData(data);

Update

The procedure to update a user metadata is the same as creating a user metadata. Values of existing keys will be updated and values of new keys will be added.

const data: MetaData = {
    'key1': 'valueToUpdate1',
    'key2': 'valueToUpdate2'
};
await sb.currentUser.updateMetaData(data);

Retrieve

You can retrieve user metadata by reading the metadata property of a User object.

const value1 = user.metaData['key1'];

Delete

You can delete a user metadata as below.

await sb.currentUser.deleteMetaData('key1');

Channel metadata

You can store additional information to channels such as background color or a long description of a channel with channel metadata, which can be fetched or rendered into the UI. Channel metadata is an object and it does not belong to a Channel object.

Create channel metadata

To store a channel metadata into a Channel object, create a MetaData of key-value items that the key and value are String, and then pass it as an argument to a parameter when calling the createMetaData() method. You can put multiple key-value items in the dictionary.

const data: MetaData = {
    'key1': 'value1',
    'key2': 'value2'
};
await channel.createMetaData(data);

Update channel metadata

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

const data: MetaData = {
    'key1': 'valueToUpdate1',   // Update an existing item with a new value.
    'key2': 'valueToUpdate2',   // Update an existing item with a new value.
    'key3': 'valueToAdd3'       // Add a new key-value item.
};
const upsertIfKeyNotExist = true;
await channel.updateMetaData(data, upsertIfKeyNotExist);

Retrieve channel metadata

You can retrieve channel metadata by creating a Collection of keys to retrieve and passing it as an argument to a parameter in the getMetaData() method. A Dictionary will return through the callback function with corresponding key-value items.

const keys = [
    'key1',
    'key2'
];
const data: MetaData = await channel.getMetaData(keys);

Retrieve cached channel metadata

When Sendbird Chat SDK detects any of the create, read, update, and delete operations on the channel metadata, the SDK caches the metadata. The cached metadata is also updated whenever a channel list is fetched.

You can retrieve the cached metadata through the cachedMetaData property without having to query the server.

channel.cachedMetaData;

Delete channel metadata

You can delete a channel metadata as below.

await channel.deleteMetaData('key1');

Channel metacounter

You can store additional information to channels such as the tracking number of likes with channel metacounter, which can be fetched or rendered into the UI. Channel metacounter is an object and it does not belong to a Channel object.

Note : 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 atomic increasing and decreasing operations.

Create

To store a metacounter into a channel, create a Dictionary of key-value items that the key is String and the value is Integer, and then pass it 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

The procedure to update 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

You can increase values in a channel metacounter by passing an object of keys to increase as an argument to a parameter in the increaseMetaCounters() method. The values of corresponding keys in the metacounter will be incremented by the number you’ve specified.

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

Decrease

You can decrease values in a channel metacounter by passing an object of keys to decrease as an argument to a parameter in the decreaseMetaCounters() method. The values of corresponding keys in the metacounter will be decremented by the number you’ve specified.

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

Retrieve

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

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

Delete

You can delete a channel metacounter as below.

const metaCounters = await channel.deleteMetaCounter('key1');