Channel Groups API for Angular2 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.
Add channels to a channel group
Maximum number of channels
You can add up to 200 channels to a channel group per API call.
This function adds channels to a channel group.
Method(s)
Use the following method in the Angular2 SDK:
Note
200 channels can be added to the channel group per API call.
1pubnub.channelGroups.addChannels({Array channels, String channelGroup},Function callback)
| Parameter | Description |
|---|---|
channels *Type: Array | The channels to add to the channel group. |
channelGroup *Type: String | The channel group to add the channels to. |
callbackType: Function | Executes on a successful/unsuccessful addChannels. |
Sample code
Add channels
1pubnub.channelGroups.addChannels(
2 {
3 channels: ['my_channel1', 'my_channel2'],
4 channelGroup: "my_channelGroup"
5 },
6 function(status) {
7 if (status.error) {
8 console.log("operation failed w/ status: ", status);
9 } else {
10 console.log("operation done!");
11 }
12 }
13);
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. 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 Angular2 SDK:
1pubnub.channelGroups.listChannels({String channelGroup},Function callback)
| Parameter | Description |
|---|---|
channelGroup *Type: String | The channel group for which to list channels. |
callbackType: Function | Executes on a successful/unsuccessful listChannels. |
Sample code
List channels
1// assuming an intialized Pubnub instance already exists
2pubnub.channelGroups.listChannels(
3 {
4 channelGroup: "my_channelGroup"
5 },
6 function (status, response) {
7 if (status.error) {
8 console.log("operation failed w/ error:", status);
9 return;
10 }
11 console.log("listing push channel for device");
12 response.channels.forEach( function (channel) {
13 console.log(channel);
14 });
15 }
show all 16 linesResponse
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. 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 Angular2 SDK:
1pubnub.channelGroups.removeChannels({Array channels, String channelGroup},Function callback)
| Parameter | Description |
|---|---|
channels *Type: Array | The channels to remove from the channel group. |
channelGroup *Type: String | The channel group from which to remove the channels. |
callbackType: Function | Executes on a successful/unsuccessful removeChannels. |
Sample code
Remove channels
1pubnub.channelGroups.removeChannels(
2 {
3 channels: ["my_channel2"],
4 channelGroup: "my_channelGroup"
5 },
6 function (status) {
7 if (status.error) {
8 console.log("operation failed w/ error:", status);
9 } else {
10 console.log("operation done!");
11 }
12 }
13);
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. 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 Angular2 SDK:
1pubnub.channelGroups.deleteGroup({String channelGroup},Function callback)
| Parameter | Description |
|---|---|
channelGroup *Type: String | The channel group to delete. |
callbackType: Function | Executes on a successful/unsuccessful deleteGroup. |
Sample code
Delete channel group
1pubnub.channelGroups.deleteGroup(
2 {
3 channelGroup: "my_channelGroup"
4 },
5 function (status) {
6 if (status.error) {
7 console.log("operation failed w/ error:", status);
8 } else {
9 console.log("operation done!");
10 }
11 }
12);
Response
1{
2 error: false,
3 operation: "PNRemoveGroupOperation",
4 statusCode: 200
5}