Add custom metadata

You can make your channels, users, and the relationships between them more descriptive by storing additional information about them.

There are three items you can assign metadata to: users, channels, and memberships. Unlike setting dynamic states, metadata properties are stored in PubNub and you can get their values anytime. Let's look at each of them.

Users have four predefined metadata properties, name, email, profileURL, and externalId. There is also a custom property that allows your to store any attribute.

The code below adds a name, an e-mail, and a custom nickname properties to provide more information about a user.

User ID / UUID

User ID is also referred to as UUID/uuid in some APIs and server responses but holds the value of the userId parameter you set during initialization.

pubnub.objects.setUUIDMetadata({
data: {
name: "Thomas Anderson",
email: "t.anderson@pubnub.com",
custom: {
"nickname": "The One"
}
}
});

Channels have two predefined metadata properties, name and description, and a custom property that allows your to store any attribute.

The code below adds all three properties to provide more information about a channel. This way you're keeping all channel information in one place and can easily present additional data in your app's user interface.

pubnub.objects.setChannelMetadata({
channel: "chats_guilds.mages_guild",
data: {
name: "Mages guild",
description: "Mages guild official chatroom.",
custom: { "administrator": "thomas_anderson" }
}
});

Memberships are relations between users and channels. You can use membership metadata to, for example, display channel memberships lists for a user or give them the possibility to join or leave channels at their discretion.

Memberships have no predefined metadata properties, as the membership relation is metadata in itself.

The code below adds the current user to the chats_guilds.mages_guild and chats_inbox.user_1905 channels and adds the friendlist metadata to the newly created chats_inbox.user_1905 membership.

pubnub.objects.setMemberships({
channels: [
"chats_guilds.mages_guild",
{id: "chats_inbox.user_1905", custom: {friendlist: true}
}]
});

Sometimes you have to limit or grant additional privileges to certain users. This is where our access manager comes in handy.

Last updated on
On this page