User Metadata

With a user metadata, you can store additional information within a channel. The user metadata allows you to store a Dictionary of String key-value pairs in a user instance. Use cases for the metadata are such as phone number, email or a long description of the user, which can each be fetched and rendered into the UI.


Create, update, retrieve, and delete a user metadata

A user metadata is a Dictionary that is stored into a user. Its uses are very flexible, allowing you to customize a user to fit you and your users' needs.

Create

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

var data = {
    'phone': '001-0002-0003',
    'email': 'sales@sendbird.com'
};

var user = sb.currentUser;
user.createMetaData(data, function(metadata, error) {
    if (error) {
        return;
    }
});

Update

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

var data = {
    'phone': '001-0002-0004',
    'email': 'engineering@sendbird.com'
};

var user = sb.currentUser;
user.updateMetaData(data, function(metadata, error) {
    if (error) {
        return;
    }
});

Retrieve

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

var keys = [
    'phone',
    'email'
];

var user = sb.currentUser;
user.getMetaData(keys, function(metadata, error) {
    if (error) {
        return;
    }
});

Delete

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

var user = sb.currentUser;
user.deleteMetaData("phone", function(response, error) {
    if (error) {
        return;
    }
});