PubNub LogoDocs
SupportContact SalesLoginTry Our APIs

›API Reference

swift

  • Getting Started
  • API Reference

    • Configuration
    • Publish & Subscribe
    • Presence
    • Access Manager
    • Channel Groups
    • Message Persistence
    • Mobile Push
    • Objects
    • Files
    • Message Actions
    • Miscellaneous
  • Status Events
  • Troubleshooting
  • Change Log
  • Feature Support
  • Platform Support

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.

  1. add(  channels: [String],  to group: String,  custom requestConfig: RequestConfiguration = RequestConfiguration(),  completion: ((Result<[String: [String]], Error>) -> Void)?)
    
    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.

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

  1. listChannels(  for group: String,  custom requestConfig: RequestConfiguration = RequestConfiguration(),  completion: ((Result<(group: String, channels: [String]), Error>) -> Void)?)
    
    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.

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

  1. remove(  channels: [String],  from group: String,  custom requestConfig: RequestConfiguration = RequestConfiguration(),  completion: ((Result<(group: String, channels: [String]), Error>) -> Void)?)
    
    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.

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

  1. listChannelGroups(  custom requestConfig: RequestConfiguration = RequestConfiguration(),  completion: ((Result<[String], Error>) -> Void)?)
    
    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.

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

  1. remove(  channelGroup: String,  custom requestConfig: RequestConfiguration = RequestConfiguration(),  completion: ((Result<String, Error>) -> Void)?)
    
    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.

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)")
  }
}
← Access ManagerMessage Persistence →
  • Adding Channels
    • Description
    • Method(s)
    • Basic Usage
  • Listing Channels
    • Description
    • Method(s)
    • Basic Usage
  • Removing Channels
    • Description
    • Method(s)
    • Basic Usage
  • Listing Channel Groups
    • Description
    • Method(s)
    • Basic Usage
  • Deleting Channel Group
    • Description
    • Method(s)
    • Basic Usage
© PubNub Inc. - Privacy Policy