Channel Metadata

With metadata and metacounter, you can store additional information within a channel. The metadata allows you to store a Dictionary of String key-value pairs in a channel instance. If you want to store an integer with atomic increasing and decreasing operations, use the metacounter instead.

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


Metadata

Metadata is a Dictionary that is stored within a channel. Its uses are very flexible, allowing you to customize a channel to fit you and your users' needs.

Create

To store metadata into a channel, create a Dictionary, and then pass it as an argument when calling CreateMetaData(). You can store multiple key-value pairs in the map.

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 metadata is identical to creation. Values are updated for existing keys, while new key-value pairs 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) {
        // Error.
        return;
    }
});

Get

To get stored metadata, create a List of keys, and then pass it as an argument to GetMetaData(). The callback returns a Dictionary<String, String> containing the corresponding key-value pairs.

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;
    }
});

Metacounter

A metacounter is a Dictionary that is stored within a channel instance. Its primary uses are to track and update discrete indicators within a channel.

Create

To store a metacounter into a channel, create a Dictionary, and then pass it as an argument when calling CreateMetaCounter(). You can store multiple key-value pairs in the map.

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;
    }
});

Get

To get stored metacounters, create a List of keys, and then pass it as an argument to GetMetaCounters(). The callback returns a Dictionary<String, Integer> containing the corresponding key-value pairs.

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;
    }
});

Increase

The increase and decrease operations work similarly to getting metacounters, as described above. Create a List of keys to pass to IncreaseMetaCounters(), which increments the corresponding metacounters by the number you've specified.

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

Likewise, pass a List of keys to DecreaseMetaCounters(), which decrements the metacounters by the number you've specified.

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;
    }
});