Channel Groups API for PubNub 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

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 adds a channel to a channel group.

Method(s)

Adding Channels is done by using the following method(s) in the JavaScript SDK:

Maximum number of channels

200 channels can be added to the channel group per API call.

pubnub.channelGroups.addChannels({
channels: Array<string>,
channelGroup: string
})
ParameterTypeRequiredDescription
Operation ArgumentsHashYesA hash of arguments.
channelsArray<string>YesThe channel to add to the channel group.
channelGroupstringYesThe channelGroup to add the channels to.

Basic Usage

Add Channels

try {
const result = await pubnub.channelGroups.addChannels({
channels: ["ch1", "ch2"],
channelGroup: "myChannelGroup",
});
console.log("operation done!");
result.channels.forEach(function (channel) {
console.log(channel);
});
} catch (status) {
console.log("operation failed w/ error:", status);
}

Response

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

List Channels

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 the channels of the channel group.

Method(s)

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

pubnub.channelGroups.listChannels({
channelGroup: string
})
ParameterTypeRequiredDescription
Operation ArgumentsHashYesA hash of arguments.
channelGroupstringYesChannel Group to list the channel(s) of.

Basic Usage

List Channels

// assuming an intialized PubNub instance already exists
try {
const result = await pubnub.channelGroups.listChannels({
channelGroup: "myChannelGroup",
});
console.log("Listing push channels for the device");
result.channels.forEach(function (channel) {
console.log(channel);
});
} catch (status) {
console.log("Operation failed w/ error:", status);
}

Response

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

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

Remove Channels

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 the channels from the channel group.

Method(s)

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

pubnub.channelGroups.removeChannels({
channels: Array<string>,
channelGroup: string
})
ParameterTypeRequiredDescription
Operation ArgumentsHashYesA hash of arguments.
channelsArray<string>YesThe channel to remove from the channel group.
channelGroupstringYesThe channelGroup to remove the channels from.

Basic Usage

Remove channels

// assuming an initialized PubNub instance already exists
try {
const result = await pubnub.channelGroups.removeChannels({
channels: ["son"],
channelGroup: "family",
});
console.log("operation done!");
result.channels.forEach(function (channel) {
console.log(channel);
});
} catch (status) {
console.log("operation failed w/ error:", status);
}

Response

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

Delete 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 the channel group.

Method(s)

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

pubnub.channelGroups.deleteGroup({
channelGroup: string
})
ParameterTypeRequiredDescription
Operation ArgumentsHashYesA hash of arguments.
channelGroupstringYesThe channelGroup to remove.

Basic Usage

Delete Channel Group

try {
const result = await pubnub.channelGroups.deleteGroup({
channelGroup: "family",
});
console.log("operation done!");
result.channels.forEach(function (channel) {
console.log(channel);
});
} catch (status) {
console.log("operation failed w/ error:", status);
}

Response

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