Channel Metadata

The Objects service allows you to persist metadata about channels, channel memberships, channel members, and users. The name and description are the predefined properties for channel metadata. Additionally, there is a custom property that you can use to store any custom attribute about a channel as per your business needs.

Channel Metadata

The PubNub Objects service emits events when the metadata associated to a particular channel ID is set or deleted. Your application can receive these events in real time and dynamically react to data changes by updating the information visible on the front end of your app, for instance.

In the following sections, we're going to focus on what you can actually do with channels and their metadata.

Set Channel Metadata

You can set any of the predefined or custom channel metadata by providing the desired information as key/value pairs. The code below adds the name, description, and custom owner information to the channel my_channel.

pubnub.objects.setChannelMetadata({
channel: "my_channel",
data: {
name: "main channel",
description: "This channel is for company wide chatter.",
custom: { "owner": "johndoe_1" }
}
});

On success, the PubNub SDK returns same metadata object along with status 200, it will also the fire objects -> channel -> set event so it can be consumed for other clients (users). Refer to Init & Add Listener section to learn more.

Get Channel Metadata

You can retrieve the metadata of a specific channel by simply providing the channel ID. You can optionally specify whether custom metadata should be included in the response. The code below returns all metadata of the channel with the ID my_channel.

pubnub.objects.getChannelMetadata({
channel: "my_channel"
});

On success, the PubNub SDK returns the metadata of the specified channel along with status 200.

Get Metadata for All Channels

You can also retrieve metadata for all the channels associated with the API key. You can optionally specify whether custom metadata should be included in the response. The code below returns all predefined and custom metadata of all channels:

pubnub.objects.getAllChannelMetadata();

On success, the PubNub SDK returns a paginated list of metadata for all channels.

Remove Channel Metadata

You can remove all metadata for a single channel. The code below removes all metadata of the channel with the ID my_channel.

pubnub.objects.removeChannelMetadata({
channel: "my_channel"
});

On completion, the PubNub SDK will fire the objects -> channel -> delete event so it can be consumed for other clients (users). Refer to Receive Messages to learn more.