On this page

Channel Groups API for 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 to a channel group

Requires Stream Controller add-on

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

This function adds channels to a channel group.

Method(s)

Use the following method in the Dart SDK:

Maximum number of channels

You can add up to 200 channels to a channel group per API call.

1pubnub.channelGroups.addChannels(
2 String group,
3 Set<String> channels,
4 {Keyset? keyset,
5 String? using}
6)
* required
ParameterDescription
group *
Type: String
The channel group to add the channels to.
channels *
Type: Set<String>
The channels to add to the channel group.
keyset
Type: Keyset
Override for the PubNub default keyset configuration.
using
Type: String
Keyset name from the keysetStore to be used for this method call.

Sample code

Reference code
This example is a self-contained code snippet ready to be run. It includes necessary imports and executes methods with console logging. Use it as a reference when working with other examples in this document.
1import 'package:pubnub/pubnub.dart';
2
3void main() async {
4 // Create PubNub instance with default keyset.
5 var pubnub = PubNub(
6 defaultKeyset: Keyset(
7 subscribeKey: 'demo',
8 publishKey: 'demo',
9 userId: UserId('myUniqueUserId')
10 ),
11 );
12
13 // Define the channel group and channels to add.
14 var group = 'cg1';
15 var channels = {'ch1', 'ch2'};
show all 22 lines

Response

The addChannels() method returns a ChannelGroupChangeChannelsResult.

1{
2 "service": "channel-registry",
3 "status": "200",
4 "error": false,
5 "message": "OK"
6}

List channels in a 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 lists all channels in a channel group.

Method(s)

Use the following method in the Dart SDK:

1pubnub.channelGroups.listChannels(
2 String group,
3 {Keyset? keyset,
4 String? using}
5)
* required
ParameterDescription
group *
Type: String
The channel group for which to list channels.
keyset
Type: Keyset
Override for the PubNub default keyset configuration.
using
Type: String
Keyset name from the keysetStore to be used for this method call.

Sample code

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

Returns

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

MethodDescription
channels
Type: Set<String>
List of channels of a channel group.
name
Type: String
Channel group name.

Remove channels from a 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 channels from a channel group.

Method(s)

Use the following method in the Dart SDK:

1pubnub.channelGroups.removeChannels(
2 String group,
3 Set<String> channels,
4 {Keyset? keyset,
5 String? using}
6)
* required
ParameterDescription
group *
Type: String
The channel group to remove the channels from.
channels *
Type: Set<String>
The channels to remove from the channel group.
keyset
Type: Keyset
Override for the PubNub default keyset configuration.
using
Type: String
Keyset name from the keysetStore to be used for this method call.

Sample code

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

Returns

The removeChannels method returns a ChannelGroupChangeChannelsResult.

1{
2 "service": "channel-registry",
3 "status": "200",
4 "error": false,
5 "message": "OK"
6}

Delete a 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 deletes a channel group.

Method(s)

Use the following method in the Dart SDK:

1pubnub.channelGroups.delete(
2 String group,
3 {Keyset? keyset,
4 String? using}
5)
* required
ParameterDescription
group *
Type: String
The channel group to remove all channels from.
keyset
Type: Keyset
Override for the PubNub default keyset configuration.
using
Type: String
Keyset name from the keysetStore to be used for this method call.

Sample code

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

Returns

The delete method returns a ChannelGroupDeleteResult.

1{
2"service": "channel-registry",
3"status": "200",
4"error": false,
5"message": "OK"
6}

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:

1// property of `Subscription` class
2subscription.channelGroups

Sample code

1var subscription = pubnub.subscribe(channelGroups: {'cg1', 'cg2'});
2var subscribedChannelGroups = subscription.channelGroups;
3
4print('subscribed channel groups are $subscribedChannelGroups');

Response

This property is of type Set<String>.

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