Channel Groups API for Kotlin SDK
Breaking changes in v9.0.0
PubNub Kotlin SDK version 9.0.0 unifies the codebases for Kotlin and Java SDKs, introduces a new way of instantiating the PubNub client, and changes asynchronous API callbacks and emitted status events. These changes can impact applications built with previous versions (< 9.0.0
) of the Kotlin SDK.
For more details about what has changed, refer to Java/Kotlin SDK migration guide.
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.
Request execution
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 invoke the .sync()
or .async()
method on the Endpoint to execute the request, or the operation will not be performed.
val channel = pubnub.channel("channelName")
channel.publish("This SDK rules!").async { result ->
result.onFailure { exception ->
// Handle error
}.onSuccess { value ->
// Handle successful method result
}
}
Add channels to a channel group
Requires Stream Controller add-on
This method requires that the Stream Controller add-on is enabled for your key in the PubNub Admin Portal. Read the support page on enabling add-on features on your keys.
This function adds channels to a channel group.
Method(s)
Use the following method in the Kotlin SDK:
Maximum number of channels
You can add up to 200 channels to a channel group per API call.
pubnub.addChannelsToChannelGroup(
channelGroup: String,
channels: List<String>
).async { result -> }
Parameter | Description |
---|---|
channelGroup *Type: String | The channel group to add the channels to. |
channels *Type: List<String> | The channels to add to the channel group. |
Sample code
Reference code
Response
The addChannelsToChannelGroup()
doesn't return actionable data, be sure to check result object on the outcome of the operation by checking the result.isFailure
or handling exception result.onFailure(exception -> { })
.
List channels in a 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 lists all channels in a channel group.
Method(s)
Use the following method in the Kotlin SDK:
pubnub.listChannelsForChannelGroup(
channelGroup: String
).async { result -> }
Parameter | Description |
---|---|
channelGroup *Type: String | The channel group for which to list channels. |
Sample code
Returns
The listChannelsForChannelGroup()
operation returns a PNChannelGroupsAllChannelsResult
which contains the following operations:
Method | Description |
---|---|
channels Type: List<String> | List of channels of a channel group . |
Remove channels from a 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 channels from a channel group.
Method(s)
Use the following method in the Kotlin SDK:
pubnub.removeChannelsFromChannelGroup(
channels: List<String>,
channelGroup: String
).async { result -> }
Parameter | Description |
---|---|
channels *Type: List<String> | The channels to remove from the channel group. |
channelGroup *Type: String | The channel group from which to remove channels. |
Sample code
Returns
The removeChannelsFromChannelGroup()
doesn't return actionable data, be sure to check result object on the outcome of the operation by checking the result.isFailure
or handling exception result.onFailure(exception -> { })
.
Delete a 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 deletes a channel group.
Method(s)
Use the following method in the Kotlin SDK:
pubnub.deleteChannelGroup(
channelGroup: String
).async { result -> }
Parameter | Description |
---|---|
channelGroup *Type: String | The channel group to delete. |
Sample code
Returns
The deleteChannelGroup()
doesn't return actionable data, be sure to check result object on the outcome of the operation by checking the result.isFailure
or handling exception result.onFailure(exception -> { })
.