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

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

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

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.

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

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

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

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

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

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

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

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

Delete 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