Channel Groups API for Objective-C 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. See Enable the features your app calls.

This function adds channels to a channel group.

Method(s)​

Adding Channels is accomplished by using the following method(s) in the Objective-C SDK:

1- (void)addChannels:(NSArray<NSString *> *)channels 
2 toGroup:(NSString *)group
3 withCompletion:(nullable PNChannelGroupChangeCompletionBlock)block;
* required
ParameterDescription
channels *
Type: NSArray
List of channel names which should be added to the group.
group *
Type: NSString
Name of the group into which channels should be added.
block
Type: PNChannelGroupChangeCompletionBlock
Channels addition process completion block which pass only one argument - request processing status to report about how data pushing was successful or not.

Sample code​

Add channels​

1// Initialize PubNub client with your keys
2PNConfiguration *configuration = [PNConfiguration configurationWithPublishKey:@"demo"
3 subscribeKey:@"demo"
4 userID:@"testUser"];
5PubNub *client = [PubNub clientWithConfiguration:configuration];
6
7// Define channel group and channels to add
8NSString *channelGroup = @"family";
9NSArray *channels = @[@"wife", @"husband", @"children"];
10
11NSLog(@"Adding channels %@ to channel group: %@", channels, channelGroup);
12
13// Add channels to the channel group
14PNChannelGroupManageRequest *request = [PNChannelGroupManageRequest requestToAddChannels:channels
15 toChannelGroup:channelGroup];
show all 35 lines

Response​

Response objects which is returned by client when Add Channels to Group API is used:

1@interface PNAcknowledgmentStatus : PNErrorStatus
2
3@end

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. See Enable the features your app calls.

This function lists all channels in a channel group.

Method(s)​

Listing Channels is accomplished by using the following method(s) in the Objective-C SDK:

1- (void)channelsForGroup:(NSString *)group 
2 withCompletion:(PNGroupChannelsAuditCompletionBlock)block;
* required
ParameterDescription
group *
Type: NSString
Name of the group from which channels should be fetched.
block *
Type: PNClientChannelsForGroupRequestHandlingBlock
Channels audition process completion block which pass two arguments: result - in case of successful request processing data field will contain results of channel groups channels audition operation; status - in case if error occurred during request processing.

Sample code​

List channels​

1NSString *channelGroup = @"family";
1[self.client channelsForGroup:channelGroup withCompletion:^(PNChannelGroupChannelsResult *result,
2 PNErrorStatus *status) {
3
4 if (!status) {
5
6 // Handle downloaded list of chanels using: result.data.channels
7 }
8 else {
9
10 /**
11 Handle channels for group audition error. Check 'category' property
12 to find out possible reason because of which request did fail.
13 Review 'errorData' property (which has PNErrorData data type) of status
14 object to get additional information about issue.
15
show all 19 lines

Response​

Response objects which is returned by client when Add Channels to Group API is used:

1@interface PNChannelGroupChannelsData : PNServiceData
2
3// Registered channels within channel group.
4@property (nonatomic, readonly, strong) NSArray<NSString *> *channels;
5
6@end
7
8@interface PNChannelGroupChannelsResult : PNResult
9
10// Stores reference on channel group's channels list audit request processing information.
11@property (nonatomic, nonnull, readonly, strong) PNChannelGroupChannelsData *data;
12
13@end

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. See Enable the features your app calls.

This function removes channels from a channel group.

Method(s)​

Removing Channels is accomplished by using the following method(s) in the Objective-C SDK:

1- (void)removeChannels:(NSArray<NSString *> *)channels 
2 fromGroup:(NSString *)group
3 withCompletion:(nullable PNChannelGroupChangeCompletionBlock)block;
* required
ParameterDescription
channels *
Type: NSArray
List of channel names which should be removed from group. If empty list passed whole channel group will be removed.
group *
Type: NSString
Channel group from which channels should be removed.
block
Type: PNChannelGroupChangeCompletionBlock
Channels removal process completion block which pass only one argument - request processing status to report about how data pushing was successful or not.

Sample code​

Remove channels​

1NSString *channelGroup = @"family";
1[self.client removeChannels:@[@"son"] fromGroup:channelGroup
2 withCompletion:^(PNAcknowledgmentStatus *status) {
3
4 if (!status.isError) {
5
6 // Handle successful channels list modification for group.
7 }
8 else {
9
10 /**
11 Handle channels list modification for group error. Check 'category' property
12 to find out possible reason because of which request did fail.
13 Review 'errorData' property (which has PNErrorData data type) of status
14 object to get additional information about issue.
15
show all 19 lines

Response​

Response objects which is returned by client when Remove Channels to Group API is used:

1@interface PNAcknowledgmentStatus : PNErrorStatus
2
3@end

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. See Enable the features your app calls.

This function deletes a channel group.

Method(s)​

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

1- (void)removeChannelsFromGroup:(NSString *)group 
2 withCompletion:(nullable PNChannelGroupChangeCompletionBlock)block;
* required
ParameterDescription
group *
Type: NSString
Name of the group from which all channels should be removed.
block
Type: PNChannelGroupChangeCompletionBlock
Channel group removal process completion block which pass only one argument - request processing status to report about how data pushing was successful or not.

Sample code​

Deleting Channel Group :

1NSString *channelGroup = @"family";
1[self.client removeChannelsFromGroup:channelGroup withCompletion:^(PNAcknowledgmentStatus *status) {
2
3 if (!status.isError) {
4
5 // Handle successful channel group removal.
6 }
7 else {
8
9 /**
10 Handle channel group removal error. Check 'category' property
11 to find out possible reason because of which request did fail.
12 Review 'errorData' property (which has PNErrorData data type) of status
13 object to get additional information about issue.
14
15 Request can be resent using: [status retry];
show all 18 lines

Response​

Response objects which is returned by client when Remove Channel Group API is used:

1@interface PNAcknowledgmentStatus : PNErrorStatus
2
3@end

Was this page useful?

Last updated on