SelectPHP Presence API Reference for Realtime Apps

These docs are for version 3.8.3 of the PHP SDK. To check out the docs for the latest version Click Here.
 

PubNub 4.0 for PHP is our latest and greatest! Please click here for our PubNub 4.0 for PHP docs and SDK.

PubNub 3.x for PHP will encounter End Of Life (EOL) Aug 1st, 2018. All users currently on 3.x should begin migrating to 4.x. The 3.x SDK will continue to work. We will no longer support it and if you run into an issue, we will request that you upgrade to the latest version before solving the issue.

If you have questions about the PubNub for PHP SDK, please contact us at support@pubnub.com.

Presence enables you to track the online and offline status of users and devices in realtime, 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.


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 hereNow() function in your application.
To call Here Now you can use the following method(s) in the PHP SDK:
  1. ParameterTypeRequiredDescription
    channelStringOptionalSpecifies the channel name to return occupancy results.
    disable_uuidsbooleanOptionalFetch uuids, by default uuids are enabled.
    statebooleanOptionalFetch state, default false.
    channelGroup StringOptionalSpecifies the channel group name to return occupancy results.
 $hereNow = $pubnub->hereNow('my_channel');
 
 print_r($hereNow);
The hereNow() 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']
}
  1. 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.
    $pubnub->hereNow("my_channel", false, true);
    {
    	"status" : 200,
    	"message" : "OK",
    	"service" : "Presence",
    	"uuids" : [
    		{
    			"uuid" : "myUUID0"
    		},
    		{
    			"state" : {
    				"abcd" : {
    					"age" : 15
    				}
    			},
    			"uuid" : "myUUID1"
    		},
    		{
    			"uuid" : "b9eb408c-bcec-4d34-b4c4-fabec057ad0d"
    		},
    		{
    			"state" : {
    				"abcd" : {
    					"age" : 15
    				}
    			},
    			"uuid" : "myUUID2"
    		},
    		{
    			"state" : {
    				"abcd" : {
    					"age" : 24
    				}
    			},
    			"uuid" : "myUUID9"
    		}
    	],
    	"occupancy" : 5
    }
  2. Requires Presence add-on XRequires that the Presence add-on is enabled with Global Here Now checked 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 return the list of UUIDs and occupancy for all channels by omitting the channel:
    $pubnub->hereNow(null, false);
    {
        "total_channels" : 2,
        "total_occupancy" : 3,
        "channels" : {
            "lobby" : {
                "occupancy" : 1,
                "uuids" : [
                    "dara01"
                ]
            },
            "game01" : {
                "occupancy" : 2,
                "uuids" : [
                    "jason01",
                    "jason02"
                ]
            }
        }
    }

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 whereNow() 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 whereNow() you can use the following method(s) in the PHP SDK:
  1. ParameterTypeRequiredDescription
    uuidStringOptionalUUID
You simply need to define the uuid and the callback function to be used to send the data to as in the example below.
$pubnub->whereNow("my_uuid");
The whereNow() 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
    channelStringYesSpecifies channel name to set state for.
    stateArrayYesThe state to set.
    uuidStringOptionalUUID
  1. ParameterTypeRequiredDescription
    channelStringYesSpecifies channel name to get state for.
    uuidStringOptionalUUID.
$pubnub->setState("my_channel", [
    'Key' => "Value"
]);
$pubnub->getState("my_channel");
The state API returns a JSON object containing key value pairs.

{
	first   : "Robert",
	last    : "Plant",
	age     : 59,
	region  : "UK"
}
The state API is used to get or set key/value pairs specific to a subscriber uuid.
State information is supplied as a JSON object of key/value pairs.
To Set State you can use the following method(s) in the PHP SDK
  1. ParameterTypeRequiredDescription
    group
    String
    Yes
    A single channel group to set state on
    state
    Array
    Yes
    A single level state array
    uuid
    String
    Optional
    UUID to set state on. By default the current instance UUID will be used
$pubnub->setChannelGroupState("my_channel_group", [
    'Key' => "Value"
]);
The state API returns a JSON object containing key value pairs.

{
	first   : "Robert",
	last    : "Plant",
	age     : 59,
	region  : "UK"
}