.NET
Channel Metadata

Channel Metadata

With metadata and metacounter, you can store additional information within a channel. These two data types allow you to store a Dictionary of key-value pairs in a channel object. If you need an integer with atomic increasing and decreasing operations, use the metacounter instead of the metadata.

Use cases for the metadata and metacounters can be tracking the number of likes, the background color or a long description of a channel, which can each be fetched and rendered into the UI.


Create, update, and retrieve a metadata

A metadata is a Dictionary<String, String> that can be stored within a channel. Its uses are flexible, allowing you to customize a channel to fit you and your users' needs.

Create

To store a metadata into a channel, create a Dictionary<String, String>, add key-value pairs to it, and then pass it as a parameter when calling the CreateMetaData() method. You can store multiple key-value pairs in the map.

Light Color Skin
Copy
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) {
        // Error.
        return;
    }
});

Update

The process for updating a metadata is identical to creation. Values are updated for existing keys, while new key-value pairs added.

Light Color Skin
Copy
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) {
        // Error.
        return;
    }
});

Retrieve

To get stored key-value in a metadata, create a List of keys to retrieve, and then pass it as a parameter to the GetMetaData() method. A Dictionary<String, String> containing the corresponding key-value pairs is returned through the callback.

Light Color Skin
Copy
List<string> keys = new List<string>();
keys.Add("key1");
keys.Add("key2");
channel.GetMetaData(keys, (Dictionary<string, string> map, SendBirdException e) =>
{
    if (e != null) {
        // Error.
        return;
    }
});

Create, increase, decrease, and retrieve a metacounter

A metacounter is a Dictionary<String, Integer> that can be stored within a channel object. Its primary uses are to track and update discrete indicators within a channel.

Create

To store a metacounter into a channel, create a Dictionary<String, Integer>, add key-value pairs to it, and then pass it as a parameter when calling CreateMetaCounter(). You can store multiple key-value pairs in the map.

Light Color Skin
Copy
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) {
        // Error.
        return;
    }
});

Increase

To increase the values of key-value pairs in a metacounter, create a Dictionary of keys to increase, and then pass it as a parameter to the IncreaseMetaCounters() method, which increments the corresponding keys by the number you've specified.

Light Color Skin
Copy
Dictionary<string, int> counters = new Dictionary<string, int>();
counters.Add("key1", 2); // Increases by 2.
counters.Add("key2", 3); // Increases by 3.
channel.IncreaseMetaCounters(counters, (Dictionary<string, int> map, SendBirdException e) =>
{
    if (e != null) {
        // Error.
        return;
    }
});

Decrease

To decrease the values of key-value pairs in a metacounter, create a Dictionary of keys to decrease, and then pass it as a parameter to the DecreaseMetaCounters() method, which decrements the corresponding keys by the number you've specified.

Light Color Skin
Copy
Dictionary<string, int> counters = new Dictionary<string, int>();
counters.Add("key1", 3); // Decreases by 3.
counters.Add("key2", 4); // Decreases by 4.
channel.DecreaseMetaCounters(counters, (Dictionary<string, int> map, SendBirdException e) =>
{
    if (e != null) {
        // Error.
        return;
    }
});

Retrieve

To get stored key-value pairs in a metacounter, create a List of keys to retrieve, and then pass it as a parameter to the GetMetaCounters() method. A Dictionary<String, Integer> containing the corresponding key-value pairs is returned through the callback.

Light Color Skin
Copy
List<string> keys = new List<string>();
keys.add("key1");
keys.add("key2");
channel.GetMetaCounters(keys, (Dictionary<string, int> map, SendBirdException e) =>
{
    if (e != null) {
        // Error.
        return;
    }
});