Set, get, and remove channel metadata
Starting a new app? Use DataSync
DataSync is the successor to App Context. It does everything App Context does for users, channels, and memberships, and adds typed schemas, partial updates with ETags, field-level access control, and per-class expiry. Refer to How DataSync compares to App Context for a feature-by-feature comparison.
DataSync is currently available to new accounts and to accounts that are not actively using App Context. If your keysets already use App Context, keep using it for now. App Context remains fully supported and these pages stay accurate.
This guide shows you how to give a single channel its App Context metadata: set its built-in and custom fields, read them back, and remove the whole record. App Context must be enabled on the keyset before you call it, and new keysets don't enable it by default. To read or list every channel's metadata at once instead of one channel, refer to Get metadata for all channels.
Set channel metadata
Call Set Channel Metadata with the channel's name and a data object holding the built-in name and description fields plus any custom fields your application needs. The call creates the record if it doesn't exist yet, or updates it if it does.
Unsupported partial updates of custom metadata
The value of the custom metadata parameter sent in this method always overwrites the value stored on PubNub servers. If you want to add new custom data to an existing one, you must:
- Get the existing metadata and store it locally.
- Append the new custom metadata to the existing one.
- Set the entire updated custom object.
- JavaScript
- Java
- Kotlin
- C#
- Go
- Swift
- PHP
- Unity
1const response = await pubnub.objects.setChannelMetadata({
2 channel: 'team.red',
3 data: {
4 name: 'Red Team',
5 description: 'The channel for Red team and no other teams.',
6 custom: {
7 owner: 'Red Leader',
8 },
9 },
10 include: {
11 customFields: false,
12 },
13});
14console.log('Set channel metadata response:', response);
1pubNub.setChannelMetadata()
2 .channel("myChannel")
3 .name("Some Name")
4 .includeCustom(true)
5 .async(result -> { /* check result */ });
1pubnub.setChannelMetadata(channel = "myChannel")
2 .async { result ->
3 result.onFailure { exception ->
4 // Handle error
5 }.onSuccess { value ->
6 // Handle successful method result
7 }
8 }
1using PubnubApi;
2
3// Configuration
4PNConfiguration pnConfiguration = new PNConfiguration(new UserId("myUniqueUserId"))
5{
6 SubscribeKey = "demo",
7 PublishKey = "demo",
8 Secure = true
9};
10
11// Initialize PubNub
12Pubnub pubnub = new Pubnub(pnConfiguration);
13
14// Set Metadata for a specific channel
15PNResult<PNSetChannelMetadataResult> setChannelMetadataResponse = await pubnub.SetChannelMetadata()
show all 24 lines1// Replace with your package name (usually "main")
2package pubnub_samples_test
3
4import (
5 "fmt"
6 "time"
7
8 pubnub "github.com/pubnub/go/v10"
9)
10
11// Example_setChannelMetadata demonstrates setting channel metadata
12func Example_setChannelMetadata() {
13 config := pubnub.NewConfigWithUserId(pubnub.UserId("demo-user"))
14 config.SubscribeKey = "demo"
15 config.PublishKey = "demo"
show all 43 lines1// Set channel metadata for a specific identifier
2let channelMetadataToSet = PubNubChannelMetadataBase(
3 metadataId: "some-channel-id",
4 name: "Channel Name"
5)
6
7pubnub.setChannelMetadata(channelMetadataToSet) { result in
8 switch result {
9 case let .success(channelMetadata):
10 print("The metadata for `\(channelMetadata.metadataId)`: \(channelMetadata)")
11 case let .failure(error):
12 print("Create request failed with error: \(error.localizedDescription)")
13 }
14}
1foreach ($sampleChannels as $channel) {
2 $setChannelMetadataResult = $pubnub->setChannelMetadata()
3 ->channel($channel['id'])
4 ->setName($channel['name'])
5 ->setDescription($channel['description'])
6 ->setCustom($channel['custom'])
7 ->sync();
8 assert($setChannelMetadataResult->getId() === $channel['id']);
9 assert($setChannelMetadataResult->getName() === $channel['name']);
10 assert($setChannelMetadataResult->getDescription() === $channel['description']);
11}
1using PubnubApi;
2using PubnubApi.Unity;
3
4// Configuration
5PNConfiguration pnConfiguration = new PNConfiguration(new UserId("myUniqueUserId"))
6{
7 SubscribeKey = "demo",
8 PublishKey = "demo",
9 Secure = true
10};
11
12// Initialize PubNub
13Pubnub pubnub = PubnubUnityUtils.NewUnityPubnub(pnConfiguration);
14
15// If you're using Unity Editor setup you can get the Pubnub instance from PNManagerBehaviour
show all 32 linesIf Access Manager is enabled on your keyset, this call requires an update permission grant on the channel. On success, the call also fires the set App Context event other subscribers receive.
Pass the eTag from a get call as ifMatchesEtag on a set call to apply the update only if the record hasn't changed since you read it, so a mismatch returns HTTP 412. Most SDKs document this parameter on the same method as ifMatchesEtag. Check the API reference for your platform in Available SDKs for the exact name.
Get channel metadata
Call Get Channel Metadata with the channel's name to read its current record. Most SDKs include the custom object in the response by default. Check your platform's API reference in Available SDKs to confirm.
- JavaScript
- Java
- Kotlin
- C#
- Go
- Swift
- PHP
- Unity
1const response = await pubnub.objects.getChannelMetadata({
2 // `channel` is the `id` in the _metadata_, not `name`
3 channel: 'team.blue',
4});
5console.log('Get channel metadata response:', response);
1PNGetChannelMetadataResult pnGetChannelMetadataResult = pubNub.getChannelMetadata()
2 .channel("myChannel")
3 .sync();
1pubnub.getChannelMetadata(channel = "myChannel")
2 .async { result ->
3 result.onFailure { exception ->
4 // Handle error
5 }.onSuccess { value ->
6 // Handle successful method result
7 }
8 }
1using PubnubApi;
2
3// Configuration
4PNConfiguration pnConfiguration = new PNConfiguration(new UserId("myUniqueUserId"))
5{
6 SubscribeKey = "demo",
7 PublishKey = "demo",
8 Secure = true
9};
10
11// Initialize PubNub
12Pubnub pubnub = new Pubnub(pnConfiguration);
13
14// Get Metadata for a specific channel
15PNResult<PNGetChannelMetadataResult> getChannelMetadataResponse = await pubnub.GetChannelMetadata()
show all 21 lines1// Replace with your package name (usually "main")
2package pubnub_samples_test
3
4import (
5 "fmt"
6 "time"
7
8 pubnub "github.com/pubnub/go/v10"
9)
10
11// Example_getChannelMetadata demonstrates retrieving channel metadata
12func Example_getChannelMetadata() {
13 config := pubnub.NewConfigWithUserId(pubnub.UserId("demo-user"))
14 config.SubscribeKey = "demo"
15 config.PublishKey = "demo"
show all 49 lines1// Retrieve channel metadata for a specific identifier
2pubnub.fetchChannelMetadata("some-channel-id") { result in
3 switch result {
4 case let .success(channelMetadata):
5 print("The metadata for `\(channelMetadata.metadataId)`: \(channelMetadata)")
6 case let .failure(error):
7 print("Fetch request failed with error: \(error.localizedDescription)")
8 }
9}
1$getChannelMetadataResult = $pubnub->getChannelMetadata()
2 ->channel($sampleChannels[0]['id'])
3 ->sync();
4assert($getChannelMetadataResult->getId() === $sampleChannels[0]['id']);
5assert($getChannelMetadataResult->getName() === $sampleChannels[0]['name']);
1using PubnubApi;
2using PubnubApi.Unity;
3
4// Configuration
5PNConfiguration pnConfiguration = new PNConfiguration(new UserId("myUniqueUserId"))
6{
7 SubscribeKey = "demo",
8 PublishKey = "demo",
9 Secure = true
10};
11
12// Initialize PubNub
13Pubnub pubnub = PubnubUnityUtils.NewUnityPubnub(pnConfiguration);
14
15// If you're using Unity Editor setup you can get the Pubnub instance from PNManagerBehaviour
show all 29 linesIf Access Manager is enabled, this call requires a get permission grant on the channel. A channel with no metadata record yet returns a not-found error, since publishing to a channel doesn't create one.
Remove channel metadata
Call Remove Channel Metadata with the channel's name to delete its record entirely, including every custom field. There's no way to remove a single field without rewriting the record. Refer to Set channel metadata to overwrite custom with everything except the field you want gone.
- JavaScript
- Java
- Kotlin
- C#
- Go
- Swift
- PHP
- Unity
1const response = await pubnub.objects.removeChannelMetadata({
2 channel: 'team.red',
3});
1pubNub.removeChannelMetadata()
2 .channel("myChannel")
3 .async(result -> { /* check result */ });
1pubnub.removeChannelMetadata(channel = "myChannel")
2 .async { result ->
3 result.onFailure { exception ->
4 // Handle error
5 }.onSuccess { value ->
6 // Handle successful method result
7 }
8 }
1using PubnubApi;
2
3// Configuration
4PNConfiguration pnConfiguration = new PNConfiguration(new UserId("myUniqueUserId"))
5{
6 SubscribeKey = "demo",
7 PublishKey = "demo",
8 Secure = true
9};
10
11// Initialize PubNub
12Pubnub pubnub = new Pubnub(pnConfiguration);
13
14// Delete Metadata for a specific channel
15PNResult<PNRemoveChannelMetadataResult> removeChannelMetadataResponse = await pubnub.RemoveChannelMetadata()
show all 20 lines1// Replace with your package name (usually "main")
2package pubnub_samples_test
3
4import (
5 "fmt"
6 "time"
7
8 pubnub "github.com/pubnub/go/v10"
9)
10
11// Example_removeChannelMetadata demonstrates removing channel metadata
12func Example_removeChannelMetadata() {
13 config := pubnub.NewConfigWithUserId(pubnub.UserId("demo-user"))
14 config.SubscribeKey = "demo"
15 config.PublishKey = "demo"
show all 37 lines1// Remove channel metadata for a specific identifier
2pubnub.removeChannelMetadata("some-channel-id") { result in
3 switch result {
4 case let .success(metadataId):
5 print("The metadata has been removed for the channel `\(metadataId)`")
6 case let .failure(error):
7 print("Delete request failed with error: \(error.localizedDescription)")
8 }
9}
1$removeChannelMetadataResult = $pubnub->removeChannelMetadata()
2 ->channel($sampleChannels[1]['id'])
3 ->sync();
4assert($removeChannelMetadataResult);
1using PubnubApi;
2using PubnubApi.Unity;
3
4// Configuration
5PNConfiguration pnConfiguration = new PNConfiguration(new UserId("myUniqueUserId"))
6{
7 SubscribeKey = "demo",
8 PublishKey = "demo",
9 Secure = true
10};
11
12// Initialize PubNub
13Pubnub pubnub = PubnubUnityUtils.NewUnityPubnub(pnConfiguration);
14
15// If you're using Unity Editor setup you can get the Pubnub instance from PNManagerBehaviour
show all 28 linesIf Access Manager is enabled, this call requires a delete permission grant on the channel. Turning on Enforce referential integrity for memberships in the Admin Portal also deletes every membership pointing at this channel when you remove its metadata. Leaving that setting off leaves those memberships in place, pointing at a channel with no metadata record. Refer to Membership connects users and channels for the full referential-integrity behavior.
Set, get, and remove channel metadata without code
BizOps Workspace's Channel Management module performs the same three operations from the Admin Portal, with no SDK call required:
- Create channels. Set a new channel's
name,description, and custom fields. - Update channels. Change an existing channel's fields.
- Delete channels. Remove a channel's metadata, including in bulk.
A record you create, edit, or delete in BizOps Workspace is the same record your SDK calls read and write. Changes in either place are immediately visible in the other.
Related tasks
- App Context. The three entity types and how App Context relates to messages and Presence.
- Get metadata for all channels. Page through every channel's metadata instead of one.
- Set, get, and remove members. Manage this channel's user roster.
- App Context events. The
setanddeleteevent payload this operation produces. - App Context API limits. Field-length limits for
name,description, andcustom.