PubNub LogoDocs
SupportContact SalesLoginTry Our APIs

›API Reference

kotlin

  • 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 Kotlin SDK

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.

Calling Kotlin methods

Most PubNub Kotlin SDK method invocations return an Endpoint object, which allows you to decide whether to perform the operation synchronously or asynchronously. You must choose one of these or the operation will not be performed at all.

For example, the following code is valid and will compile, but the publish won't be performed:

pubnub.publish(
    message = "this sdk rules!",
    channel = "my_channel"
)

To successfully publish a message, you must follow the actual method invocation with whether to perform it synchronously or asynchronously, for example:

pubnub.publish(
    message = "this sdk rules!",
    channel = "my_channel"
).async { result, status ->
    if (status.error) {
        // handle error
    } else {
        // handle successful method result
    }
}

Adding Channels

Requires Stream Controller add-onRequires 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.

Methods

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

Note

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

pubnub.addChannelsToChannelGroup(
    channelGroup: String,
    channels: List<String>
).async { result, status -> }
ParameterTypeRequiredDescription
channelGroupStringYesThe channel group to add the channels to.
channelsList<String>YesThe channels to add to the channel group.

Basic Usage

pubnub.addChannelsToChannelGroup(
    channelGroup = "cg1"
    channels = listOf("ch1", "ch2", "ch3")
).async { result, status -> }

Response

The addChannelsToChannelGroup() doesn't return actionable data, be sure to check the status object on the outcome of the operation by checking the status.error.

Listing Channels

Requires Stream Controller add-onRequires 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.

Methods

Listing channels is accomplished by using the following method(s) in the Kotlin SDK:

pubnub.listChannelsForChannelGroup(
    channelGroup: String
).async { result, status -> }
ParameterTypeRequiredDescription
channelGroupStringYesChannel group to fetch the belonging channels

Basic Usage

pubnub.listChannelsForChannelGroup(
    channelGroup = "cg1"
).async { result, status ->
    if (!status.error) {
        result!!.channels // ["ch1", "ch2", "ch3"]
    } else {
        // handle error
        status.exception?.printStackTrace()
    }
}

Returns

The listChannelsForChannelGroup() operation returns a PNChannelGroupsAllChannelsResult which contains the following operations:

MethodTypeDescription
channelsList<String>List of channels of a channel group.

Removing Channels

Requires Stream Controller add-onRequires 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 Kotlin SDK:

pubnub.removeChannelsFromChannelGroup(
    channels: List<String>,
    channelGroup: String
).async { result, status -> }
ParameterTypeRequiredDescription
channelsList<String>YesThe channels to remove from the channel group
channelGroupStringYesThe channel group to remove channels from

Basic Usage

pubnub.removeChannelsFromChannelGroup(
    channelGroup = "cg1"
).async { result, status -> }

Returns

The removeChannelsFromChannelGroup() doesn't return actionable data, be sure to check the status object on the outcome of the operation by checking the status.error.

Deleting Channel Group

Requires Stream Controller add-onRequires 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 Kotlin V4 SDK:

pubnub.deleteChannelGroup(
    channelGroup: String
).async { result, status -> }
ParameterTypeRequiredDescription
channelGroupStringYesThe channel group to remove

Basic Usage

pubnub.deleteChannelGroup(
    channelGroup = "cg1"
).async { result, status -> }

Returns

The deleteChannelGroup() doesn't return actionable data, be sure to check the status object on the outcome of the operation by checking the status.error.

← Access ManagerMessage Persistence →
  • Adding Channels
    • Description
    • Methods
    • Basic Usage
    • Response
  • Listing Channels
    • Description
    • Methods
    • Basic Usage
    • Returns
  • Removing Channels
    • Description
    • Method(s)
    • Basic Usage
    • Returns
  • Deleting Channel Group
    • Description
    • Method(s)
    • Basic Usage
    • Returns
© PubNub Inc. - Privacy Policy