Channel Groups API for JavaScript SDK

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.

Supported and recommended asynchronous patterns

PubNub supports Callbacks, Promises, and Async/Await 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 syntax to your code.

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. See Enable the features your app calls.

This function adds channels to a channel group.

Method(s)​

Use the following method in the JavaScript SDK:

Maximum number of channels

You can add up to 200 channels to a channel group per API call.

1pubnub.channelGroups.addChannels({
2 channels: Array<string>,
3 channelGroup: string
4})
* required
ParameterDescription
channels *
Type: Array<string>
The channels to add to the channel group.
channelGroup *
Type: string
The channel group to add the channels to.

Sample code​

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​

1

1

Response​

1{
2 error: false,
3 operation: "PNAddChannelsToGroupOperation",
4 statusCode: 200
5}

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. See Enable the features your app calls.

This function lists all channels in a channel group.

Method(s)​

Use the following method in the JavaScript SDK:

1pubnub.channelGroups.listChannels({
2 channelGroup: string
3})
* required
ParameterDescription
channelGroup *
Type: string
The channel group for which to list channels.

Sample code​

List channels​

1

Response​

1// Example of Status
2{
3 error: false,
4 operation: "PNChannelsForGroupOperation",
5 statusCode: 200
6}
7
8// Example of Response
9{
10 channels: ["ch1", "ch2"]
11}

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. See Enable the features your app calls.

This function removes channels from a channel group.

Method(s)​

Use the following method in the JavaScript SDK:

1pubnub.channelGroups.removeChannels({
2 channels: Array<string>,
3 channelGroup: string
4})
* required
ParameterDescription
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​

1

Response​

1{
2 error: false,
3 operation: "PNRemoveChannelsFromGroupOperation",
4 statusCode: 200
5}

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. See Enable the features your app calls.

This function deletes a channel group.

Method(s)​

Use the following method in the JavaScript SDK:

1pubnub.channelGroups.deleteGroup({
2 channelGroup: string
3})
* required
ParameterDescription
channelGroup *
Type: string
The channel group to delete.

Sample code​

Delete channel group​

1

Response​

1{
2 error: false,
3 operation: "PNRemoveGroupOperation",
4 statusCode: 200
5}

Was this page useful?

Last updated on