PHPPHP V4 Presence API Reference for Realtime Apps

Go to Publish & Subscribe


 

These docs are for PubNub 4.x for PHP which is our latest and greatest! For the docs of the older versions of the SDK, please check PubNub 3.x for PHP.

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

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 V4 SDK:
  1. ParameterTypeRequiredDefaultsDescription
    channelsString|ArrayOptionalThe channels to get the here now details.
    channelGroupsString|ArrayOptionalThe channel groups to get the here now details.
    includeStateBooleanOptionalfalseIf true, the response will include the presence states of the users for channels/channelGroups.
    includeUuidsBooleanOptionaltrueIf true, the response will include the UUIDs of the connected clients.
try {
    $result = $pubnub->hereNow()
                  ->channels(["my_channel", "demo"])
                  ->includeUuids(true)
                  ->sync();
} catch (PubNubException $err) {
    print_r($err);
}

foreach ($result->getChannels() as $channelData) {
    print("---\n");
    printf("channel: %s\n", $channelData->getChannelName());
    printf("occupancy: %s\n", $channelData->getOccupancy());

    foreach ($channelData->getOccupants() as $occupant) {
        printf("uuid: %s, state: %s\n", $occupant->getUuid(), $occupant->getState());
    }
}
The hereNow() operation returns a PNHereNowResult which contains the following fields:
MethodTypeDescription
getTotalChannels()IntegerTotal Channels.
getTotalOccupancy()IntegerTotal Occupancy.
getChannels()ArrayA array with values of PNHereNowChannelData for each channel. See PNHereNowChannelData for more details.
MethodTypeDescription
getChannelName()StringChannel name.
getOccupancy()IntegerOccupancy of the channel.
getOccupants()ArrayA array of PNHereNowOccupantData, see PNHereNowOccupantData for more details.
MethodTypeDescription
getUuid()StringUUID of the user.
getState()ArrayState of the user.
  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.
    $result = $pubnub->hereNow()
                  ->channels("my_channel")
                  ->includeUuids(true)
                  ->includeState(true)
                  ->sync();
    PubNub\Models\Consumer\Presence\PNHereNowResult Object(
        [totalChannels:protected] => 2
        [totalOccupancy:protected] => 3
        [channels:protected] => Array(
            [0] => PubNub\Models\Consumer\Presence\PNHereNowChannelData Object(
                [channelName:protected] => ch1
                [occupancy:protected] => 1
                [occupants:protected] => Array(
                    [0] => PubNub\Models\Consumer\Presence\PNHereNowOccupantsData Object(
                        [uuid:protected] => user1
                        [state:protected] =>
                    )
                )
            )
            [1] => PubNub\Models\Consumer\Presence\PNHereNowChannelData Object(
                [channelName:protected] => ch2
                [occupancy:protected] => 2
                [occupants:protected] => Array(
                    [0] => PubNub\Models\Consumer\Presence\PNHereNowOccupantsData Object(
                        [uuid:protected] => user1
                        [state:protected] =>
                    )
                    [1] => PubNub\Models\Consumer\Presence\PNHereNowOccupantsData Object(
                        [uuid:protected] => user3
                        [state:protected] =>
                    )
                )
            )
        )
     )
  2. 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 return only the occupancy information for a single channel by specifying the channel and setting UUIDs to false:
    $result = $pubnub->hereNow()
                  ->channels("my_channel")
                  ->includeUuids(false)
                  ->includeState(false)
                  ->sync();
    PubNub\Models\Consumer\Presence\PNHereNowResult Object(
        [totalChannels:protected] => 2
        [totalOccupancy:protected] => 3
        [channels:protected] => Array(
            [0] => PubNub\Models\Consumer\Presence\PNHereNowChannelData Object(
                [channelName:protected] => ch1
                [occupancy:protected] => 1
                [occupants:protected] => Array(
                    [0] => PubNub\Models\Consumer\Presence\PNHereNowOccupantsData Object(
                        [uuid:protected] => user1
                        [state:protected] =>
                    )
                )
            )
            [1] => PubNub\Models\Consumer\Presence\PNHereNowChannelData Object(
                [channelName:protected] => ch2
                [occupancy:protected] => 2
                [occupants:protected] => Array(
                    [0] => PubNub\Models\Consumer\Presence\PNHereNowOccupantsData Object(
                        [uuid:protected] => user1
                        [state:protected] =>
                    )
                    [1] => PubNub\Models\Consumer\Presence\PNHereNowOccupantsData Object(
                        [uuid:protected] => user3
                        [state:protected] =>
                    )
                )
            )
        )
    )
  3. 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:
    $result = $pubnub->hereNow()
                  ->includeUuids(true)
                  ->includeState(false)
                  ->sync();
    PubNub\Models\Consumer\Presence\PNHereNowResult Object(
        [totalChannels:protected] => 2
        [totalOccupancy:protected] => 3
        [channels:protected] => Array(
            [0] => PubNub\Models\Consumer\Presence\PNHereNowChannelData Object(
                [channelName:protected] => ch1
                [occupancy:protected] => 1
                [occupants:protected] => Array(
                    [0] => PubNub\Models\Consumer\Presence\PNHereNowOccupantsData Object(
                        [uuid:protected] => user1
                        [state:protected] =>
                    )
                )
            )
            [1] => PubNub\Models\Consumer\Presence\PNHereNowChannelData Object(
                [channelName:protected] => ch2
                [occupancy:protected] => 2
                [occupants:protected] => Array(
                    [0] => PubNub\Models\Consumer\Presence\PNHereNowOccupantsData Object(
                        [uuid:protected] => user1
                        [state:protected] =>
                    )
                    [1] => PubNub\Models\Consumer\Presence\PNHereNowOccupantsData Object(
                        [uuid:protected] => user3
                        [state:protected] =>
                    )
                )
            )
        )
    )
  4. 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 only the occupancy information (Global Here Now) by omitting the channel name
    $result = $pubnub->hereNow()
                  ->includeState(true)
                  ->includeUuids(true)
                  ->sync();
    PubNub\Models\Consumer\Presence\PNHereNowResult Object(
        [totalChannels:protected] => 2
        [totalOccupancy:protected] => 3
        [channels:protected] => Array(
            [0] => PubNub\Models\Consumer\Presence\PNHereNowChannelData Object(
                [channelName:protected] => ch1
                [occupancy:protected] => 1
                [occupants:protected] => Array(
                    [0] => PubNub\Models\Consumer\Presence\PNHereNowOccupantsData Object(
                        [uuid:protected] => user1
                        [state:protected] =>
                    )
                )
            )
            [1] => PubNub\Models\Consumer\Presence\PNHereNowChannelData Object(
                [channelName:protected] => ch2
                [occupancy:protected] => 2
                [occupants:protected] => Array(
                    [0] => PubNub\Models\Consumer\Presence\PNHereNowOccupantsData Object(
                        [uuid:protected] => user1
                            [state:protected] =>
                    )
                    [1] => PubNub\Models\Consumer\Presence\PNHereNowOccupantsData Object(
                        [uuid:protected] => user3
                        [state:protected] =>
                    )
                )
            )
        )
    )
  5. $pubnub->hereNow()
        ->channelGroups(["cg1", "cg2", "cg3"])
        ->includeUuids(true)
        ->includeState(true)
        ->sync();
    (
        [totalChannels:protected] => 1
        [totalOccupancy:protected] => 4
        [channels:protected] => Array(
            [0] => PubNub\Models\Consumer\Presence\PNHereNowChannelData Object(
                [channelName:protected] => ch1
                [occupancy:protected] => 1
                [occupants:protected] => Array(
                    [0] => PubNub\Models\Consumer\Presence\PNHereNowOccupantsData Object(
                        [uuid:protected] => 123123234t234f34fq3dq
                        [state:protected] =>
                    )
                    [1] => PubNub\Models\Consumer\Presence\PNHereNowOccupantsData Object(
                        [uuid:protected] => 143r34f34t34fq34q34q3
                        [state:protected] =>
                    )
                    [2] => PubNub\Models\Consumer\Presence\PNHereNowOccupantsData Object(
                        [uuid:protected] => 23f34d3f4rq34r34rq23q
                        [state:protected] =>
                    )
                    [3] => PubNub\Models\Consumer\Presence\PNHereNowOccupantsData Object(
                        [uuid:protected] => w34tcw45t45tcw435tww3
                        [state:protected] =>
                    )
                )
            )
        )
     )
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 V4 SDK:
  1. ParameterTypeRequiredDefaultsDescription
    uuidStringOptionalUuid of the user we want to spy on.
