Presence API for PubNub Windows C SDK

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

This method requires that the Presence add-on is enabled for your key in the Admin Portal. Read the support page on enabling add-on features on your keys.

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.

Cache

This method has a 3 second response cache time.

Method(s)

To call Here Now you can use the following method(s) in the Windows C SDK:

enum pubnub_res pubnub_here_now (pubnub_t *p, const char *channel, const char *channel_group)
ParameterTypeRequiredDescription
ppubnub_t*YesPointer to Pubnub client context.
channelconst char*OptionalThe string with the channel name (or comma-delimited list of channel names) to get presence info for.
channel_groupconst char*OptionalThe string with the channel name (or comma-delimited list of channel group names) to get presence info for.

Basic Usage

Get a list of uuids subscribed to channel

// Sync

pubnub_here_now(ctx, "my_channel", NULL);
pbresult = pubnub_await(ctx);
if (PNR_OK == pbresult) {
char const *json_response = pubnub_get(ctx);
}

//callback

int here_now(pubnub_t *pn) {
char const *chan = "my_channel";
char const *msg;
enum pubnub_res res;

show all 34 lines

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']
}

Where Now

Requires Presence add-on

This method requires that the Presence add-on is enabled for your key in the Admin Portal. Read the support page on enabling add-on features on your keys.

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.

Timeout events

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 Windows C SDK:

enum pubnub_res pubnub_where_now (pubnub_t *p, const char *uuid)
ParameterTypeRequiredDescription
ppubnub_t*YesPointer to Pubnub client context.
uuidconst char*OptionalThe UUID of the user to get the channel presence. If NULL, the current UUID of the p 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) {
printf("pubnub_where_now() returned unexpected: %d\n", res);
return -1;
}

res = pubnub_await(pn);

if (res == PNR_STARTED) {
printf("pubnub_await() returned unexpected: PNR_STARTED(%d)\n", res);
show all 53 lines

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

This method requires that the Presence add-on is enabled for your key in the Admin Portal. Read the support page on enabling add-on features on your keys.

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)

Set State

enum pubnub_res pubnub_set_state (pubnub_t *p, char const *channel, char const *channel_group, const char *uuid, char const *state)
ParameterTypeRequiredDescription
ppubnub_t*YesPointer to Pubnub client context.
channelchar const*OptionalThe string with the channel name (or comma-delimited list of channel names) to set state for.
channel_groupchar const*OptionalThe string with the channel name (or comma-delimited list of channel group names) to set state for.
uuidconst char*YesThe UUID of the user for which to set state for. If NULL, the current UUID of the p context will be used.
statechar const*YesHas to be a JSON object

Get State

enum pubnub_res pubnub_state_get (pubnub_t *p, char const *channel, char const *channel_group, const char *uuid)
ParameterTypeRequiredDescription
ppubnub_t*YesPointer to Pubnub Client Context
channelchar const*OptionalThe string with the channel name (or comma-delimited list of channel names) to get the state for.
channel_groupchar const*OptionalThe string with the channel name (or comma-delimited list of channel group names) to get the state for.
uuidconst char*OptionalThe UUID of the user for which to get the state for. If NULL, the current UUID of the p context will be used.

Basic Usage

Set State

pubnub_set_state(ctx, "hello_world", NULL, NULL, NULL);
pbresult = pubnub_await(ctx);
if (PNR_OK == pbresult) {
printf("Set success\n");
}

Get State

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"
}

Extended Set State

Requires Presence add-on

This method requires that the Presence add-on is enabled for your key in the Admin Portal. Read the support page on enabling add-on features on your keys.

Extended Set State Options structure

ParameterTypeRequiredDescription
ppubnub_t*YesPointer to PubNub client context.
channel_groupchar const*OptionalThe string with the channel name (or comma-delimited list of channel group names) to set state for.
user_idconst char*YesThe user_id of the user for which to set state for. If NULL, the current user_id of the p context will be used.
heartbeatboolYesIf set to true, you can set the state and make a heartbeat call at the same time via the /heartbeat endpoint.

Description

The extended state API is used to set/get key/value pairs specific to a subscriber user_id with extra options.

State information is supplied as a JSON object of key/value pairs.

Method(s)

Set State with options

enum pubnub_res pubnub_set_state_ex(pubnub_t* p, 
const char* channel,
const char* state,
struct pubnub_set_state_options opts);
ParameterTypeRequiredDescription
ppubnub_t*YesPointer to PubNub client context.
channelchar const*OptionalThe string with the channel name (or comma-delimited list of channel names) to set state for.
statechar const*YesHas to be a JSON object.
optspubnub_set_state_optionsYesRefer to Extended set state options structure.

Basic Usage

Set State with Heartbeat

struct pubnub_set_state_options opt = pubnub_set_state_defopts();
opt.heartbeat = true;
pbresult = pubnub_set_state_ex(pn, "my_channel", "{}", opt);

Returns

The state API returns a JSON object containing key value pairs.

The state API returns the result of the transaction.

State was set: {"status": 200, "message": "OK", "service": "Presence"}

Heartbeat

Requires Presence add-on

This method requires that the Presence add-on is enabled for your key in the Admin Portal. Read the support page on enabling add-on features on your keys.

This method notifies channels and channel groups about a client's presence. You can send heartbeats to channels you are not subscribed to.

Method(s)

enum pubnub_res pubnub_heartbeat(pubnub_t*   pb, const char* channel, const char* channel_group)
ParameterTypeRequiredDescription
pbpubnub_t*YesPointer to PubNub client context.
channelchar const*OptionalThe string with the channel name (or comma-delimited list of channel names) to notify.
channel_groupchar const*OptionalThe string with the channel_group name (or comma-delimited list of channel group names) to notify.

Basic Usage

res = pubnub_heartbeat(pbp, chan, NULL);
if (PNR_STARTED == res) {
res = pubnub_await(pbp);
}
if (PNR_OK == res) {
puts("Heartbeated! Got messages:");
}
else {
printf("Heartbeating failed with code: %d('%s')\n",
res,
pubnub_res_2_string(res));
}

Returns

This method returns a PubNub result enum or an error, depending on the status of the transaction.

Last updated on