FreeRTOS Presence API Reference for Realtime Apps
Presence enables you to track the online and offline status of users and devices in real time, as well as store custom state information. Presence provides authoritative information on:
- When a user has joined or left a channel
- Who, and how many, users are subscribed to a particular channel
- Which channel(s) an individual user is subscribed to
- Associated state information for these users
Learn more about our Presence feature here.
Here Now
Requires Presence add-on Requires that the Presence add-on is enabled for your key. See this page on enabling add-on features on your keys:
https://support.pubnub.com/hc/en-us/articles/360051974791-How-do-I-enable-add-on-features-for-my-keys-
Description
You can obtain information about the current state of a channel including a list of unique user-ids currently subscribed to the channel and the total occupancy count of the channel by calling the pubnub_here_now()
function in your application.
Method(s)
To call Here Now
you can use the following method(s) in the FreeRTOS SDK:
enum pubnub_res pubnub_here_now (pubnub_t *p, const char *channel, const char *channel_group)
Parameter Type Required Description p
pubnub_t* Yes Pointer to PubNub client context. channel
const char* Optional The string
with thechannel
name (or comma-delimited list ofchannel
names) to getpresence
info for.channel_group
const char* Optional The string
with thechannel
name (or comma-delimited list of channelgroup
names) to getpresence
info for.
Basic Usage
Get a list of uuids subscribed to channel:
// Sync
char const *chan = "my_channel";
char const *msg;
enum pubnub_res res;
res = pubnub_here_now(pn, chan, NULL);
if (res != PNR_STARTED) {
return -1;
}
res = pubnub_await(pn);
if (res == PNR_STARTED) {
return -1;
}
if (PNR_OK == res) {
msg = pubnub_get(pn);
// Print out, on Windows simulator `puts(msg)` should work
} else {
return -1;
}
return 0;
//callback
int start_here_now(pubnub_t *pn) {
char const *chan = "my_channel";
if (PNR_STARTERD != pubnub_here_now(pn, chan, NULL)) {
return -1;
}
return 0;
}
int receive_here_now(pubnub_t *pn, enum pubnub_res res) {
char const *msg;
if (res == PNR_STARTED) {
return -1;
}
if (PNR_OK == res) {
msg = pubnub_get(pn);
// Print out, on Windows simulator `puts(msg)` should work
} else {
return -1;
}
return 0;
}
Rest Response from Server
The pubnub_here_now()
function returns a list of uuid s currently subscribed to the channel.
uuids:["String","String", ... ,"String"]
- List of UUIDs currently subscribed to the channel.occupancy: Number
- Total current occupancy of the channel.
{
occupancy : 4,
uuids : ['123123234t234f34fq3dq', '143r34f34t34fq34q34q3', '23f34d3f4rq34r34rq23q', 'w34tcw45t45tcw435tww3']
}
Global Here Now
Description
Return Occupancy
for all channels by calling the pubnub_global_here_now()
function in your application.
Method(s)
To call Global Here Now
you can use the following method(s) in the FreeRTOS SDK:
enum pubnub_res pubnub_global_here_now (pubnub_t *p)
Parameter Type Required Description p
pubnub_t* Yes Pointer to Pubnub Client Context. Can't be NULL
Basic Usage
// Sync
char const *msg;
enum pubnub_res res;
res = pubnub_global_here_now(pn);
if (res != PNR_STARTED) {
return -1;
}
res = pubnub_await(pn);
if (res == PNR_STARTED) {
return -1;
}
if (PNR_OK == res) {
msg = pubnub_get(pn);
// Print out, on Windows simulator `puts(msg)` should work
} else {
return -1;
}
return 0;
// Callback
int start_global_here_now(pubnub_t *pn) {
if (pubnub_global_here_now(pn) != PNR_STARTED) {
return -1;
}
return 0;
}
int received_global_here_now(pubnub_t *pn,enum pubnub_res res) {
if (PNR_OK == res) {
char const *msg = pubnub_get(pn);
// Print out, on Windows simulator `puts(msg)` should work
} else {
return -1;
}
return 0;
}
Rest Response from Server
{
"status": 200,
"message": "OK",
"payload": {
"channels": {
"81d8d989-b95f-443c-a726-04fac323b331": {
"uuids": [
"70fc1140-22b5-4abc-85b2-ff8c17b24d59"
],
"occupancy": 1
},
"81b7a746-d153-49e2-ab70-3037a75cec7e": {
"uuids": [
"91363e7f-584b-49cc-822c-52c2c01e5928"
],
"occupancy": 1
},
"c068d15e-772a-4bcd-aa27-f920b7a4eaa8": {
"uuids": [
"ccfac1dd-b3a5-4afd-9fd9-db11aeb65395"
],
"occupancy": 1
}
},
"total_channels": 3,
"total_occupancy": 3
},
"service": "Presence"
}
Where Now
Requires Presence add-on Requires that the Presence add-on is enabled for your key. See this page on enabling add-on features on your keys:
https://support.pubnub.com/hc/en-us/articles/360051974791-How-do-I-enable-add-on-features-for-my-keys-
Description
You can obtain information about the current list of channels to which a UUID is subscribed to by calling the pubnub_where_now() function in your application.
Note
If the app is killed/crashes and restarted (or the page containing the PubNub instance is refreshed on the browser) within the heartbeat window no timeout event is generated.
Method(s)
To call pubnub_where_now() you can use the following method(s) in the FreeRTOS SDK:
enum pubnub_res pubnub_where_now (pubnub_t *p, const char *uuid)
Parameter Type Required Description p
pubnub_t* Yes Pointer to PubNub client context. uuid
const char* Optional The UUID
of the user to get thechannel
presence. IfNULL
, the currentUUID
of thep
context will be used.
Basic Usage
You simply need to define the uuid
and the callback
function to be used to send the data to as in the example below.
Get a list of channels a UUID is subscribed to:
// Sync
char const *msg;
enum pubnub_res res;
res = pubnub_where_now(pn, "my_uuid");
if (res != PNR_STARTED) {
return -1;
}
res = pubnub_await(pn);
if (res == PNR_STARTED) {
return -1;
}
if (PNR_OK == res) {
msg = pubnub_get(pn);
// Print out, on Windows simulator `puts(msg)` should work
} else {
return -1;
}
return 0;
// Callback
int start_where_now(pubnub_t *pn) {
return PNR_STARTED == pubnub_where_now(pn, "my_uuid") ? 0 : -1;
}
int receive_where_now(pubnub_t *pn, enum pubnub_res res) {
if (PNR_OK == res) {
char const* msg = pubnub_get(pn);
// Print out, on Windows simulator `puts(msg)` should work
} else {
return -1;
}
return 0;
}
Rest Response from Server
The pubnub_where_now() function returns a list of channels a uuid is subscribed to.
channels:["String","String", ... ,"String"]
- List of channels a uuid is subscribed to.
Example Response:
{
"channels": [
"lobby",
"game01",
"chat"
]
}
User State
Requires Presence add-on Requires that the Presence add-on is enabled for your key. See this page on enabling add-on features on your keys:
https://support.pubnub.com/hc/en-us/articles/360051974791-How-do-I-enable-add-on-features-for-my-keys-
Description
The state API is used to set/get key/value pairs specific to a subscriber uuid
.
State information is supplied as a JSON object of key/value pairs.
Method(s)
-
enum pubnub_res pubnub_set_state (pubnub_t *p, char const *channel, char const *channel_group, const char *uuid, char const *state)
Parameter Type Required Description p
pubnub_t* Yes Pointer to PubNub client context. channel
char const* Optional The string
with thechannel
name (or comma-delimited list ofchannel
names) to setstate
for.channel_group
char const* Optional The string
with thechannel
name (or comma-delimited list of channelgroup
names) to setstate
for.uuid
const char* Yes The UUID
of the user for which to setstate
for. IfNULL
, the currentUUID
of thep
context will be used.state
char const* Yes Has to be a JSON object -
enum pubnub_res pubnub_state_get (pubnub_t *p, char const *channel, char const *channel_group, const char *uuid)
Parameter Type Required Description p
pubnub_t* Yes Pointer to Pubnub Client Context channel
char const* Optional The string with the channel
name (or comma-delimited list of channel names) to get the state for.channel_group
char const* Optional The string with the channel
name (or comma-delimited list ofchannel group
names) to get thestate
for.uuid
const char* Optional The UUID
of the user for which to get thestate
for. If NULL, the currentUUID
of thep
context will be used.
Basic Usage
pubnub_set_state(ctx, "hello_world", NULL, NULL, NULL);
pbresult = pubnub_await(ctx);
if (PNR_OK == pbresult) {
printf("Set success\n");
}
pubnub_get_state(ctx, "my_channel", NULL, NULL);
pbresult = pubnub_await(ctx);
if (PNR_OK == pbresult) {
char const *json_response = pubnub_get(ctx);
}
Returns
The state API returns a JSON object containing key value pairs.
{
first : "Robert",
last : "Plant",
age : 59,
region : "UK"
}