You simply need to define the uuid and the callback function to be used to send the data to as in the example below.
$result = $pubnub->whereNow()
              ->sync();
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"
	]
}
  1. $result = $pubnub->whereNow()
                  ->uuid("his-uuid")
                  ->sync();
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
    channelsString|ArrayOptionalchannels to set state.
    channelGroupsString|ArrayOptionalchannel groups to set state.
    stateArrayOptionalState to set.
  1. ParameterTypeRequiredDescription
    channelsString|ArrayOptionalchannels to get state.
    channelGroupsString|ArrayOptionalchannel groups to get state.
    uuidStringOptionaluuid
$pubnub->setState()
    ->channels(["ch1", "ch2", "ch3"])
    ->state(["age" => 30])
    ->sync();
$pubnub->getState()
    ->channels(["ch1", "ch2", "ch3"])
    ->sync();
The setState() operation returns a PNSetStateResult which contains the following fields:
MethodTypeDescription
setState()ArrayArray of UUIDs and the user states.
The getState() operation returns a PNGetStateResult which contains the following fields:
MethodTypeDescription
getChannels()ArrayArray of channels and the user states.
  1. $pubnub->setState()
        ->channelGroups(["gr1", "gr2", "gr3"])
        ->state(["age" => 30])
        ->sync();

Go to Access Manager