C-Core API & SDK Docs 5.1.1
SDK version
Always use the latest SDK version to have access to the newest features and avoid security vulnerabilities, bugs, and performance issues.
Get Code: Source
https://github.com/pubnub/c-core
Hello World
There is no installation of this SDK. Just clone the Git(Hub) repo, or download a ZIP or tarball of a release from https://github.com/pubnub/c-core/releases It has the code and example Makefiles
to get you started.
Makefiles
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).
Makefile without SSL/TLS Support
The Makefile
for POSIX without SSL/TLS support is available at /posix/posix.mk See /posix/README.md for info on how to build on POSIX (and POSIX-like
) systems.
Makefile with SSL/TLS Support
The Makefile
for POSIX with SSL/TLS, (via OpenSSL) is available at /openssl/posix.mk in the repo. See /openssl/README.md for info on how to build w/OpenSSL on POSIX and other OpenSSL related data.
Makefile with UWP Support
The Makefile
for Windows C with UWP support is available at /windows/uwp.mk in the repo. See /windows/README.md for info on how to build with UWP on Windows.
Makefile with UWP and SSL/TLS Support
The Makefile
for Windows C with UWP and SSL/TLS, (via OpenSSL) is available at /openssl/uwp.mk in the repo. See /openssl/README.md for info on how to build with UWP on Windows.
Including the Header
The calling pattern you choose to use with PubNub (Synchronous vs Callback) will determine which header to import. More information on these calling patterns can be found later on in this guide.
Sync
If using the synchronous (sync) pattern, import only pubnub_sync.h
:
#include "pubnub_sync.h"
Callback
If using the callback pattern, import pubnub_callback.h
and pthread.h
:
#include "pubnub_callback.h"
#include <pthread.h>
Runtime Selection
If using the runtime selection pattern, import pubnub_ntf_enforcement.h
:
#include "core/pubnub_ntf_enforcement.h"
Memory Allocation
This client uses dynamic memory allocation for the PubNub contexts, but the usage is the same as for any other Pubnub C client - always use pubnub_alloc()
to create a context (and check its return value) and always use pubnub_free()
to dispose of a context.
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.
#include "pubnub_alloc.h"
#include <stdio.h>
int main()
{
pubnub_t *ctx = pubnub_alloc();
if (NULL == ctx) {
puts("Couldn't allocate a Pubnub context");
return -1;
}
/* Do something with ctx...
and then: */
pubnub_free(ctx);
return 0
}
Timers
We only provide one timer - the (total) transaction
timer. In general, it is started when a transaction is started and stopped when a transaction is finished. If it expires, the transaction will be cancelled. Keep in mind that this canceling is local, so, for example, if you already published a message, but, for some reason, the HTTP response didn't arrive in time, this canceling will not revoke
the publish - it will just stop the wait for response.
If the transaction timer expires, the outcome of the transaction will be timeout
- different than when you cancel a transaction yourself.
The actual duration of the timer is at least as long as you set it. It could be significantly higher, depending on various platform issues. But, in general, it will be close to what you set.
You should set the timer after initializing the context and before starting a transaction. The duration you set will be used for all subsequent transactions, regardless of their type (i.e. for publish and subscribe and all other).
Thread safety
C-core supports thread-safe operation, though, for performance, you may think about not using it. To use thread-safety support, define the preprocessor symbol PUBNUB_THREADSAFE
(just define it, the value does not matter).
Thread-safe Usage
Thread safety is internal. Just because you can access the PubNub context through the PubNub C-core SDK API from different threads safely, doesn't mean you're off the hook
for your own data that is related to a context. For example, if you're using the callback interface and signalling an event from it to other (worker
) thread(s), you have to synchronise that data transfer yourself.
If you compiled thread-safety support in, you are free to access the same context from different threads, pretty much in any way you wish. However, there are some advised guidelines you should follow:
- If you're using the sync blocking interface, threads that come to wait on the context may wait a long time, so try to avoid it (also, re-think your whole need for a thread-safe C-core)
- If you're using the sync non-blocking interface by calling
pubnub_await
, things are pretty much the same as for sync blocking interface - If you're using the sync non-blocking interface and avoid
pubnub_await
, waiting threads will not block so long, but, pretty much the only useful thing you can do is cancel a transaction from another thread. - Using the sync interface, it's perfectly fine to call
pubnub_await
orpubnub_last_result
in different threads, but, you probably shouldn't do that, as it will make debugging harder. - If you're using the callback interface, it's perfectly fine to call Functions from your callback, but, you should avoid doing that, except for some helper functions. Following this guideline will make your debugging, thus life, a lot easier
Thread-unsafe usage
If you compile without thread-safety support, obviously, you will have an SDK which is not thread safe - that is, it is not safe to use a single context from more than one thread at the same time. So, if you're using such SDK configuration in a multithreaded code, which, on POSIX, you likely are, then:
- If at all possible, use a single context from only one thread - the one that created it
- If 1. is not possible, provide some synchronization yourself, for example, using pthread condition variables, or just mutexes, or some higher abstraction, like message queues
- As a special case, if you're using the callback interface, you can start a transaction in one thread and then don't touch the context from that thread any more - use it only in the callback. This is safe.
Context usage
Keep in mind that it is perfectly safe to use different contexts from different threads at the same time. To each (thread) its own (context).
Transaction and Operation
The C-Core SDK operates as a set of transactions. A transaction is initiated by the client SDK and is defined as a single message exchange between the SDK and PubNub service. Every interaction that the client SDK initiates with PubNub is sequenced as a series of transactions which ultimately results in a PubNub service-specific operation.
Status and Events
The SDK provides a set of status and event identifiers which can help developers interact with the library. The status identifier codes are returned as part of the SDK's API invocation. These are used by the developer to check for status of transactions or for detecting normal / abnormal conditions in an API call. Some of the commonly used status codes are as follows
PNR_OK
: Success, the transaction finished successfullyPNR_STARTED
: The previously initiated transaction has started.PNR_IN_PROGRESS
: Indicates that the previous transaction with PubNub service is still in progress.
Refer to the API docs for a complete list of status identifiers supported by the library.
Events
refer to the PubNub REST operations which are initiated by the client SDK. The most common example of events are subscribe and publish. A client subscribing for a channel is a subscribe event and a client publishing a message on a channel is a publish event.
Some of the common event identifiers are as follows:
PBTT_SUBSCRIBE
: Subscriber operationPBTT_PUBLISH
: Publish operation
Refer to the API docs for a complete list of operations supported by the SDK.
Calling Patterns
This SDK provides sync
, callback
(notification), and runtime_selection
(runtime selection of the calling pattern) interfaces for retrieving the outcome of a Pubnub request/transaction/operation.
Sync
The sync
interface works like this:
- Start a transaction (say, publish - using
pubnub_publish()
) - Either
pubnub_await()
the outcome, or use your own loop in which you checkif (PNR_STARTED != pubnub_last_result())
- Handle the outcome as you wish
This is illustrated in the Hello World
example below (which is the same for any platform that supports sync
interface).
Callback
The callback
interface is somewhat more flexible, uses less CPU resources, but is, in general, a little harder to use. One way to use it is to emulate the sync
interface:
- Create a callback function (
my_callback
) per the prototype required bypubnub_register_callback()
- In
my_callback()
, use a condition variable to signal that outcome was received - Set the callback via
pubnub_register_callback()
- Start a transaction (say, publish - using
pubnub_publish()
) - Wait on the condition variable (the same one used in
my_callback
) - Handle the outcome as you wish
This is illustrated in the Hello World
example below, using pthreads condition variable. Obviously, on platforms that don't support pthreads you will use some similar API (for example, SetEvent/WaitForSingleObject on Windows).
There are other ways to use the callback interface, like the state machine
or similar, where the callback will handle the outcome of a transaction but will also start the next Pubnub transaction, or do other stuff that it needs to do. This is very application specific, so we don't provide an example here.