Channel Groups API for Java SDK

Breaking changes in v9.0.0

PubNub Java SDK version 9.0.0 unifies the codebases for Java and Kotlin 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 Java 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.

Add Channels

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 adds a channel to a channel group.

Method(s)

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

Maximum number of channels

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

this.pubnub.addChannelsToChannelGroup()
.channelGroup(String)
.channels(Array)
* required
ParameterDescription
channelGroup *
Type: String
The channelGroup to add the channels to.
channels *
Type: Array
The channel to add to the channel group.
async *
Type: Consumer<Result>
Consumer of a Result of type PNChannelGroupsAddChannelResult

Basic Usage

Reference code

This example is a self-contained code snippet ready to be run. It includes necessary imports and executes methods with console logging. Use it as a reference when working with other examples in this document.

Add Channels

import com.pubnub.api.PubNubException;
import com.pubnub.api.UserId;
import com.pubnub.api.java.PubNub;
import com.pubnub.api.java.v2.PNConfiguration;
import java.util.Arrays;

public class AddChannelsToChannelGroupApp {
public static void main(String[] args) throws PubNubException {
PNConfiguration.Builder configBuilder = PNConfiguration.builder(new UserId("demoUserId"), "demo");
configBuilder.publishKey("demo");
PubNub pubnub = PubNub.create(configBuilder.build());

pubnub.addChannelsToChannelGroup()
.channelGroup("cg1")
.channels(Arrays.asList("ch1", "ch2", "ch3"))
show all 24 lines

Response

The addChannelsToChannelGroup() does not return actionable data, be sure to check the status object on the outcome of the operation by checking the status.isError().

{
"service" : "channel-registry",
"status" : 200,
"error" : false,
"message" : "OK"
}

List Channels

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 the channels of the channel group.

Method(s)

Listing Channels is accomplished by using the following method(s) in the Java SDK:

pubnub.listChannelsForChannelGroup()
.channelGroup(String)
.async(result -> { /* check result */ });
* required
ParameterDescription
channelGroup *
Type: String
Channel group to fetch the channels.
async *
Type: Consumer<Result>
Consumer of a Result of type PNChannelGroupsAllChannelsResult.

Basic Usage

List Channels

pubnub.listChannelsForChannelGroup()
.channelGroup("cg1")
.async(result -> { /* check result */ });

Returns

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

MethodDescription
getChannels()
Type: List<String>
List of channels of a channel group.

Remove Channels

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 the channels from the channel group.

Method(s)

Removing Channels is accomplished by using the following method(s) in the Java SDK:

pubnub.removeChannelsFromChannelGroup()
.channelGroup(String)
.channels(Array)
* required
ParameterDescription
channels *
Type: Array
The channels to remove from the channel group.
channelGroup *
Type: String
Specifies channelGroup to remove the channels from.
async
Type: Consumer<Result>
Consumer of a Result of type PNChannelGroupsRemoveChannelResult.

Basic Usage

Remove channels

pubnub.removeChannelsFromChannelGroup()
.channelGroup("family")
.channels(Arrays.asList("son"))
.async(result -> { /* check result */ });

Response

The removeChannelsFromChannelGroup() does not return actionable data, be sure to check the status object on the outcome of the operation by checking the status.isError().

{
"status" : 200,
"message" : "OK",
"service" : "channel-registry",
"error" : False
}

Delete 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 the channel group.

Method(s)

Deleting Channel Group is accomplished by using the following method(s) in the Java SDK:

pubnub.deleteChannelGroup()
.channelGroup(String)
* required
ParameterDescription
channelGroup *
Type: String
Specifies channelGroup to remove.
async
Type: Consumer<Result>
Consumer of a Result of type PNChannelGroupsDeleteGroupResult.

Basic Usage

Delete Channel Group

pubnub.deleteChannelGroup()
.channelGroup("family")
.async(result -> { /* check result */ });

Response

The deleteChannelGroup() does not return actionable data, be sure to check the status object on the outcome of the operation by checking the status.isError().

{
"status" : 200,
"message" : "OK",
"service" : "channel-registry",
"error" : False
}
Last updated on