PubNub POSIX C++ SDK 4.11.2
Get Code: Source
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
:
#include "pubnub.hpp"
#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.
pubnub::context pb("demo", "demo");
pubnub::futres f = pb.publish("hello_world", "\"Here I am!\"");
if (PNR_OK == f.await()) {
std::cout << "Published OK" << std::endl;
}
pubnub::futres f = pb.publish("hello_world", "\"Stuck in the middle\"");
if (PNR_OK == f.get()) {
std::cout << "Published OK (again)" << std::endl;
}
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.
pubnub::context pb("demo", "demo");
pubnub::context pbsub("demo", "demo");
pbsub.subscribe("hello_world").await(); // connect
pubnub::futres fsub = pbsub.subscribe("hello_world");
fsub.start_await();
pubnub::futres f = pb.publish("hello_world", "\"with you!\"");
if (PNR_OK == f.await()) {
std::cout << "Published OK" << std::endl;
}
if (PNR_OK == fsub.end_await()) {
std::cout << "Received messages:" << std::endl;
for (auto &&msg : pbsub.get_all()) {
std::cout << msg << std::endl;
}
}