On this page

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

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 Python-Asyncio SDK:

Maximum number of channels

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

1pubnub.add_channel_to_channel_group() \
2 .channels(String|List|Tuple) \
3 .channel_group(String)
* required
ParameterDescription
channels *
Type: String | List | Tuple
The channels to add to the channel group.
channel_group *
Type: String
The channel group to add the channels to.

Sample code

Add channels

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.
1import asyncio
2import os
3from pubnub.pnconfiguration import PNConfiguration
4from pubnub.pubnub_asyncio import PubNubAsyncio
5from pubnub.exceptions import PubNubException
6
7
8async def add_channels_to_group(pubnub: PubNubAsyncio):
9 try:
10 # Add Channels to a Channel Group
11 envelope = await pubnub.add_channel_to_channel_group() \
12 .channels(["ch1", "ch2"]) \
13 .channel_group("cg1") \
14 .future()
15
show all 40 lines

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 Python-Asyncio SDK:

1pubnub.list_channels_in_channel_group() \
2 .channel_group(String)
* required
ParameterDescription
channel_group *
Type: String
The channel group for which to list channels.

Sample code

List channels

1envelope = pubnub.list_channels_in_channel_group().\
2 channel_group("cg1").future()

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 Python-Asyncio SDK:

1pubnub.remove_channel_from_channel_group() \
2 .channels(String|List|Tuple) \
3 .channel_group(String)
* required
ParameterDescription
channels *
Type: String | List | Tuple
The channels to remove from the channel group.
channel_group *
Type: String
The channel group from which to remove the channels.

Sample code

Remove channels

1envelope = await pubnub.remove_channel_from_channel_group().\
2 channels(["son", "daughter"]).\
3 channel_group("channel_group").\
4 future()

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 Python-Asyncio SDK:

1pubnub.remove_channel_group() \
2 .channel_group(String)
* required
ParameterDescription
channel_group *
Type: String
The channel group to delete.

Sample code

Delete channel group

1envelope = await pubnub.remove_channel_group() \
2 .channel_group("cg1").future()
Last updated on