Channel Groups API for PubNub Swift Native SDK
Note
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 backend-channels the channel group contains.
Learn more about our Channel Groups here.
Adding Channels
Requires Stream Controller add-on Requires that the Stream Controller add-on is enabled for your key. See this page on enabling add-on features on your keys:
https://support.pubnub.com/hc/en-us/articles/360051974791-How-do-I-enable-add-on-features-for-my-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:
Note
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)?)
Parameter Type Required Defaults Description channels
[String] Yes List of channels
to add to the group.to
String Yes The Channel Group to add the list of channels to. custom
RequestConfiguration Optional RequestConfiguration()
An object that allows for per-request customization of PubNub Configuration or Network Session completion
((Result<(group: String, channels: [String]), Error>) -> Void)? Optional nil
The async Result
of the method callCompletion Handler Result
- Success A
Tuple
containing the channel-group and theArray
of channels added. - Failure An
Error
describing the failure.
- Success A
Basic Usage
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 Requires that the Stream Controller add-on is enabled for your key. See this page on enabling add-on features on your keys:
https://support.pubnub.com/hc/en-us/articles/360051974791-How-do-I-enable-add-on-features-for-my-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)?)
Parameter Type Required Defaults Description for
String Yes The channel group to list channels on. custom
RequestConfiguration Optional RequestConfiguration()
An object that allows for per-request customization of PubNub Configuration or Network Session completion
((Result<(group: String, channels: [String]), Error>) -> Void)? Optional nil
The async Result
of the method callCompletion Handler Result
- Success A
Tuple
containing the channel-group and theArray
of its channels. - Failure An
Error
describing the failure.
- Success A
Basic Usage
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 Requires that the Stream Controller add-on is enabled for your key. See this page on enabling add-on features on your keys:
https://support.pubnub.com/hc/en-us/articles/360051974791-How-do-I-enable-add-on-features-for-my-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)?)
Parameter Type Required Defaults Description channels
[String] Yes The list of channels
to remove from the channel group.from
String Yes The channel group to remove channels from. custom
RequestConfiguration Optional RequestConfiguration()
An object that allows for per-request customization of PubNub Configuration or Network Session completion
((Result<(group: String, channels: [String]), Error>) -> Void)? Optional nil
The async Result
of the method callCompletion Handler Result
- Success A
Tuple
containing the channel-group and theArray
of channels removed. - Failure An
Error
describing the failure.
- Success A
Basic Usage
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 Requires that the Stream Controller add-on is enabled for your key. See this page on enabling add-on features on your keys:
https://support.pubnub.com/hc/en-us/articles/360051974791-How-do-I-enable-add-on-features-for-my-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)?)
Parameter Type Required Defaults Description custom
RequestConfiguration Optional RequestConfiguration()
An object that allows for per-request customization of PubNub Configuration or Network Session completion
((Result<[String], Error>) -> Void)? Optional nil
The async Result
of the method callCompletion Handler Result
- Success List of all channel-groups.
- Failure An
Error
describing the failure.
Basic Usage
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 Requires that the Stream Controller add-on is enabled for your key. See this page on enabling add-on features on your keys:
https://support.pubnub.com/hc/en-us/articles/360051974791-How-do-I-enable-add-on-features-for-my-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)?)
Parameter Type Required Defaults Description channelGroup
String Yes The channel group to delete. custom
RequestConfiguration Optional RequestConfiguration()
An object that allows for per-request customization of PubNub Configuration or Network Session completion
((Result<String, Error>) -> Void)? Optional nil
The async Result
of the method callCompletion Handler Result
- Success The channel-group that was removed.
- Failure An
Error
describing the failure.
Basic Usage
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)")
}
}