Presence API for POSIX C++ SDK (legacy)
New SDK available
The POSIX C++ SDK is built on C-Core (legacy), which is still supported. If you are starting a new project, use the new 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="")
Get a list of active UUIDs on multiple channels
1here_now (std::vector<std::string> const &channel, std::vector<std::string> const &channel_group)
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 linesRest 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="")
uuidrequired#- Specifies
uuidfor 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 linesRest 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)
Set state with options
1set_state (std::string const &channel, const &state, set_state_options options )
set_state_options
channel_group#- The
stringwith thechannelname (or comma-delimited list of channelgroupnames) to setstatefor. user_idrequired#- The
user_idof the user for which to setstatefor. IfNULL, the currentuser_idis used. heartbeatrequired#- If set to
true, you can set the state and make a heartbeat call at the same time via the/heartbeatendpoint.
Get state
1state_get (std::string const &channel, std::string const &channel_group="", 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 linesSet 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)
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.