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 metadata

You can store additional information to channels such as background color or channel description with channel metadata, which can be fetched or rendered into the UI. Channel metadata is an object consisting of key-value items.


Create metadata

To store channel metadata into a Channel object, create a MetaData of key-value items in which the data type of the key and value is String. Then, pass the MetaData object 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 metadata

The process of updating channel metadata is the same as creating one. Values of existing keys can be updated and values of new keys can be added by calling the updateMetaData() method.

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

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

Retrieve cached 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 metadata

You can delete channel metadata by calling the deleteMetaData() method.

await channel.deleteMetaData('key1');