Configuration API for PubNub Arduino SDK
Arduino complete API reference for building real-time applications on PubNub, including basic usage and sample code.
Initialization
This function is used for initializing the PubNub Client API context. This function must be called before attempting to utilize any API functionality in order to establish account level credentials such as publish_key
and subscribe_key
.
Note
To initialize the PubNub library, use the begin()
method in your setup()
function in your sketch.
Method(s)
To Initialize
PubNub you can use the following method(s) in the Arduino SDK:
bool begin(const char *publish_key, const char *subscribe_key, const char *origin = "ps.pndsn.com");
Parameter | Type | Required | Description |
---|---|---|---|
publish_key | const char* | Yes | The string of the publish key to use. |
subscribe_key | const char* | Yes | The string of the subscribe key to use. |
origin | const char* | Optional | The host name of the PubNub origin to use. Defaults to ps.pndsn.com . |
Basic Usage
Note
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.
Pubnub.begin("demo", "demo");
Returns
Type | Description |
---|---|
bool | true if initialization succeeded, false otherwise. |
UUID
This function is used to set a user ID on the fly.
Method(s)
To set UUID
you can use the following method(s) in Arduino SDK
Add Publisher UUID to Message Add Publisher UUID to Message
Note that this function only allows you to set the UUID. Arduino is a subscribe API v1 client and these clients don't implicitly send either the publisher's UUID or the publish timetoken.
If you want to include the publisher information in your message payload, you need to set the client UUID manually, and explicitly include it in your message payload. Refer to Sending publisher UUID for more information.
void set_uuid(const char *uuid);
Parameter | Type | Required | Description |
---|---|---|---|
uuid | const char* | Yes | The UUID to use. Set 0 to not use UUID . |
Basic Usage
Note
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.
Pubnub.set_uuid("myUniqueUUID");
Returns
Type | Description |
---|---|
void | Nothing. |
Sending Publisher UUID
To attach the publisher's UUID to the message, you need to store the UUID in a regular variable and then pass it inside the message payload.
char client_uuid[] = "myUniqueUUID";
PubNub_BASE_CLIENT *client = PubNub.publish("my_channel", "{\"msg\": \"hello\", \"publisher\": \"" + client_uuid + "\"}");
if (!client) {
client->stop();
}
Authentication Key
To set the auth key, use the set_auth()
method in either your setup()
or your loop()
function in your sketch.
Property
To Set Authentication Key
you can use the following method(s) in the Arduino SDK:
void set_auth(const char *auth);
Parameter | Type | Required | Description |
---|---|---|---|
auth | const char* | Yes | The auth key to use. Set 0 to not use the auth key. |
Basic Usage
Pubnub.set_auth("my_auth_key");
Returns
Type | Description |
---|---|
void | Nothing. |