Channel Groups API for PubNub Dart SDK

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

Learn more about our Channel Groups here.

Channel Group operations

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

Adding 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.

Description

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"
}

Listing 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.

Description

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.

Removing 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.

Description

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"
}

Deleting 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.

Description

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

Description

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