---
source_url: https://www.pubnub.com/docs/sdks/java/api-reference/channel-groups
title: Channel Groups API for Java SDK
updated_at: 2026-05-18T12:50:07.174Z
sdk_name: PubNub Java SDK
sdk_version: 6.4.5
---

> Documentation Index
> For a curated overview of PubNub documentation, see: https://www.pubnub.com/docs/llms.txt
> For the full list of all documentation pages, see: https://www.pubnub.com/docs/llms-full.txt


# Channel Groups API for Java SDK

PubNub Java SDK, use the latest version: 6.4.5

Install:

```bash
Add PubNub dependency to your build@6.4.5
```

:::warning Breaking changes in v9.0.0
PubNub Java SDK version 9.0.0 unifies the codebases for Java and [Kotlin](https://www.pubnub.com/docs/sdks/kotlin) SDKs, introduces a new way of instantiating the PubNub client, and changes asynchronous API callbacks and emitted [status events](https://www.pubnub.com/docs/sdks/java/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](https://www.pubnub.com/docs/sdks/kotlin/migration-guides/kotlin-v9-migration-guide).
:::

[Channel groups](https://www.pubnub.com/docs/general/channels/subscribe#channel-groups) allow PubNub developers to bundle thousands of [channels](https://www.pubnub.com/docs/general/channels/overview) 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.

:::note 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 to a channel group

:::note Requires Stream Controller add-on
This method requires that the *Stream Controller* add-on is enabled for your key in the PubNub [Admin Portal](https://admin.pubnub.com/). Read the [support page](https://support.pubnub.com/hc/en-us/articles/360051974791-How-do-I-enable-add-on-features-for-my-keys-) on enabling add-on features on your keys.
:::

This function adds channels to a channel group.

### Method(s)

Use the following method in the Java SDK:

:::note Maximum number of channels
You can add up to 200 channels to a channel group per API call.
:::

```java
this.pubnub.addChannelsToChannelGroup()
    .channelGroup(String)
    .channels(Array)
```

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| channelGroup | String | Yes |  | The channel group to add the channels to. |
| channels | Array | Yes |  | The channels to add to the channel group. |
| async | Consumer<Result> | Yes |  | `Consumer` of a `Result` of type `PNChannelGroupsAddChannelResult` |

### Sample code

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

```java
import com.pubnub.api.PubNubException;
import com.pubnub.api.UserId;
import com.pubnub.api.java.PubNub;
import com.pubnub.api.java.v2.PNConfiguration;
import com.pubnub.api.models.consumer.channel_group.PNChannelGroupsAddChannelResult;

import java.util.Arrays;

public class ChannelGroupsApp {
    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"))
                .async(result -> {
                    result.onSuccess((PNChannelGroupsAddChannelResult res) -> {
                        System.out.println("Channels added to channel group successfully.");
                    }).onFailure((PubNubException exception) -> {
                        System.out.println("Error adding channels to channel group: " + exception.getMessage());
                    });
                });
    }
}
```

### Response

The `addChannelsToChannelGroup()` does not 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

:::note Requires Stream Controller add-on
This method requires that the *Stream Controller* add-on is enabled for your key in the [Admin Portal](https://admin.pubnub.com/). Read the [support page](https://support.pubnub.com/hc/en-us/articles/360051974791-How-do-I-enable-add-on-features-for-my-keys-) 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 Java SDK:

```java
pubnub.listChannelsForChannelGroup()
    .channelGroup(String)
    .async(result -> { /* check result */ });
```

| Parameter | Description |
| --- | --- |
| `channelGroup` *Type: String | The channel group for which to list channels. |
| `async` *Type: `Consumer<Result>` | `Consumer` of a `Result` of type `PNChannelGroupsAllChannelsResult`. |

### Sample code

#### List channels

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

### Returns

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

| Method | Description |
| --- | --- |
| `getChannels()`Type: List`<String>` | List of `channels` of a `channel group`. |

## Remove channels from a channel group

:::note Requires Stream Controller add-on
This method requires that the *Stream Controller* add-on is enabled for your key in the [Admin Portal](https://admin.pubnub.com/). Read the [support page](https://support.pubnub.com/hc/en-us/articles/360051974791-How-do-I-enable-add-on-features-for-my-keys-) on enabling add-on features on your keys.
:::

This function removes channels from a channel group.

### Method(s)

Use the following method in the Java SDK:

```java
pubnub.removeChannelsFromChannelGroup()
    .channelGroup(String)
    .channels(Array)
```

| Parameter | Description |
| --- | --- |
| `channels` *Type: Array | The channels to remove from the channel group. |
| `channelGroup` *Type: String | The channel group from which to remove the channels. |
| `async`Type: `Consumer<Result>` | `Consumer` of a `Result` of type `PNChannelGroupsRemoveChannelResult`. |

### Sample code

#### Remove channels

```java
pubNub.removeChannelsFromChannelGroup()
        .channelGroup("family")
        .channels(Arrays.asList("son"))
        .sync();
```

### Response

The `removeChannelsFromChannelGroup()` does not 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

:::note Requires Stream Controller add-on
This method requires that the *Stream Controller* add-on is enabled for your key in the [Admin Portal](https://admin.pubnub.com/). Read the [support page](https://support.pubnub.com/hc/en-us/articles/360051974791-How-do-I-enable-add-on-features-for-my-keys-) on enabling add-on features on your keys.
:::

This function deletes a channel group.

### Method(s)

Use the following method in the Java SDK:

```java
pubnub.deleteChannelGroup()
    .channelGroup(String)
```

| Parameter | Description |
| --- | --- |
| `channelGroup` *Type: String | The channel group to delete. |
| `async`Type: `Consumer<Result>` | `Consumer` of a `Result` of type `PNChannelGroupsDeleteGroupResult`. |

### Sample code

#### Delete channel group

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

### Response

The `deleteChannelGroup()` does not 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 -> { });`.

## Terms in this document

* **Channel** - A pathway for sending and receiving messages between devices, created automatically when you first use it, that can handle any number of users and messages for different communication needs, like 1-1 text chats, group conversations, and other data streaming.
* **PubNub** - PubNub is a real-time messaging platform that provides APIs and SDKs for building scalable applications. It handles the complex infrastructure of real-time communication, including: Message delivery and persistence, Presence detection, Access control, Push notifications, File sharing, Serverless processing with Functions and Events & Actions, Analytics and monitoring with BizOps Workspace, AI-powered insights with Illuminate.