Presence API for PubNub PHP SDK

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

Here Now

Requires Presence add-on

This method requires that the Presence add-on is enabled for your key in the Admin Portal. Read the support page on enabling add-on features on your keys.

Description

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.

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 PHP V4 SDK:

$pubnub->hereNow()->channels(string|array)->channelGroups(string|array)->includeState(boolean)->includeUuids(boolean)->sync();
ParameterTypeRequiredDefaultDescription
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.

Basic Usage

Get a list of UUIDs subscribed to channel

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) {
show all 18 lines

Response

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.

PNHereNowChannelData

MethodTypeDescription
getChannelName()StringChannel name.
getOccupancy()IntegerOccupancy of the channel.
getOccupants()ArrayA array of PNHereNowOccupantData, see PNHereNowOccupantData for more details.

PNHereNowOccupantData

MethodTypeDescription
getUuid()StringUUID of the user.
getState()ArrayState of the user.

Other Examples

Returning State

Requires Presence add-on

This method requires that the Presence add-on is enabled for your key in the Admin Portal. Read the support page on enabling add-on features on your keys.

$result = $pubnub->hereNow()
->channels("my_channel")
->includeUuids(true)
->includeState(true)
->sync();
Example Response
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(
show all 30 lines

Return Occupancy Only

Requires Presence add-on

This method requires that the Presence add-on is enabled for your key in the Admin Portal. Read the support page on enabling add-on features on your 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();
Example Response
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(
show all 30 lines

Here Now for Channel Groups

$pubnub->hereNow()
->channelGroups(["cg1", "cg2", "cg3"])
->includeUuids(true)
->includeState(true)
->sync();
Example Response
(
[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] =>
show all 28 lines

Where Now

Requires Presence add-on

This method requires that the Presence add-on is enabled for your key in the Admin Portal. Read the support page on enabling add-on features on your keys.

Description

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.

Timeout events

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.

Method(s)

To call whereNow() you can use the following method(s) in the PHP V4 SDK:

$pubnub->whereNow()->uuid(string)->sync();
ParameterTypeRequiredDefaultDescription
uuidStringOptionalUuid of the user we want to spy on.

Basic Usage

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

$result = $pubnub->whereNow()
->sync();

Rest Response from Server

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.

Example Response

{
"channels": [
"lobby",
"game01",
"chat"
]
}

Other Examples

$result = $pubnub->whereNow()
->uuid("his-uuid")
->sync();

User State

Requires Presence add-on

This method requires that the Presence add-on is enabled for your key in the Admin Portal. Read the support page on enabling add-on features on your keys.

Description

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.

Method(s)

Set State:

$pubnub->setState()->channels(string|array)->channelGroups(string|array)->state(array)->sync();
ParameterTypeRequiredDescription
channelsString|ArrayOptionalchannels to set state.
channelGroupsString|ArrayOptionalchannel groups to set state.
stateArrayOptionalState to set.

Get State:**

$pubnub->getState()->channels(string|array)->channelGroups(string|array)->uuid(string)->sync();
ParameterTypeRequiredDescription
channelsString|ArrayOptionalchannels to get state.
channelGroupsString|ArrayOptionalchannel groups to get state.
uuidStringOptionaluuid

Basic Usage

Set State

$pubnub->setState()
->channels(["ch1", "ch2", "ch3"])
->state(["age" => 30])
->sync();

Get State

$pubnub->getState()
->channels(["ch1", "ch2", "ch3"])
->sync();

Response

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.

Other Examples

Set state for channels in a channel group

$pubnub->setState()
->channelGroups(["gr1", "gr2", "gr3"])
->state(["age" => 30])
->sync();
Last updated on