Chat SDKs Unity v3
Chat SDKs Unity
Chat SDKs
Unity
Version 3
Sendbird Chat SDK v3 for Unity is no longer supported as a new version is released. Check out our latest Chat SDK v4

Channel metadata & metacounter

Copy link

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

The key’s length must be no larger than 128 characters. For 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.


Channel metadata

Copy link

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. A channel metadata is a Dictionary<String, String> and it can be stored into a Channel object.

Create

Copy link

To store a channel metadata into a Channel object, create a Dictionary<String, String> 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.

Dictionary<string, string> data = new Dictionary<string, string>();
data.Add("key1", "value1");
data.Add("key2", "value2");

channel.CreateMetaData(data, (Dictionary<string, string> map, SendBirdException e) =>
{
    if (e != null)
    {
        // Handle error.
    }

    ...
});

Update

Copy link

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.

Dictionary<string, string> data = new Dictionary<string, string>();
data.Add("key1", "valueToUpdate");
data.Add("key2", "valueToUpdate");

channel.UpdateMetaData(data, (Dictionary<string, string> map, SendBirdException e) =>
{
    if (e != null)
    {
        // Handle error.
    }

    ...
});

Retrieve

Copy link

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

List<string> keys = new List<string>();
keys.Add("key1");
keys.Add("key2");

channel.GetMetaData(keys, (Dictionary<string, string> map, SendBirdException e) =>
{
    if (e != null)
    {
        // Handle error.
    }

    ...
});

Channel metacounter

Copy link

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. A channel metacounter is a Dictionary<String, Integer> and it can be stored into 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

Copy link

To store a metacounter into a channel, create a Dictionary<String, Integer> 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.

Dictionary<string, int> counters = new Dictionary<string, int>();
counters.Add("key1", 1);
counters.Add("key2", 2);

channel.CreateMetaCounters(counters, (Dictionary<string, int> map, SendBirdException e) =>
{
    if (e != null)
    {
        // Handle error.
    }

    ...
});

Increase

Copy link

You can increase values in a channel metacounter by passing a Dictionary 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.

Dictionary<string, int> counters = new Dictionary<string, int>();
counters.Add("key1", 2); // Increase by 2.
counters.Add("key2", 3); // Increase by 3.

channel.IncreaseMetaCounters(counters, (Dictionary<string, int> map, SendBirdException e) =>
{
    if (e != null)
    {
        // Handle error.
    }

    ...
});

Decrease

Copy link

You can decrease values in a channel metacounter by passing a Dictionary 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.

Dictionary<string, int> counters = new Dictionary<string, int>();
counters.Add("key1", 3); // Decrease by 3.
counters.Add("key2", 4); // Decrease by 4.

channel.DecreaseMetaCounters(counters, (Dictionary<string, int> map, SendBirdException e) =>
{
    if (e != null)
    {
        // Handle error.
    }

    ...
});

Retrieve

Copy link

You can retrieve channel metacounter by creating a List of keys to retrieve and passing it as an argument to a parameter in the GetMetaCounters() method. A Dictionary<String, Integer> will return through the callback method with corresponding key-value items.

List<string> keys = new List<string>();
keys.add("key1");
keys.add("key2");

channel.GetMetaCounters(keys, (Dictionary<string, int> map, SendBirdException e) =>
{
    if (e != null)
    {
        // Handle error.
    }

    ...
});