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

Request execution and return values

You can decide whether to perform the Python SDK operations synchronously or asynchronously.

  • .sync() returns an Envelope object, which has two fields: Envelope.result, whose type differs for each API, and Envelope.status of type PnStatus.

    1pubnub.publish() \
    2 .channel("myChannel") \
    3 .message("Hello from PubNub Python SDK") \
    4 .sync()
  • .pn_async(callback) returns None and passes the values of Envelope.result and Envelope.status to a callback you must define beforehand.

    1def my_callback_function(result, status):
    2 print(f'TT: {result.timetoken}, status: {status.category.name}')
    3
    4pubnub.publish() \
    5 .channel("myChannel") \
    6 .message("Hello from PubNub Python SDK") \
    7 .pn_async(my_callback_function)

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)​

Adding Channels is accomplished by using the following method(s) in the Python 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 channel 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 os
2from pubnub.pnconfiguration import PNConfiguration
3from pubnub.pubnub import PubNub
4from pubnub.exceptions import PubNubException
5
6
7def add_channels_to_group(pubnub: PubNub):
8 try:
9 pubnub.add_channel_to_channel_group() \
10 .channels(["ch1", "ch2"]) \
11 .channel_group("cg1") \
12 .sync()
13 print("Channels added to channel group successfully.")
14 except PubNubException as e:
15 print(f"Error: {e}")
show all 33 lines

Returns​

The add_channel_to_channel_group() operation returns an Envelope which contains the following fields:

FieldTypeDescription
resultPNChannelGroupsAddChannelResultA detailed object containing the result of the operation.
statusPNStatusA status object with additional information.

PNChannelGroupsAddChannelResult​

1Channel successfully added

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)​

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

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

Sample code​

List channels​

1result = pubnub.list_channels_in_channel_group() \
2 .channel_group("cg1") \
3 .sync()

Returns​

The list_channels_in_channel_group() operation returns an Envelope which contains the following fields:

FieldTypeDescription
resultPNChannelGroupsListResultA detailed object containing the result of the operation.
statusPNStatusA status object with additional information.

PNChannelGroupsListResult​

FieldTypeDescription
channelsDictionaryA list of channels in a channel group.

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)​

Removing Channels is accomplished by using the following method(s) in the Python 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
Specifies channel_group to remove the channels from.

Sample code​

Remove channels​

1pubnub.remove_channel_from_channel_group() \
2 .channels(["son", "daughter"]) \
3 .channel_group("channel_group") \
4 .sync()

Returns​

The remove_channel_from_channel_group() operation returns an Envelope which contains the following fields:

FieldTypeDescription
resultPNChannelGroupsRemoveChannelResultA detailed object containing the result of the operation.
statusPNStatusA status object with additional information.

PNChannelGroupsRemoveChannelResult​

1Channel successfully removed

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)​

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

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

Sample code​

Delete channel group​

1pubnub.remove_channel_group() \
2 .channel_group("cg1") \
3 .sync()

Returns​

The remove_channel_group() operation returns an Envelope which contains the following fields:

FieldTypeDescription
resultPNChannelGroupsRemoveGroupResultA detailed object containing the result of the operation.
statusPNStatusA status object with additional information.

PNChannelGroupsRemoveGroupResult​

1Group successfully removed

Was this page useful?

Last updated on