Posix C++ Channel Groups API Reference for Realtime Apps
Channel Groups allows PubNub developers to bundle thousands of channels into a group that can be identified by name. These Channel Groups can then be subscribed to, receiving data from the many backend-channels the channel group contains.
Learn more about our Channel Groups here.
Adding Channels
Requires Stream Controller add-on Requires that the Stream Controller add-on is enabled for your key. See this page on enabling add-on features on your keys:
https://support.pubnub.com/hc/en-us/articles/360051974791-How-do-I-enable-add-on-features-for-my-keys-
Description
This function adds a channel to a channel group.
Method(s)
Adding Channels
is accomplished by using the following method(s) in the Posix C++ SDK:
add_channel_to_group(std::vector<std::string> const &channel, std::vector<std::string> const &channel_group)
Parameter Type Required Description channel
std::vector <std::string>const & Yes The channel(s)
vector to add to the channel group.channel_group
std::vector<std::string>const & Yes The channel_group
to add to thechannel(s)
.A future result
(pubnub::futres)
. If transaction is successful, the response will be available viaget_channel()
function as one channel, a JSON object.add_channel_to_group (std::string const &channel, std::string const &channel_group)
Parameter Type Required Description channel
std::string const & Yes The channel(s)
to add to thechannel group
.channel_group
std::string const & Yes The channel_group
to add thechannel(s)
to.A future result
(pubnub::futres)
. If transaction is successful, the response will be available viaget_channel()
function as one channel, a JSON object.
Basic Usage
const std::string channel_group("family");
//Sync
static void add_channel(pubnub::context &pn) {
enum pubnub_res res;
try {
res = pn.add_channel_to_group("wife", channel_group).await();
if (PNR_OK == res) {
std::cout << pn.get_channel() << std::endl;
} else {
std::cout << "Failed with code " << res << std::endl;
}
} catch (std::exception &ex) {
std::cout << "Exception: " << ex.what() << std::endl;
}
}
//Lambdas
static void add_channel(pubnub::context &ipn) {
ipn.add_channel_to_group("wife", channel_group)
.then([=](pubnub::context &pn, pubnub_res res) {
if (PNR_OK == res) {
std::cout << pn.get_channel() << std::endl;
} else {
std::cout << "Failed with code " << res << std::endl;
}
});
}
//Functions
static void on_added(pubnub::context &pn, pubnub_res res) {
if (PNR_OK == res) {
std::cout << pn.get_channel() << std::endl;
} else {
std::cout << "Failed with code " << res << std::endl;
}
}
static void add_channel(pubnub::context &ipn) {
ipn.add_channel_to_group("wife", channel_group).then(on_added);
}
Rest Response from Server
{
"service" : "channel-registry",
"status" : 200,
"error" : false,
"message" : "OK"
}
Listing Channels
Requires Stream Controller add-on Requires that the Stream Controller add-on is enabled for your key. See this page on enabling add-on features on your keys:
https://support.pubnub.com/hc/en-us/articles/360051974791-How-do-I-enable-add-on-features-for-my-keys-
Description
This function lists all the channels of the channel group.
Method(s)
Listing Channels
is accomplished by using the following method(s) in the Posix C++ SDK:
list_channel_group (std::string const &channel_group)
Parameter Type Required Description channel_group
std::string const & Yes channel_group
to fetch the channels of.
Basic Usage
const std::string channel_group("family");
//Sync
static void list_channels(pubnub::context &pn) {
enum pubnub_res res;
try {
res = pn.list_channel_group(channel_group).await();
if (PNR_OK == res) {
std::cout << pn.get_channel() << std::endl;
} else {
std::cout << "Failed with code " << res << std::endl;
}
} catch (std::exception &ex) {
std::cout << "Exception: " << ex.what() << std::endl;
}
}
//Lambdas
static void list_channels(pubnub::context &ipn) {
ipn.list_channel_group(channel_group)
.then([=](pubnub::context &pn, pubnub_res res) {
if (PNR_OK == res) {
std::cout << pn.get_channel() << std::endl;
} else {
std::cout << "Failed with code " << res << std::endl;
}
});
}
//Functions
static void on_list_received(pubnub::context &pn, pubnub_res res) {
if (PNR_OK == res) {
std::cout << pn.get_channel() << std::endl;
} else {
std::cout << "Failed with code " << res << std::endl;
}
}
static void list_channels(pubnub::context &ipn) {
ipn.list_channel_group(channel_group).then(on_list_received);
}
Rest Response from Server
{
"status" : 200,
"payload" : {
"channels" : ["hi"],
"group" : "abcd"
},
"service" : "channel-registry",
"error" : False
}
Removing Channels
Requires Stream Controller add-on Requires that the Stream Controller add-on is enabled for your key. See this page on enabling add-on features on your keys:
https://support.pubnub.com/hc/en-us/articles/360051974791-How-do-I-enable-add-on-features-for-my-keys-
Description
This function removes the channels from the channel group.
Method(s)
Removing Channels
is accomplished by using the following method(s) in the Posix C++ SDK:
remove_channel_from_group (std::string const &channel, std::string const &channel_group)
Parameter Type Required Description channel_group
std::string const & Yes Specifies channel_group
to remove the channels from.channel
std::string const & Yes The channel
to remove from thechannel group
.
Basic Usage
Removing channels :
const std::string channel_group("family");
//Sync
static void remove_channel(pubnub::context &pn) {
enum pubnub_res res;
try {
res = pn.remove_channel_from_group("son", channel_group).await();
if (PNR_OK == res) {
std::cout << pn.get_channel() << std::endl;
} else {
std::cout << "Failed with code " << res << std::endl;
}
} catch (std::exception &ex) {
std::cout << "Exception: " << ex.what() << std::endl;
}
}
//Lambdas
static void remove_channel(pubnub::context &ipn) {
ipn.remove_channel_from_group("son", channel_group)
.then([=](pubnub::context &pn, pubnub_res res) {
if (PNR_OK == res) {
std::cout << pn.get_channel() << std::endl;
} else {
std::cout << "Failed with code " << res << std::endl;
}
});
}
//Functions
static void on_removed(pubnub::context &pn, pubnub_res res) {
if (PNR_OK == res) {
std::cout << pn.get_channel() << std::endl;
} else {
std::cout << "Failed with code " << res << std::endl;
}
}
static void remove_channel(pubnub::context &ipn) {
ipn.remove_channel_from_group("son", channel_group).then(on_removed);
}
Rest Response from Server
{
"status" : 200,
"message" : "OK",
"service" : "channel-registry",
"error" : False
}
Deleting Channel Group
Requires Stream Controller add-on Requires that the Stream Controller add-on is enabled for your key. See this page on enabling add-on features on your keys:
https://support.pubnub.com/hc/en-us/articles/360051974791-How-do-I-enable-add-on-features-for-my-keys-
Description
This function removes the channel group.
Method(s)
Deleting Channel Group
is accomplished by using the following method(s) in the Posix C++ SDK:
remove_channel_group (std::string const &channel_group)
Parameter Type Required Description channel_group
std::string const & Yes Specifies channel_group
to remove.
Basic Usage
Deleting Channel Group :
const std::string channel_group("family");
//Sync
static void remove_group(pubnub::context &pn) {
enum pubnub_res res;
try {
res = pn.remove_channel_group(channel_group).await();
if (PNR_OK == res) {
std::cout << pn.get_channel() << std::endl;
} else {
std::cout << "Failed with code " << res << std::endl;
}
} catch (std::exception &ex) {
std::cout << "Exception: " << ex.what() << std::endl;
}
}
//Lambdas
static void remove_group(pubnub::context &ipn) {
ipn.remove_channel_group(channel_group)
.then([=](pubnub::context &pn, pubnub_res res) {
if (PNR_OK == res) {
std::cout << pn.get_channel() << std::endl;
} else {
std::cout << "Failed with code " << res << std::endl;
}
});
}
//Functions
static void on_group_removed(pubnub::context &pn, pubnub_res res) {
if (PNR_OK == res) {
std::cout << pn.get_channel() << std::endl;
} else {
std::cout << "Failed with code " << res << std::endl;
}
}
static void remove_group(pubnub::context &ipn) {
ipn.remove_channel_group(channel_group).then(on_group_removed);
}
Rest Response from Server
{
"status" : 200,
"message" : "OK",
"service" : "channel-registry",
"error" : False
}