---
source_url: https://www.pubnub.com/docs/sdks/javascript/api-reference/channel-groups
title: Channel Groups API for JavaScript SDK
updated_at: 2026-06-19T11:37:30.989Z
sdk_name: PubNub JavaScript SDK
sdk_version: 11.0.2
---

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

PubNub JavaScript SDK, use the latest version: 11.0.2

Install:

```bash
npm install pubnub@11.0.2
```

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

:::note Supported and recommended asynchronous patterns
PubNub supports [Callbacks, Promises, and Async/Await](https://javascript.info/async) for asynchronous JS operations. The recommended pattern is Async/Await and all sample requests in this document are based on it. This pattern returns a status only on detecting an error. To receive the error status, you must add the [try...catch](https://javascript.info/try-catch) syntax to your code.
:::

## 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 JavaScript SDK:

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

```javascript
pubnub.channelGroups.addChannels({
    channels: Array<string>,
    channelGroup: string
})
```

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

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

```javascript
import PubNub from 'pubnub';
// Initialize PubNub with your keys
const pubnub = new PubNub({
  publishKey: 'YOUR_PUBLISH_KEY',
  subscribeKey: 'YOUR_SUBSCRIBE_KEY',
  userId: 'YOUR_USER_ID',
});
```

```javascript
try {
  const response = await pubnub.channelGroups.addChannels({
    channels: ['ch1', 'ch2'],
    channelGroup: 'myChannelGroup',
  });
  console.log('addChannels to Group response:', response);
} catch (error) {
  console.error(
    `Add channels to group error: ${error}.${
      (error as PubNubError).status ? ` Additional information: ${(error as PubNubError).status}` : ''
    }`,
  );
}
```

### Response

```javascript
{
    error: false,
    operation: "PNAddChannelsToGroupOperation",
    statusCode: 200
}
```

## 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 JavaScript SDK:

```javascript
pubnub.channelGroups.listChannels({
    channelGroup: string
})
```

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

### Sample code

#### List channels

```javascript
// assuming an intialized PubNub instance already exists
// to get some data in response, first add some channels to the group using addChannels() method.
try {
  const response = await pubnub.channelGroups.listChannels({
    channelGroup: 'myChannelGroup',
  });
  console.log('Listing push channels for the device:', response);
  response.channels.forEach((channel: string) => {
    console.log(channel);
  });
} catch (error) {
  console.error(
    `List channels of group error: ${error}.${
      (error as PubNubError).status ? ` Additional information: ${(error as PubNubError).status}` : ''
    }`,
  );
}
```

### Response

```javascript
// Example of Status
{
    error: false,
    operation: "PNChannelsForGroupOperation",
    statusCode: 200
}

// Example of Response
{
    channels: ["ch1", "ch2"]
}
```

## 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 JavaScript SDK:

```javascript
pubnub.channelGroups.removeChannels({
    channels: Array<string>,
    channelGroup: string
})
```

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

### Sample code

#### Remove channels

```javascript
// assuming an initialized PubNub instance already exists
// and channel which is going to be removed from the group is aredaly added to the group to observe the removal
try {
  const response = await pubnub.channelGroups.removeChannels({
    channels: ['son'],
    channelGroup: 'family',
  });
  console.log('removeChannels from group response:', response);
} catch (error) {
  console.error(
    `Remove channels from group error: ${error}.${
      (error as PubNubError).status ? ` Additional information: ${(error as PubNubError).status}` : ''
    }`,
  );
}
```

### Response

```javascript
{
    error: false,
    operation: "PNRemoveChannelsFromGroupOperation",
    statusCode: 200
}
```

## 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 JavaScript SDK:

```javascript
pubnub.channelGroups.deleteGroup({
    channelGroup: string
})
```

| Parameter | Description |
| --- | --- |
| `channelGroup` *Type: string | The channel group to delete. |

### Sample code

#### Delete channel group

```javascript
// assuming an initialized PubNub instance already exists
// and channel group which is getting deleted already exist to see the deletion effect.
try {
  const response = await pubnub.channelGroups.deleteGroup({
    channelGroup: 'family',
  });
  console.log('deleteChannelGroup response:', response);
} catch (error) {
  console.error(
    `Delete channel group error: ${error}.${
      (error as PubNubError).status ? ` Additional information: ${(error as PubNubError).status}` : ''
    }`,
  );
}
```

### Response

```javascript
{
    error: false,
    operation: "PNRemoveGroupOperation",
    statusCode: 200
}
```

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