Posix C++Posix C++Windows C++C++Posix C++ Presence API Reference for Realtime Apps

Go to Publish & Subscribe


Requires Presence add-on XRequires that the Presence add-on is enabled for your key. See this page on enabling add-on features on your keys:

http://www.pubnub.com/knowledge-base/discussion/644/how-do-i-enable-add-on-features-for-my-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 here_now() function in your application.
To call Here Now you can use the following method(s) in the Posix C++ SDK:
  1. ParameterTypeRequiredDescription
    channelstd::string const &YesSpecifies channel for which to return active uuids.
    channel_groupstd::string const &OptionalSpecifies channel_group for which to return active uuids.
  2. ParameterTypeRequiredDescription
    channelstd::string const &YesSpecifies channel vector for which to return active uuids.
    channel_groupstd::string const &YesSpecifies channel_group vector for which to return active uuids.
// Sync
void here_now(pubnub::context &pn) {
  enum pubnub_res res;
 
  res = pn.here_now("my_channel").await();
 
  if (PNR_OK == res) {
    std::cout << pn.get() << std::endl;
  } else {
    std::cout << "Here Now request failed" << std::endl;
  }
}
 
// Lambdas
void here_now(pubnub::context &pn) {
  pn.here_now("my_channel").then([=](pubnub::context &pn, pubnub_res res) {
      if (PNR_OK == res) {
        std::cout << pn.get() << std::endl;
      } else {
        std::cout << "Here Now request failed" << std::endl;
      }
    });
}
 
// Functions
void on_here_now(pubnub::context &pn, pubnub_res res) {
  if (PNR_OK == res) {
    std::cout << pn.get() << std::endl;
  } else {
    std::cout << "Here Now request failed" << std::endl;
  }
}
 
void here_now(pubnub::context &pn) {
  pn.here_now("my_channel").then(on_here_now);
}
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.
{
	occupancy : 4,
	uuids : ['123123234t234f34fq3dq', '143r34f34t34fq34q34q3', '23f34d3f4rq34r34rq23q', 'w34tcw45t45tcw435tww3']
}
Return Occupancy for all channels by calling the Global Here Now function in your application.
To call Global Here Now you can use the following method(s) in the Posix C++ SDK:
//Sync
static void global_here_now(pubnub::context &pn) {
  enum pubnub_res res;

  try {
    res = pn.global_here_now().await();

    if (PNR_OK == res) {
      std::cout <<  pn.get() << std::endl;
    } else {
      std::cout << "Failed with code " << res << std::endl;
    }
  } catch (std::exception &ex) {
    std::cout << "Exception: " << ex.what() << std::endl;
  }
}

//Lambdas
static void global_here_now(pubnub::context &ipn) {
  ipn.global_here_now()
    .then([=](pubnub::context &pn, pubnub_res res) {
      if (PNR_OK == res) {
        std::cout <<  pn.get() << std::endl;
      } else {
        std::cout << "Failed with code " << res << std::endl;
      }
  });
}

//Functions
static void on_global_here_now(pubnub::context &pn, pubnub_res res) {
  if (PNR_OK == res) {
    std::cout <<  pn.get() << std::endl;
  } else {
    std::cout << "Failed with code " << res << std::endl;
  }
}

static void global_here_now(pubnub::context &ipn) {
  ipn.global_here_now().then(on_global_here_now);
}
{
	"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"
}
Requires Presence add-on XRequires that the Presence add-on is enabled for your key. See this page on enabling add-on features on your keys:

http://www.pubnub.com/knowledge-base/discussion/644/how-do-i-enable-add-on-features-for-my-keys.
You can obtain information about the current list of channels to which a UUID is subscribed to by calling the where_now() function in your application.
 
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.
To call where_now() you can use the following method(s) in the Posix C++ SDK:
  1. ParameterTypeRequiredDescription
    uuidstd::string const &YesSpecifies uuid for which current subscribed channels info is required
You simply need to define the uuid and the callback function to be used to send the data to as in the example below.
//Sync
static void where_now(pubnub::context &pn) {
  enum pubnub_res res;

  try {
    res = pn.where_now("my_uuid").await();

    if (PNR_OK == res) {
      std::cout <<  pn.get() << std::endl;
    } else {
      std::cout << "Failed with code " << res << std::endl;
    }
  } catch (std::exception &ex) {
    std::cout << "Exception: " << ex.what() << std::endl;
  }
}

//Lambdas
static void where_now(pubnub::context &ipn) {
  ipn.where_now("my_uuid")
    .then([=](pubnub::context &pn, pubnub_res res) {
      if (PNR_OK == res) {
        std::cout <<  pn.get() << std::endl;
      } else {
        std::cout << "Failed with code " << res << std::endl;
      }
  });
}

//Functions
static void on_where_now(pubnub::context &pn, pubnub_res res) {
  if (PNR_OK == res) {
    std::cout <<  pn.get() << std::endl;
  } else {
    std::cout << "Failed with code " << res << std::endl;
  }
}

static void where_now(pubnub::context &ipn) {
  ipn.where_now("my_uuid").then(on_where_now);
}
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.
{
	"channels": [
		"lobby",
		"game01",
		"chat"
	]
}
Requires Presence add-on XRequires that the Presence add-on is enabled for your key. See this page on enabling add-on features on your keys:

http://www.pubnub.com/knowledge-base/discussion/644/how-do-i-enable-add-on-features-for-my-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.
  1. ParameterTypeRequiredDescription
    channelstd::string const &YesChannel to set state on
    channel_groupstd::string const &YesChannel_group to set state on
    uuidstd::string const &YesUUID
    statestd::string const &YesSerialized JSON state
  1. ParameterTypeRequiredDescription
    channelstd::string const &YesChannel to get state.
    channel_groupstd::string const &OptionalChannel group to get the state.
    uuidstd::string const &OptionalUUID
static void set_state(pubnub::context &pn) {
  enum pubnub_res res;

  try {
    std::string state("{\"first\":\"Robert\", \"last\":\"Plant\", \"age\":59, \"region\":\"UK\"}");
    res = pn.set_state("my_channel", "", "my_uuid", state).await();

    if (PNR_OK == res) {
      std::cout <<  pn.get() << std::endl;
    } else {
      std::cout << "Failed with code " << res << std::endl;
    }
  } catch (std::exception &ex) {
    std::cout << "Exception: " << ex.what() << std::endl;
  }
}
static void get_state(pubnub::context &pn) {
  enum pubnub_res res;

  try {
    res = pn.state_get("my_channel", "", "my_uuid").await();

    if (PNR_OK == res) {
      std::cout <<  pn.get() << std::endl;
    } else {
      std::cout << "Failed with code " << res << std::endl;
    }
  } catch (std::exception &ex) {
    std::cout << "Exception: " << ex.what() << std::endl;
  }
}
The state API returns a JSON object containing key value pairs.
{
	first   : "Robert",
	last    : "Plant",
	age     : 59,
	region  : "UK"
}

Go to Channel Groups