Channel Groups API for C# 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

We recommend using try and catch statements when working with the C# SDK.

If there's an issue with the provided API parameter values, like missing a required parameter, the SDK throws an exception. However, if there is a server-side API execution issue or a network problem, the error details are contained within the status.

try
{
PNResult<PNPublishResult> publishResponse = await pubnub.Publish()
.Message("Why do Java developers wear glasses? Because they can't C#.")
.Channel("my_channel")
.ExecuteAsync();

PNStatus status = publishResponse.Status;

Console.WriteLine("Server status code : " + status.StatusCode.ToString());
}
catch (Exception ex)
{
Console.WriteLine($"Request can't be executed due to error: {ex.Message}");
}

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 C# SDK:

Maximum number of channels

200 channels can be added to the channel group per API call.

pubnub.AddChannelsToChannelGroup()
.ChannelGroup(string)
.Channels(Array)
.QueryParam(Dictionary<string,object>)
* required
ParameterDescription
ChannelGroup *
Type: string
The ChannelGroup to add the channels to.
Channels *
Type: Array
The Channels to add to the channel group.
QueryParam
Type: Dictionary<string, object>
Dictionary object to pass name/value pairs as query string params with PubNub URL request for debug purpose.
Async
Type: PNCallback
PNCallback of type PNChannelGroupsAddChannelResult.
Execute *
Type: PNCallback
PNCallback of type PNChannelGroupsAddChannelResult.
ExecuteAsync
Type: None
Returns PNResult<PNChannelGroupsAddChannelResult>.

Basic Usage

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.

Add Channels

using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using PubnubApi;

class Program
{
static async Task Main(string[] args)
{
PNConfiguration pnConfiguration = new PNConfiguration(new UserId("myUniqueUserId"))
{
SubscribeKey = "demo",
PublishKey = "demo"
};

show all 44 lines

Returns

The AddChannelsToChannelGroup() operation returns a PNResult<PNChannelGroupsAddChannelResult> which contains the following properties:

Property NameTypeDescription
Result
PNChannelGroupsAddChannelResult
Returns a PNChannelGroupsAddChannelResult object.
Status
PNStatus
Returns a PNStatus object.

PNChannelGroupsAddChannelResult contains the following properties:

Property NameTypeDescription
PNChannelGroupsAddChannelResult
Object
Returns empty object.
PNStatus
Object
Returns status of request if error occurred or not.

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 C# SDK:

pubnub.ListChannelsForChannelGroup()
.ChannelGroup(string)
.QueryParam(Dictionary<string,object>)
* required
ParameterDescription
ChannelGroup *
Type: string
Channel group to fetch the channels.
QueryParam
Type: Dictionary<string, object>
Dictionary object to pass name/value pairs as query string params with PubNub URL request for debug purpose.
Async
Type: PNCallback
PNCallback of type PNChannelGroupsAllChannelsResult.
Execute *
Type: PNCallback
PNCallback of type PNChannelGroupsAllChannelsResult.
ExecuteAsync
Type: None
Returns PNResult<PNChannelGroupsAllChannelsResult>.

Basic Usage

List Channels

PNResult<PNChannelGroupsAllChannelsResult> cgListChResponse = await pubnub.ListChannelsForChannelGroup()
.ChannelGroup("cg1")
.ExecuteAsync();

Returns

The ListChannelsForChannelGroup() operation returns a PNChannelGroupsAllChannelsResult which contains the following properties:

Property NameTypeDescription
Result
PNChannelGroupsAllChannelsResult
Returns a PNChannelGroupsAllChannelsResult object.
Status
PNStatus
Returns a PNStatus object.

PNChannelGroupsAllChannelsResult contains the following property:

Property NameTypeDescription
Channels
List<string>
List of channels of 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 C# SDK:

pubnub.RemoveChannelsFromChannelGroup()
.ChannelGroup(string)
.Channels(Array)
.QueryParam(Dictionary<string,object>)
* required
ParameterDescription
ChannelGroup *
Type: string
Specifies ChannelGroup to remove the channels from.
Channels *
Type: Array
The Channels to remove from the channel group.
QueryParam
Type: Dictionary<string, object>
Dictionary object to pass name/value pairs as query string params with PubNub URL request for debug purpose.
Async
Type: PNCallback
PNCallback of type PNChannelGroupsRemoveChannelResult.
Execute *
Type: PNCallback
PNCallback of type PNChannelGroupsRemoveChannelResult.
ExecuteAsync
Type: None
Returns PNResult<PNChannelGroupsRemoveChannelResult>.

Basic Usage

Remove channels

PNResult<PNChannelGroupsRemoveChannelResult> rmChFromCgResponse = await pubnub.RemoveChannelsFromChannelGroup()
.ChannelGroup("family")
.Channels(new string[] {
"son"
})
.ExecuteAsync();

Returns

The RemoveChannelsFromChannelGroup() operation returns a PNChannelGroupsAddChannelResult which contains the following properties:

Property NameTypeDescription
Result
PNChannelGroupsRemoveChannelResult
Returns a PNChannelGroupsRemoveChannelResult object.
Status
PNStatus
Returns a PNStatus object.

PNChannelGroupsRemoveChannelResult contains the following property:

Property NameTypeDescription
PNChannelGroupsRemoveChannelResult
Object
Returns empty object.

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 C# SDK:

pubnub.DeleteChannelGroup()
.ChannelGroup(string)
.QueryParam(Dictionary<string,object>)
* required
ParameterDescription
ChannelGroup *
Type: string
Specifies ChannelGroup to remove.
QueryParam
Type: Dictionary<string, object>
Dictionary object to pass name/value pairs as query string params with PubNub URL request for debug purpose.
Async
Type: PNCallback
PNCallback of type PNChannelGroupsDeleteGroupResult.
Execute *
Type: PNCallback
PNCallback of type PNChannelGroupsDeleteGroupResult.
ExecuteAsync
Type: None
Returns PNResult<PNChannelGroupsAllChannelsResult>.

Basic Usage

Delete Channel Group

PNResult<PNChannelGroupsDeleteGroupResult> delCgResponse = await pubnub.DeleteChannelGroup()
.ChannelGroup("family")
.ExecuteAsync();

Response

{
"status" : 200,
"message" : "OK",
"service" : "channel-registry",
"error" : False
}
Last updated on