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 anEnvelope
object, which has two fields:Envelope.result
, whose type differs for each API, andEnvelope.status
of typePnStatus
.pubnub.publish() \
.channel("myChannel") \
.message("Hello from PubNub Python SDK") \
.sync() -
.pn_async(callback)
returnsNone
and passes the values ofEnvelope.result
andEnvelope.status
to a callback you must define beforehand.def my_callback_function(result, status):
print(f'TT: {result.timetoken}, status: {status.category.name}')
pubnub.publish() \
.channel("myChannel") \
.message("Hello from PubNub Python SDK") \
.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. Read the support page on enabling add-on features on your keys.
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.
pubnub.add_channel_to_channel_group() \
.channels(String|List|Tuple) \
.channel_group(String)
Parameter | Description |
---|---|
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
- Builder Pattern
- Named Arguments
import os
from pubnub.pnconfiguration import PNConfiguration
from pubnub.pubnub import PubNub
from pubnub.exceptions import PubNubException
def add_channels_to_group(pubnub: PubNub):
try:
pubnub.add_channel_to_channel_group() \
.channels(["ch1", "ch2"]) \
.channel_group("cg1") \
.sync()
print("Channels added to channel group successfully.")
except PubNubException as e:
print(f"Error: {e}")
show all 33 linesimport os
from pubnub.pnconfiguration import PNConfiguration
from pubnub.pubnub import PubNub
from pubnub.exceptions import PubNubException
def add_channels_to_group(pubnub: PubNub):
try:
pubnub.add_channel_to_channel_group(
channels=["ch1", "ch2"],
channel_group="cg1"
).sync()
print("Channels added to channel group successfully.")
except PubNubException as e:
print(f"Error: {e}")
show all 34 linesReturns
The add_channel_to_channel_group()
operation returns an Envelope
which contains the following fields:
Field | Type | Description |
---|---|---|
result | PNChannelGroupsAddChannelResult | A detailed object containing the result of the operation. |
status | PNStatus | A status object with additional information. |
PNChannelGroupsAddChannelResult
Channel 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. Read the support page on enabling add-on features on your keys.
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:
pubnub.list_channels_in_channel_group() \
.channel_group(String)
Parameter | Description |
---|---|
channel_group *Type: String | The channel group to fetch channels. |
Sample code
List channels
- Builder Pattern
- Named Arguments
result = pubnub.list_channels_in_channel_group() \
.channel_group("cg1") \
.sync()
result = pubnub.list_channels_in_channel_group(channel_group="cg1").sync()
Returns
The list_channels_in_channel_group()
operation returns an Envelope
which contains the following fields:
Field | Type | Description |
---|---|---|
result | PNChannelGroupsListResult | A detailed object containing the result of the operation. |
status | PNStatus | A status object with additional information. |
PNChannelGroupsListResult
Field | Type | Description |
---|---|---|
channels | Dictionary | A 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. Read the support page on enabling add-on features on your keys.
This function removes channels from a channel group.
Method(s)
Removing Channels
is accomplished by using the following method(s) in the Python SDK:
pubnub.remove_channel_from_channel_group() \
.channels(String|List|Tuple) \
.channel_group(String)
Parameter | Description |
---|---|
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
- Builder Pattern
- Named Arguments
pubnub.remove_channel_from_channel_group() \
.channels(["son", "daughter"]) \
.channel_group("channel_group") \
.sync()
pubnub.remove_channel_from_channel_group(channels=["ch1", "ch2"], channel_group="cg1").sync()
Returns
The remove_channel_from_channel_group()
operation returns an Envelope
which contains the following fields:
Field | Type | Description |
---|---|---|
result | PNChannelGroupsRemoveChannelResult | A detailed object containing the result of the operation. |
status | PNStatus | A status object with additional information. |
PNChannelGroupsRemoveChannelResult
Channel 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. Read the support page on enabling add-on features on your keys.
This function deletes a channel group.
Method(s)
Deleting Channel Group
is accomplished by using the following method(s) in the Python SDK:
pubnub.remove_channel_group(channel_group)
Parameter | Description |
---|---|
channel_group *Type: String | The channel group to remove. |
Sample code
Delete channel group
- Builder Pattern
- Named Arguments
pubnub.remove_channel_group() \
.channel_group("cg1") \
.sync()
pubnub.remove_channel_group(channel_group="cg1").sync()
Returns
The remove_channel_group()
operation returns an Envelope
which contains the following fields:
Field | Type | Description |
---|---|---|
result | PNChannelGroupsRemoveGroupResult | A detailed object containing the result of the operation. |
status | PNStatus | A status object with additional information. |
PNChannelGroupsRemoveGroupResult
Group successfully removed