POSIX C++ (legacy) API & SDK Docs 7.3.6
Get code: source
SDK version
Always use the latest SDK version to have access to the newest features and avoid security vulnerabilities, bugs, and performance issues.
New SDK available
The POSIX C++ SDK is built on C-Core (legacy), which is still supported. If you are starting a new project, use the new C SDK.
Hello World
We recommend you use the Makefiles as a starting point in your own projects (whether they are based on Make or some other build tool / system or IDE).
Makefiles
-
The Makefile to build for POSIX C++ is available at posix.mk. Please take a look at README for general info about the repo layout & contents.
-
The Makefile for Posix C++ with SSL/TLS (via OpenSSL) is available at posix_openssl.mk in the repo. See /openssl/README.md for info on how to build with OpenSSL on POSIX C++ and other OpenSSL related data.
Including the header
To include the PubNub POSIX C++ headers into your app, you must include pubnub.hpp:
1#include "pubnub.hpp"
2#include <iostream>
Once the header has been included, its easy to make calls to the PubNub service via the POSIX C++ API. The PubNub POSIX C++ API is flexible, supporting many different calling patterns. As such, before we dive in to actual coding samples, its important to go over the different types of calling patterns provided by the PubNub POSIX C++ client's futures support.
C++ calling patterns: futures
pubnub::futres
The pubnub::futres class name is a portmanteau, blending C++ future with enum pubnub_res. It was purposely not named pubnub::future to avoid namespacing conflicts.
Both POSIX and Windows C++ SDKs use the pubnub::futres class, based on the std::future class (introduced in C++ 11) which provides a way to track the outcome of an operation asynchronously. Every operation that the SDK initiates with PubNub returns a futures object. This object allows to track the progress and completion of the operation via the status identifiers.
Using futures synchronously
There are several ways to use futures synchronously:
- Single
await()orget() - Separated
start_await()andend_await() - Your own
last_result()loop
Single await() or get()
The pubnub::futres::await() method, similar to the pubnub_await() function from the C sync interface, is used to await the outcome of a transaction/operation. Since its function is the same as std::future<>::get(), we provide a synonym, and you can call pubnub::futres::get() with the same effect.
1pubnub::context pb("demo", "demo");
2pubnub::futres f = pb.publish("hello_world", "\"Here I am!\"");
3if (PNR_OK == f.await()) {
4 std::cout << "Published OK" << std::endl;
5}
6pubnub::futres f = pb.publish("hello_world", "\"Stuck in the middle\"");
7if (PNR_OK == f.get()) {
8 std::cout << "Published OK (again)" << std::endl;
9}
Separated start_await() and end_await()
You don't have to await at once. You can start the await, and finish later. This is useful if you are working with more than one context, so you start an operation on one context, then start the await on that context, proceed to start an operation on another context and then end the started await.
1pubnub::context pb("demo", "demo");
2pubnub::context pbsub("demo", "demo");
3pbsub.subscribe("hello_world").await(); // connect
4pubnub::futres fsub = pbsub.subscribe("hello_world");
5fsub.start_await();
6pubnub::futres f = pb.publish("hello_world", "\"with you!\"");
7if (PNR_OK == f.await()) {
8 std::cout << "Published OK" << std::endl;
9}
10if (PNR_OK == fsub.end_await()) {
11 std::cout << "Received messages:" << std::endl;
12 for (auto &&msg : pbsub.get_all()) {
13 std::cout << msg << std::endl;
14 }
15}
Your own last_result() loop
You don't have to use the await() functions. You can call last_result() in a loop and perform other operations. If you aren't doing other operations, this pattern won't be that useful for you -- in that case, just call await().
1pubnub::context pb("demo", "demo");
2pubnub::futres f = pb.publish("hello_world", "\"Waiting for Godot\"");
3for (;;) {
4 pubnub_res result = f.last_result();
5 if (result() == PNR_STARTED) {
6 // Do something while we wait
7 play_some_tetris();
8 }
9 else if (PNR_OK == f.get()) {
10 std::cout << "Published OK" << std::endl;
11 }
12}
Using futures asynchronously
All asynchronous logic is built upon pubnub::futres::then() (similar to what is available in some future libraries, and proposed for addition to C++17). There are several ways to use futures asynchronously:
- Using futures with a callback
- Using futures with a lambda
- Using futures with a function object
Using futures with a callback
By providing a free function or a static class method, you'll be using the C++ client similarly to that of the C callback interface.
Required UUID
Always set the UUID to uniquely identify the user or device that connects to PubNub. This UUID should be persisted, and should remain unchanged for the lifetime of the user or the device. If you don't set the UUID, you won't be able to connect to PubNub.
1static void on_publish(pubnub::context &pb, pubnub_res result)
2{
3 if (PNR_OK == result) {
4 std::cout << "Published OK" << std::endl;
5 }
6}
7
8int main(int argc, char *argv[])
9{
10 pubnub::context pb("demo", "demo");
11 pubnub::futres f = pb.publish("hello_world", "\"Don't call us\"");
12 f.then(on_publish);
13 return 0;
14}