Build Realtime Apps With The Real-Time Network - the mbed PubNub API+SDK
The PubNub library enables your mbed board to communicate with the world via the PubNub cloud messaging system.
The library provides a PubNub class that is tied to a particular set of keys and offers methods that correspond to the appropriate API methods - publish, subscribe, history, etc. The JSON encoded messages are passed as raw strings to conserve memory, but at your option, you can use e.g. picojson library to deal with JSON. The API is synchronous - use multiple rtos threads to talk to PubNub on the background.
Getting Started
Can't wait to try it out? Connect your mbed application board and proceed to the demo project:
Import programPubNubDemo
Reference demo of the PubNub library for the mbed application board - control your board over the internet!
Library Usage
Import library
Public Member Functions |
|
PubNub (const char *publish_key, const char *subscribe_key, const char *origin="http://pubsub.pubnub.com") | |
Init a Pubnub Client context.
|
|
PubNubRes | publish (const char *channel, const char *message, char **reply=NULL) |
Publish API call.
|
|
PubNubRes | subscribe (const char *channel, char **reply) |
Subscribe API call.
|
|
PubNubRes | history (const char *channel, char **reply, int *replysize, int limit=10) |
History API call.
|
|
PubNubRes | time (char *ts) |
Time API call.
|
Diff: PubNub.h
- Revision:
- 3:36f064f7bdf0
- Parent:
- 2:d78239c9ebb8
- Child:
- 4:a4759c403023
diff -r d78239c9ebb8 -r 36f064f7bdf0 PubNub.h --- a/PubNub.h Sun Mar 02 01:43:20 2014 +0000 +++ b/PubNub.h Sun Mar 02 01:47:11 2014 +0000 @@ -90,16 +90,20 @@ ~PubNub(); - /** Publish + /** Publish API call * * Send a message (assumed to be well-formed JSON) to a given channel. * * Examples: + * @code p.publish("demo", "\"lala\""); + * @endcode * or + * @code if (p.publish("demo", "{\"lala\":1}") != PNR_OK) { blink_error(); } + * @endcode * * @param channel required channel name. * @param message required JSON message object. @@ -107,7 +111,7 @@ * @return PNR_OK on success. */ PubNubRes publish(const char *channel, const char *message, char **reply = NULL); - /** Subscribe + /** Subscribe API call * * Listen for a message on a given channel. The function will block * and return when a message arrives. Typically, you will run this @@ -128,6 +132,7 @@ * before waiting for new data from the server. * * Examples: + * @code while (1) { char *reply; if (p.subscribe("demo", &reply) != PNR_OK) continue; @@ -136,6 +141,7 @@ if (sscanf(reply, "{\"code\":%d}", &code) == 1) printf("received JSON msg with code %d\n", code); } + * @endcode * * @param channel required channel name. * @param reply required pointer for passing the returned reply (do not free()). @@ -144,7 +150,7 @@ /* TODO: subscribe_multi */ - /** History + /** History API call * * Receive list of the last N messages on the given channel. * @@ -154,6 +160,7 @@ * can be NULL if there is no history for this channel. * * Example: + * @code char *reply; int replysize; if (p.history("demo", &reply, &replysize) != PNR_OK) return; @@ -161,6 +168,7 @@ for (char *msg = reply; msg < reply + replysize; msg += strlen(msg)+1) printf("historic message: %s", msg); free(reply); + * @endcode * * @param channel required channel name. * @param reply required pointer for passing the returned reply (free() after use). @@ -169,7 +177,7 @@ * @return PNR_OK on success. */ PubNubRes history(const char *channel, char **reply, int *replysize, int limit = 10); - /** Time + /** Time API call * * Receive the current server timestamp and store it in the * provided string buffer (a char[32] or larger).