On this page

Presence API for POSIX C++ SDK

Presence lets you track who is online or offline and store custom state information. Presence shows:

  • When a user has joined or left a channel
  • How many users are subscribed to a particular channel (occupancy)
  • Which channels a user or device is subscribed to
  • Presence state associated with these users

Learn more about our Presence feature in the Presence overview.

Here now​

Requires Presence

This method requires that the Presence add-on is enabled for your key in the Admin Portal.

For information on how to receive presence events and what those events are, refer to Presence Events.

This method returns information about the current state of a channel, including a list of unique user IDs (universally unique identifiers, UUIDs) currently subscribed to the channel and the total occupancy count of the channel.

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 Posix C++ SDK:

Get a list of active UUIDs on a single channel​

1here_now (std::string const &channel, std::string const &channel_group="")
* required
ParameterDescription
channel *
Type: std::string const &
Specifies channel for which to return active uuids.
channel_group
Type: std::string const &
Specifies channel_group for which to return active uuids. Wildcards are not supported.

Get a list of active UUIDs on multiple channels​

1here_now (std::vector<std::string> const &channel, std::vector<std::string> const &channel_group)
* required
ParameterDescription
channel *
Type: std::string const &
Specifies channel vector for which to return active uuids.
channel_group *
Type: std::string const &
Specifies channel_group vector for which to return active uuids.

Sample code​

Get a list of uuids subscribed to channel​

1// Sync
2void here_now(pubnub::context &pn) {
3 enum pubnub_res res;
4
5 res = pn.here_now("my_channel").await();
6
7 if (PNR_OK == res) {
8 std::cout << pn.get() << std::endl;
9 } else {
10 std::cout << "Here Now request failed" << std::endl;
11 }
12}
13
14// Lambdas
15void here_now(pubnub::context &pn) {
show all 36 lines

Rest response from server​

The here_now() function returns a list of uuids 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.
1{
2 occupancy : 4,
3 uuids : ['123123234t234f34fq3dq', '143r34f34t34fq34q34q3', '23f34d3f4rq34r34rq23q', 'w34tcw45t45tcw435tww3']
4}

Where now​

Requires Presence

This method requires that the Presence add-on is enabled for your key in the Admin Portal.

For information on how to receive presence events and what those events are, refer to Presence Events.

This method returns the list of channels a UUID is subscribed to.

Timeout events

If the app restarts (or the page refreshes) within the heartbeat window, no timeout event is generated.

Method(s)​

To call where_now() you can use the following method(s) in the Posix C++ SDK:

1where_now (std::string const &uuid="")
* required
ParameterDescription
uuid *
Type: std::string const &
Specifies uuid for which current subscribed channels info is required

Sample code​

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​

1//Sync
2static void where_now(pubnub::context &pn) {
3 enum pubnub_res res;
4
5 try {
6 res = pn.where_now("my_uuid").await();
7
8 if (PNR_OK == res) {
9 std::cout << pn.get() << std::endl;
10 } else {
11 std::cout << "Failed with code " << res << std::endl;
12 }
13 } catch (std::exception &ex) {
14 std::cout << "Exception: " << ex.what() << std::endl;
15 }
show all 41 lines

Rest response from server​

The 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​

1{
2 "channels": [
3 "lobby",
4 "game01",
5 "chat"
6 ]
7}

User state​

Requires Presence

This method requires that the Presence add-on is enabled for your key in the Admin Portal.

For information on how to receive presence events and what those events are, refer to Presence Events.

Clients can set a dynamic custom state (score, game state, location) for their users on one or more channels and store it on a channel as long as the user stays subscribed.

The state is not persisted, and when the client disconnects, the state data is lost. For more information, refer to Presence State.

Method(s)​

Set state​
1set_state (std::string const &channel, std::string const &channel_group, std::string const &uuid, std::string const &state)
* required
ParameterDescription
channel *
Type: std::string const &
Channel to set state on
channel_group *
Type: std::string const &
Channel_group to set state on
uuid *
Type: std::string const &
UUID
state *
Type: std::string const &
Serialized JSON state
Set state with options​
1set_state (std::string const &channel, const &state, set_state_options options )
* required
ParameterDescription
channel *
Type: std::string const &
Channel to set state on
state *
Type: std::string const &
Serialized JSON state
options *
Type: set_state_options
Extra options for this call. Refer to the section below for more information.
set_state_options​
* required
ParameterDescription
channel_group
Type: std::string
The string with the channel name (or comma-delimited list of channel group names) to set state for.
user_id *
Type: std::string
The user_id of the user for which to set state for. If NULL, the current user_id is used.
heartbeat *
Type: bool
If set to true, you can set the state and make a heartbeat call at the same time via the /heartbeat endpoint.
Get state​
1state_get (std::string const &channel, std::string const &channel_group="", std::string const &uuid="")
* required
ParameterDescription
channel *
Type: std::string const &
Channel to get state.
channel_group
Type: std::string const &
Channel group to get the state.
uuid
Type: std::string const &
UUID

Sample code​

Set state​

1static void set_state(pubnub::context &pn) {
2 enum pubnub_res res;
3
4 try {
5 std::string state("{\"first\":\"Robert\", \"last\":\"Plant\", \"age\":59, \"region\":\"UK\"}");
6 res = pn.set_state("my_channel", "", "my_uuid", state).await();
7
8 if (PNR_OK == res) {
9 std::cout << pn.get() << std::endl;
10 } else {
11 std::cout << "Failed with code " << res << std::endl;
12 }
13 } catch (std::exception &ex) {
14 std::cout << "Exception: " << ex.what() << std::endl;
15 }
show all 16 lines

Set state with heartbeat​

1std::cout << "Setting state with options" << std::endl;
2 if (PNR_OK == pb.set_state(
3 chan, "{\"x\":5}", pubnub::set_state_options().heartbeat(true)
4 ).await()) {
5 std::cout << "State was set: " << pb.get() << std::endl;
6 }
7 else {
8 std::cout << "Setting state failed!" << std::endl;
9 }

Get state​

1static void get_state(pubnub::context &pn) {
2 enum pubnub_res res;
3
4 try {
5 res = pn.state_get("my_channel", "", "my_uuid").await();
6
7 if (PNR_OK == res) {
8 std::cout << pn.get() << std::endl;
9 } else {
10 std::cout << "Failed with code " << res << std::endl;
11 }
12 } catch (std::exception &ex) {
13 std::cout << "Exception: " << ex.what() << std::endl;
14 }
15}

Returns​

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

1{
2 first : "Robert",
3 last : "Plant",
4 age : 59,
5 region : "UK"
6}

Heartbeat​

Requires Presence

This method requires that the Presence add-on is enabled for your key in the Admin Portal.

For information on how to receive presence events and what those events are, refer to Presence Events.

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)​

1pb.heartbeat(std::string const& channel, std::string const& channel_group)
* required
ParameterDescription
channel
Type: std::string const&
The string with the channel name (or comma-delimited list of channel names) to notify.
channel_group
Type: std::string const&
The string with the channel_group name (or comma-delimited list of channel group names) to notify.

Sample code​

1res = pb.heartbeat(chan, NULL).await();
2if (PNR_OK == res) {
3 puts("Heartbeated! Got messages:");
4}
5else {
6 printf("Heartbeating failed with code: %d('%s')\n",
7 res,
8 pubnub_res_2_string(res));
9}

Returns​

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