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. Read the support page 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:

Maximum number of channels

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

pubnub.channelGroups.addChannels({
channels: Array<string>,
channelGroup: string
})
* 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



Response

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

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. Read the support page 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:

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

Sample code

List channels


Response

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

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

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. Read the support page 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:

pubnub.channelGroups.removeChannels({
channels: Array<string>,
channelGroup: string
})
* 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


Response

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

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. Read the support page on enabling add-on features on your keys.

This function deletes a channel group.

Method(s)

Use the following method in the JavaScript SDK:

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

Sample code

Delete channel group


Response

{
error: false,
operation: "PNRemoveGroupOperation",
statusCode: 200
}
Last updated on