Presence API for PHP 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 PHP SDK:
$pubnub->hereNow()
->channels(string|array)
->channelGroups(string|array)
->includeState(boolean)
->includeUuids(boolean)
->sync();
Parameter | Description |
---|---|
channels Type: String|Array Default: n/a | The channels to get the here now details. |
channelGroups Type: String|Array Default: n/a | The channel groups to get the here now details. Wildcards are not supported. |
includeState Type: Boolean Default: false | If true , the response will include the presence states of the users for channels /channelGroups . |
includeUuids Type: Boolean Default: true | If true , the response will include the UUIDs of the connected clients. |
Sample code
Get a list of UUIDs subscribed to channel
Reference code
Response
The hereNow()
operation returns a PNHereNowResult
which contains the following fields:
Method | Description |
---|---|
getTotalChannels() Type: Integer | Total Channels . |
getTotalOccupancy() Type: Integer | Total Occupancy . |
getChannels() Type: Array | A array with values of PNHereNowChannelData for each channel . See PNHereNowChannelData for more details. |
PNHereNowChannelData
Method | Description |
---|---|
getChannelName() Type: String | Channel name. |
getOccupancy() Type: Integer | Occupancy of the channel . |
getOccupants() Type: Array | A array of PNHereNowOccupantData , see PNHereNowOccupantData for more details. |
PNHereNowOccupantData
Method | Description |
---|---|
getUuid() Type: String | UUID of the user. |
getState() Type: Array | State of the user. |
Other examples
Returning 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.
$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 linesReturn occupancy only
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.
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 linesHere 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 linesWhere 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 whereNow()
you can use the following method(s) in the PHP SDK:
$pubnub->whereNow()
->uuid(string)
->sync();
Parameter | Description |
---|---|
uuid Type: String Default: n/a | Uuid of the user we want to spy on. |
Sample code
$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 auuid
is subscribed to.
Example response
{
"channels": [
"lobby",
"game01",
"chat"
]
}
Other examples
$result = $pubnub->whereNow()
->uuid("his-uuid")
->sync();
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)
$pubnub->setState()
->channels(string|array)
->channelGroups(string|array)
->state(array)
->sync();
Parameter | Description |
---|---|
channels Type: String|Array | channels to set state . |
channelGroups Type: String|Array | channel groups to set state . |
state Type: Array | State to set. |
$pubnub->getState()
->channels(string|array)
->channelGroups(string|array)
->uuid(string)
->sync();
Parameter | Description |
---|---|
channels Type: String|Array | channels to get state . |
channelGroups Type: String|Array | channel groups to get state . |
uuid Type: String | uuid |
Sample code
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:
Method | Description |
---|---|
setState() Type: Array | Array of UUIDs and the user states. |
The getState()
operation returns a PNGetStateResult
which contains the following fields:
Method | Description |
---|---|
getChannels() Type: Array | Array 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();