---
source_url: https://www.pubnub.com/docs/sdks/php/api-reference/channel-groups
title: Channel Groups API for PHP SDK
updated_at: 2026-06-19T11:38:04.846Z
sdk_name: PubNub PHP SDK
sdk_version: 9.0.0
---

> 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 PHP SDK

PubNub PHP SDK, use the latest version: 9.0.0

Install:

```bash
composer require pubnub/pubnub@9.0.0
```

[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)

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

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

```php
$pubnub->addChannelToChannelGroup()
    ->channels(string|array)
    ->channelGroup(string)
    ->sync();
```

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| channels | String|Array | Yes |  | The channels to add to the channel group. |
| channelGroup | String | Yes |  | The channel group to add the channels to. |

### Sample code

#### Add channels

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

```php
<?php

// Include Composer autoloader (adjust path if needed)
require_once 'vendor/autoload.php';

use PubNub\PNConfiguration;
use PubNub\PubNub;
use PubNub\Exceptions\PubNubServerException;

// snippet.setup
// Create configuration
$pnConfig = new PNConfiguration();
$pnConfig->setSubscribeKey(getenv("SUBSCRIBE_KEY") ?? "demo");
$pnConfig->setPublishKey(getenv("PUBLISH_KEY") ?? "demo");
$pnConfig->setUserId("php-channel-group-demo");

// Initialize PubNub instance
$pubnub = new PubNub($pnConfig);
// snippet.end

try {
    // Add channels to channel group
    $result = $pubnub->addChannelToChannelGroup()
        ->channels(["news", "sports"])
        ->channelGroup("my-group")
        ->sync();

    // Print success message
    echo "Channels added to group successfully!" . PHP_EOL;

    // Example of how to use this channel group for subscription
    echo PHP_EOL . "To subscribe to this channel group:" . PHP_EOL;
    echo '$pubnub->subscribe()->channelGroups(["my-group"])->execute();' . PHP_EOL;
} catch (PubNubServerException $exception) {
    // Handle errors
    echo "Error adding channels to group: " . $exception->getMessage() . PHP_EOL;

    if (method_exists($exception, 'getServerErrorMessage') && $exception->getServerErrorMessage()) {
        echo "Server Error: " . $exception->getServerErrorMessage() . PHP_EOL;
    }

    if (method_exists($exception, 'getStatusCode') && $exception->getStatusCode()) {
        echo "Status Code: " . $exception->getStatusCode() . PHP_EOL;
    }
} catch (Exception $exception) {
    // Handle general exceptions
    echo "Error: " . $exception->getMessage() . PHP_EOL;
}
```

### Rest response from server

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

## 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)

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

```php
$pubnub->listChannelsInChannelGroup()
    ->channelGroup(string)
    ->sync();
```

| Parameter | Description |
| --- | --- |
| `channelGroup` *Type: String | The channel group for which to list channels. |

### Sample code

#### List channels

```php
$result = $pubnub->listChannelsInChannelGroup()
    ->channelGroup("cg1")
    ->sync();
```

### Rest response from server

```json
{
    "status" : 200,
    "payload" : {
        "channels" : ["hi"],
        "group" : "abcd"
    },
    "service" : "channel-registry",
    "error" : False
}
```

## 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)

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

```php
$pubnub->removeChannelFromChannelGroup()
    ->channels(string|array)
    ->channelGroup(string)
    ->sync();
```

| Parameter | Description |
| --- | --- |
| `channels` *Type: String|Array | The channels to remove from the channel group. |
| `channelGroup` *Type: String | The channel group from which to remove the channels. |

### Sample code

#### Remove channels

```php
$pubnub->removeChannelFromChannelGroup()
    ->channels("son")
    ->channelGroup("family")
    ->sync();
```

### Rest response from server

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

## 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)

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

```php
$pubnub->removeChannelGroup()
    ->channelGroup(string)
    ->sync();
```

| Parameter | Description |
| --- | --- |
| `channelGroup` *Type: String | The channel group to remove. |

### Sample code

#### Delete channel group

```php
$pubnub->removeChannelGroup()
    ->channelGroup("family")
    ->sync();
```

### Rest response from server

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

## 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.