Channel Groups API for PubNub 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
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 accomplished by using the following method(s) in the Python SDK:
Maximum number of channels
200 channels
can be added to the channel group
per API call.
pubnub.add_channel_to_channel_group() \
.channels(String|List|Tuple) \
.channel_group(String)
Parameter | Type | Required | Description |
---|---|---|---|
channels | String | List | Tuple | Yes | The channel to add to the channel_group . |
channel_group | String | Yes | The channel_group to add the channels to. |
Basic Usage
Add Channels
- Builder Pattern
- Named Arguments
pubnub.add_channel_to_channel_group() \
.channels(["ch1", "ch2"]) \
.channel_group("cg1") \
.sync()
pubnub.add_channel_to_channel_group(channels=["ch1", "ch2"], channel_group="cg1").sync()
Returns
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
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 Python SDK:
pubnub.list_channels_in_channel_group() \
.channel_group(String)
Parameter | Type | Required | Description |
---|---|---|---|
channel_group | String | Yes | The channel group to fetch channels. |
Basic Usage
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
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 Python SDK:
pubnub.remove_channel_from_channel_group() \
.channels(String|List|Tuple) \
.channel_group(String)
Parameter | Type | Required | Description |
---|---|---|---|
channels | String | List | Tuple | Yes | The channels to remove from the channel group. |
channel_group | String | Yes | Specifies channel_group to remove the channels from. |
Basic Usage
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 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 Python SDK:
pubnub.remove_channel_group(channel_group)
Parameter | Type | Required | Description |
---|---|---|---|
channel_group | String | Yes | The channel group to remove. |
Basic Usage
Delete Channel Group
- Builder Pattern
- Named Arguments
pubnub.remove_channel_group() \
.channel_group("cg1") \
.sync()
pubnub.remove_channel_group(channel_group="cg1").sync()