JavaScript
Channel Metadata

Channel Metadata

With metadata and metacounter, you can store additional information within a channel. Two data types allow you to store a Dictionary of key-value pairs in a channel object. When 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, retrieve, and delete a metadata

A metadata is a Dictionary of key(String)-value(String) pairs 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 of key-value pairs, and then pass it as a parameter when calling the createMetaData() method. You can store multiple key-value pairs in the dictionary.

Light Color Skin
Copy
var data = {
    'key1': 'value1',
    'key2': 'value2'
};

channel.createMetaData(data, function(response, error) {
    if (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 if the value of the upsert parameter is true.

Light Color Skin
Copy
var data = {
    '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.
};

var upsertIfNotExist = true;    // If false, the item with `key3` isn't added to the metadat metadata. 

channel.updateMetaData(data, upsertIfNotExist, function(response, error) {
    if (error) {
        return;
    }
});

Retrieve

To get stored key-value pairs in a metadata, create a Collection of keys to retrieve, and then pass it as a parameter to the getMetaData() method. The callback returns a Dictionary containing the corresponding key-value pairs.

Light Color Skin
Copy
var keys = [ 
    'key1',
    'key2'
];

channel.getMetaData(keys, function(response, error) {
    if (error) {
        return;
    }
});

Delete

The process for deleting a metadata is as simple as creating one.

Light Color Skin
Copy
channel.deleteMetaData('key1', function(response, error) {
    if (error) {
        return;
    }
});

Create, update, increase, decrease, retrieve, and delete a metacounter

A metacounter is a Dictionary of key(String)-value(Integer) pairs that is 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 of key-value pairs, and then pass it as a parameter when calling the createMetaCounters() method. You can store multiple key-value pairs in the dictionary.

Light Color Skin
Copy
var counters = {
    'key1': 1,
    'key2': 2'
};

channel.createMetaCounters(counters, function(response, error) {
    if (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 if the value of the upsert parameter is true.

Light Color Skin
Copy
var data = {
    'key1': 3,  // Update 1 to 3.
    'key2': 4,  // Update 2 to 4.
    'key3': 0   // Add a new key-value item.
};

var upsertIfNotExist = true;    // If false, the item with `key3` isn't added to the metadat metadata. 

channel.updateMetaCounters(data, upsertIfNotExist, function(response, error) {
    if (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
var counters = {
    'key1': 2,  // Increase by 2.
    'key2': 3'  // Increase by 3.
};

channel.increaseMetaCounters(counters, function(response, error) {
    if (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
var counters = {
    'key1': 3,  // Decrease by 3.
    'key2': 4'  // Decrease by 4.
};

channel.decreaseMetaCounters(counters, function(response, error) {
    if (error) {
        return;
    }
});

Retrieve

To get stored key-value pairs in a metacounter, create a Collection of keys to retrieve, and then pass it as a parameter to the getMetaCounters() method. The callback returns a Dictionary containing the corresponding key-value pairs.

Light Color Skin
Copy
var keys = [ 
    'key1',
    'key2'
];

channel.getMetaCounters(keys, function(response, error) {
    if (error) {
        return;
    }
});

Delete

The process for deleting a metacounter is as simple as creating one.

Light Color Skin
Copy
channel.deleteMetaCounter('key1', function(response, error) {
    if (error) {
        return;
    }
});