Add custom metadata

In PubNub, you can add descriptive metadata to channels, users, and the relationships between them.

There are three items you can assign metadata to: users, channels, and memberships. Unlike dynamic state, metadata is stored in PubNub and is available anytime. Let's look at each of them.

Metadata scope by entity:

Entity TypeMetadata ScopeUse Cases
Channels
Channel-wide
Topic categorization, display name, description
Users
User profile
Preferences, roles, profile fields
Memberships
Relationship
Permission levels, user-channel settings

Users have four predefined metadata properties: name, email, profileURL, and externalId. There is also a custom property that allows you 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. The custom property allows you 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 relationships between users and channels. Use membership metadata to display a user’s channel list or to store user‑channel settings.

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

To manage visibility and permissions, use Access Manager (see Manage access).

Last updated on
On this page