Channel Groups API for PubNub Swift Native SDK

Migration guide

The PubNub Swift 3.0 SDK contains many significant changes from the 2.x SDK, including breaking changes. Please refer to the PubNub Swift 3.0 Migration Guide for more details.

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.

Method(s)

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

Maximum number of channels

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

add(  channels: [String],  to group: String,  custom requestConfig: RequestConfiguration = RequestConfiguration(),  completion: ((Result<[String: [String]], Error>) -> Void)?)
ParameterTypeRequiredDefaultDescription
channels[String]YesList of channels to add to the group.
toStringYesThe Channel Group to add the list of channels to.
customRequestConfigurationOptionalRequestConfiguration()An object that allows for per-request customization of PubNub Configuration or Network Session
completion((Result<(group: String, channels: [String]), Error>) -> Void)?OptionalnilThe async Result of the method call

Completion Handler Result

Success

A Tuple containing the channel-group and the Array of channels added.

Failure

An Error describing the failure.

Basic Usage

Adding Channels

pubnub.add(
channels: ["channelSwift", "otherChannel"],
to: "SwiftGroup"
) { result in
switch result {
case let .success(response):
print("The channel-group `\(response.group)` had the following channels added: \(response.channels)")
case let .failure(error):
print("Failed Add Channels Response: \(error.localizedDescription)")
}
}

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.

Method(s)

Listing Channels is accomplished by using the following method(s) in the Swift SDK:

listChannels(  for group: String,  custom requestConfig: RequestConfiguration = RequestConfiguration(),  completion: ((Result<(group: String, channels: [String]), Error>) -> Void)?)
ParameterTypeRequiredDefaultDescription
forStringYesThe channel group to list channels on.
customRequestConfigurationOptionalRequestConfiguration()An object that allows for per-request customization of PubNub Configuration or Network Session
completion((Result<(group: String, channels: [String]), Error>) -> Void)?OptionalnilThe async Result of the method call

Completion Handler Result

Success

A Tuple containing the channel-group and the Array of its channels.

Failure

An Error describing the failure.

Basic Usage

Listing Channels

pubnub.listChannels(for: "family") { result in
switch result {
case let .success(response):
print("The channel-group `\(response.group)` is made of the following channels: \(response.channels)")
case let .failure(error):
print("Failed Add Channels Response: \(error.localizedDescription)")
}
}

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 Swift SDK:

remove(  channels: [String],  from group: String,  custom requestConfig: RequestConfiguration = RequestConfiguration(),  completion: ((Result<(group: String, channels: [String]), Error>) -> Void)?)
ParameterTypeRequiredDefaultDescription
channels[String]YesThe list of channels to remove from the channel group.
fromStringYesThe channel group to remove channels from.
customRequestConfigurationOptionalRequestConfiguration()An object that allows for per-request customization of PubNub Configuration or Network Session
completion((Result<(group: String, channels: [String]), Error>) -> Void)?OptionalnilThe async Result of the method call

Completion Handler Result

Success

A Tuple containing the channel-group and the Array of channels removed.

Failure

An Error describing the failure.

Basic Usage

Removing channels

pubnub.remove(
channels: ["channelSwift", "otherChannel"],
from: "SwiftGroup"
) { result in
switch result {
case let .success(response):
print("The channel-group `\(response.group)` had the following channels removed: \(response.channels)")
case let .failure(error):
print("Failed Add Channels Response: \(error.localizedDescription)")
}
}

Listing Channel Groups

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 channel groups.

Method(s)

Listing Channel Groups is accomplished by using the following method(s) in the Swift SDK:

listChannelGroups(  custom requestConfig: RequestConfiguration = RequestConfiguration(),  completion: ((Result<[String], Error>) -> Void)?)
ParameterTypeRequiredDefaultDescription
customRequestConfigurationOptionalRequestConfiguration()An object that allows for per-request customization of PubNub Configuration or Network Session
completion((Result<[String], Error>) -> Void)?OptionalnilThe async Result of the method call

Completion Handler Result

Success

List of all channel-groups.

Failure

An Error describing the failure.

Basic Usage

Listing Channel Groups

pubnub.listChannelGroups { result in
switch result {
case let .success(channelGroups):
print("List of all channel-groups: \(channelGroups)")
case let .failure(error):
print("Failed Channel Groups Response: \(error.localizedDescription)")
}
}

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 the channel group.

Method(s)

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

remove(  channelGroup: String,  custom requestConfig: RequestConfiguration = RequestConfiguration(),  completion: ((Result<String, Error>) -> Void)?)
ParameterTypeRequiredDefaultDescription
channelGroupStringYesThe channel group to delete.
customRequestConfigurationOptionalRequestConfiguration()An object that allows for per-request customization of PubNub Configuration or Network Session
completion((Result<String, Error>) -> Void)?OptionalnilThe async Result of the method call

Completion Handler Result

Success

The channel-group that was removed.

Failure

An Error describing the failure.

Basic Usage

Deleting Channel Group

pubnub.delete(channelGroup: "channelSwift") { result in
switch result {
case let .success(channelGroup):
print("The channel-group that was removed: \(channelGroup)")
case let .failure(error):
print("Failed Add Channels Response: \(error.localizedDescription)")
}
}
Last updated on