Channel Groups API for PubNub Dart SDK

Channel groups allow PubNub developers to bundle thousands of channels into a group that can be identified by a name. These channel groups can then be subscribed to, receiving data from the many back-end channels the channel group contains.

Channel group operations

You can't publish to a channel group. You can only subscribe to it. To publish within the channel group, you need to publish to each channel individually.

Add Channels

Requires Stream Controller add-on

This method requires that the Stream Controller add-on is enabled for your key in the Admin Portal. Read the support page on enabling add-on features on your keys.

This function adds a channel to a channel group.

Methods

Adding Channels is accomplished by using the following method(s) in the Dart SDK:

Maximum number of channels

200 channels can be added to the channel group per API call.

pubnub.channelGroups.addChannels(
String group,
Set<String> channels,
{Keyset? keyset,
String? using}
)
ParameterTypeRequiredDescription
groupStringYesThe channel group to add the channels to.
channelsSet<String>YesThe channels to add to the channel group.
keysetKeysetOptionalOverride for the PubNub default keyset configuration.
usingStringOptionalKeyset name from the keysetStore to be used for this method call.

Basic Usage

var result =
await pubnub.channelGroups.addChannels('cg1', {'ch1', 'ch2'});

Response

The addChannels() method returns a ChannelGroupChangeChannelsResult.

{
"service": "channel-registry",
"status": "200",
"error": false,
"message": "OK"
}

List Channels

Requires Stream Controller add-on

This method requires that the Stream Controller add-on is enabled for your key in the Admin Portal. Read the support page on enabling add-on features on your keys.

This function lists all the channels of the channel group.

Methods

Listing channels is accomplished by using the following method(s) in the Dart SDK:

pubnub.channelGroups.listChannels(
String group,
{Keyset? keyset,
String? using}
)
ParameterTypeRequiredDescription
groupStringYesThe channel group fetch the channels.
keysetKeysetOptionalOverride for the PubNub default keyset configuration.
usingStringOptionalKeyset name from the keysetStore to be used for this method call.

Basic Usage

var result = await pubnub.channelGroups.listChannels('cg1');

Returns

The listChannels() operation returns a ChannelGroupListChannelsResult which contains the following operations:

MethodTypeDescription
channelsSet<String>List of channels of a channel group.
nameStringChannel group name.

Remove Channels

Requires Stream Controller add-on

This method requires that the Stream Controller add-on is enabled for your key in the Admin Portal. Read the support page on enabling add-on features on your keys.

This function removes the channels from the channel group.

Method(s)

Removing Channels is accomplished by using the following method(s) in the Dart SDK:

pubnub.channelGroups.removeChannels(
String group,
Set<String> channels,
{Keyset? keyset,
String? using}
)
ParameterTypeRequiredDescription
groupStringYesThe channel group to remove the channels from.
channelsSet<String>YesThe channels to remove from the channel group.
keysetKeysetOptionalOverride for the PubNub default keyset configuration.
usingStringOptionalKeyset name from the keysetStore to be used for this method call.

Basic Usage

var result = await pubnub.channelGroups.removeChannels('cg1', {'ch1'});

Returns

The removeChannels method returns a ChannelGroupChangeChannelsResult.

{
"service": "channel-registry",
"status": "200",
"error": false,
"message": "OK"
}

Delete Channel Group

Requires Stream Controller add-on

This method requires that the Stream Controller add-on is enabled for your key in the Admin Portal. Read the support page on enabling add-on features on your keys.

This function removes all channels from the channel group.

Method(s)

Deleting Channel Group is accomplished by using the following method(s) in the Dart SDK:

pubnub.channelGroups.delete(
String group,
{Keyset? keyset,
String? using}
)
ParameterTypeRequiredDescription
groupStringYesThe channel group to remove all channels from.
keysetKeysetOptionalOverride for the PubNub default keyset configuration.
usingStringOptionalKeyset name from the keysetStore to be used for this method call.

Basic Usage

var result = await pubnub.channelGroups.delete('cg1');

Returns

The delete method returns a ChannelGroupDeleteResult.

{
"service": "channel-registry",
"status": "200",
"error": false,
"message": "OK"
}

Get Subscribed Channel Groups

Returns all the subscribed channel groups in a Set<String>.

Method(s)

Get Subscribed Channel Groups is accomplished by inspecting the following property in the Dart SDK:

// property of `Subscription` class
subscription.channelGroups

Basic Usage

var subscription = pubnub.subscribe(channelGroups: {'cg1', 'cg2'});
var subscribedChannelGroups = subscription.channelGroups;

print('subscribed channel groups are $subscribedChannelGroups');

Response

This property is of type Set<String>.

["channel1", "channel2"]
Last updated on