SwiftSwift Native Channel Groups API Reference for Realtime Apps

 
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


Requires Stream Controller add-on XRequires that the Stream Controller add-on is enabled for your key. See this page on enabling add-on features on your keys:

http://www.pubnub.com/knowledge-base/discussion/644/how-do-i-enable-add-on-features-for-my-keys.
This function adds a channel to a channel group.
Adding Channels is accomplished by using the following method(s) in the Swift SDK:
 

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

  1. ParameterTypeRequiredDefaultsDescription
    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.
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)")
  }
}

Requires Stream Controller add-on XRequires that the Stream Controller add-on is enabled for your key. See this page on enabling add-on features on your keys:

http://www.pubnub.com/knowledge-base/discussion/644/how-do-i-enable-add-on-features-for-my-keys.
This function lists all the channels of the channel group.
Listing Channels is accomplished by using the following method(s) in the Swift SDK:
  1. ParameterTypeRequiredDefaultsDescription
    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.
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)")
  }
}

Requires Stream Controller add-on XRequires that the Stream Controller add-on is enabled for your key. See this page on enabling add-on features on your keys:

http://www.pubnub.com/knowledge-base/discussion/644/how-do-i-enable-add-on-features-for-my-keys.
This function removes the channels from the channel group.
Removing Channels is accomplished by using the following method(s) in the Swift SDK:
  1. ParameterTypeRequiredDefaultsDescription
    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.
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)")
  }
}
Requires Stream Controller add-on XRequires that the Stream Controller add-on is enabled for your key. See this page on enabling add-on features on your keys:

http://www.pubnub.com/knowledge-base/discussion/644/how-do-i-enable-add-on-features-for-my-keys.
This function lists all the channel groups.
Listing Channel Groups is accomplished by using the following method(s) in the Swift SDK:
  1. ParameterTypeRequiredDefaultsDescription
    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.
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)")
  }
}

Requires Stream Controller add-on XRequires that the Stream Controller add-on is enabled for your key. See this page on enabling add-on features on your keys:

http://www.pubnub.com/knowledge-base/discussion/644/how-do-i-enable-add-on-features-for-my-keys.
This function removes the channel group.
Deleting Channel Group is accomplished by using the following method(s) in the Swift SDK:
  1. ParameterTypeRequiredDefaultsDescription
    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.
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)")
  }
}