Presence API for Unity 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.
Presence event modes
Presence mode indicates when presence events are triggered for that channel. It can be either:
- Announce Mode
- Interval Mode
The mode is dependant on the occupancy count.
This setting is controlled through Admin Portal. To learn more about this, visit Presence documentation.
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 Unity SDK:
1pubnub.HereNow()
2 .Channels(Array)
3 .ChannelGroups(Array)
4 .IncludeState(bool)
5 .IncludeUUIDs(bool)
6 .QueryParam(Dictionary<string,object>)
7 .Execute(System.Action<PNHereNowResult, PNStatus>)
| Parameter | Description |
|---|---|
ChannelsType: Array | The Channels to get the here now details. |
ChannelGroupsType: Array | The ChannelGroups to get the here now details. Wildcards are not supported. |
IncludeStateType: bool | If true, the response will include the presence states of the users for Channels/ChannelGroups. |
IncludeUUIDsType: bool | If true, the response will include the UUIDs of the connected clients. |
QueryParamType: Dictionary <string, object> | Dictionary object to pass name/value pairs as query string params with PubNub URL request for debug purpose. |
AsyncType: | PNCallback of type PNHereNowResult. |
Execute *Type: System.Action | System.Action of type PNHereNowResult. |
ExecuteAsyncType: None | Returns Task<PNResult<PNHereNowResult>>. |
Sample code
Get a list of UUIDs subscribed to channel
Reference code
1
Returns
The HereNow() operation returns a PNResult<PNHereNowResult> which contains the following properties:
| Property Name | Type | Description |
|---|---|---|
Result | PNHereNowResult | Returns a PNHereNowResult object. |
Status | PNStatus | Returns a PNStatus object. |
PNHereNowResult contains the following properties:
| Property Name | Type | Description |
|---|---|---|
TotalChannels | int | Total Channels. |
TotalOccupancy | int | Total Occupancy. |
Channels | Dictionary<string, PNHereNowChannelData> | A map with values of PNHereNowChannelData for each channel. See PNHereNowChannelData for more details. |
PNHereNowChannelData
| Property Name | Type | Description |
|---|---|---|
ChannelName | string | Channel name. |
Occupancy | int | Occupancy of the channel. |
Occupants | List<PNHereNowOccupantData> | A list of PNHereNowOccupantData, see PNHereNowOccupantData for more details. |
PNHereNowOccupantData
| Property Name | Type | Description |
|---|---|---|
Uuid | string | UUIDs of the user. |
State | object | State of the user. |
Other examples
Get a list of UUIDs subscribed to channel synchronously
1
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.
1
Example response
1{
2 "status" : 200,
3 "message" : "OK",
4 "service" : "Presence",
5 "uuids" : [
6 {
7 "uuid" : "myUUID0"
8 },
9 {
10 "state" : {
11 "abcd" : {
12 "age" : 15
13 }
14 },
15 "uuid" : "myUUID1"
show all 38 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:
1
Example response
1{
2 "status": 200,
3 "message": "OK",
4 "payload": {
5 "channels": {
6 "81d8d989-b95f-443c-a726-04fac323b331": {
7 "uuids": [ "70fc1140-22b5-4abc-85b2-ff8c17b24d59" ],
8 "occupancy": 1
9 },
10 "81b7a746-d153-49e2-ab70-3037a75cec7e": {
11 "uuids": [ "91363e7f-584b-49cc-822c-52c2c01e5928" ],
12 "occupancy": 1
13 },
14 "c068d15e-772a-4bcd-aa27-f920b7a4eaa8": {
15 "uuids": [ "ccfac1dd-b3a5-4afd-9fd9-db11aeb65395" ],
show all 23 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 Unity SDK:
1pubnub.WhereNow()
2 .Uuid(string)
3 .QueryParam(Dictionary<string,object>)
4 .Execute(System.Action<PNWhereNowResult, PNStatus>)
| Parameter | Description |
|---|---|
Uuid *Type: string | Uuid. |
QueryParamType: Dictionary <string, object> | Dictionary object to pass name/value pairs as query string params with PubNub URL request for debug purpose. |
AsyncType: | PNCallback of type PNWhereNowResult. |
Execute *Type: System.Action | System.Action of type PNWhereNowResult. |
ExecuteAsyncType: None | Returns Task<PNResult<PNWhereNowResult>>. |
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
Returns
The WhereNow() operation returns a PNResult<PNWhereNowResult> which contain the following properties:
| Property Name | Type | Description |
|---|---|---|
Result | PNWhereNowResult | Returns a PNWhereNowResult object. |
Status | PNStatus | Returns a PNStatus object. |
PNWhereNowResult contains the following properties:
| Property Name | Type | Description |
|---|---|---|
Channels | List<string> | The list of channels where the UUID is present. |
Other examples
Get a list of channels synchronously
1
Obtain information about the current list of channels of some other UUID
1
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.
The state API is used to set/get key/value pairs specific to a subscriber Uuid.
State information is supplied as a Generic Dictionary object(Dictionary<string, object>) of key/value pairs.
Method(s)
Set state
1pubnub.SetPresenceState()
2 .Channels(Array)
3 .ChannelGroups(Array)
4 .State(Dictionary<string, object>)
5 .Uuid(string)
6 .QueryParam(Dictionary<string,object>)
7 .Execute(System.Action<PNSetStateResult, PNStatus>)
| Parameter | Description |
|---|---|
ChannelsType: Array | Channels to set state. |
ChannelGroupsType: Array | ChannelGroups to set state. |
StateType: Dictionary <string, object> | State to set. |
Uuid *Type: string | Uuid |
QueryParamType: Dictionary <string, object> | Dictionary object to pass name/value pairs as query string params with PubNub URL request for debug purpose. |
AsyncType: | PNCallback of type PNSetStateResult. |
Execute *Type: System.Action | System.Action of type PNSetStateResult. |
ExecuteAsyncType: None | Returns Task<PNResult<PNSetStateResult>>. |
Get state
1pubnub.GetPresenceState()
2 .Channels(Array)
3 .ChannelGroups(Array)
4 .Uuid(string)
5 .QueryParam(Dictionary<string,object>)
6 .Execute(System.Action<PNGetStateResult, PNStatus>)
| Parameter | Description |
|---|---|
ChannelsType: Array | Channel name to fetch the state. |
ChannelGroupsType: Array | ChannelGroups name to fetch the state. |
Uuid *Type: string | Uuid |
QueryParamType: Dictionary <string, object> | Dictionary object to pass name/value pairs as query string params with PubNub URL request for debug purpose. |
AsyncType: | PNCallback of type PNGetStateResult. |
Execute *Type: System.Action | System.Action of type PNGetStateResult. |
ExecuteAsyncType: None | Returns Task<PNResult<PNGetStateResult>>. |
Sample code
Set state
1
Get state
1
Returns
The SetPresenceState() operation returns a PNResult<PNSetStateResult> which contains the following properties:
| Property Name | Type | Description |
|---|---|---|
Result | PNGetStateResult | Returns a PNSetStateResult object. |
Status | PNStatus | Returns a PNStatus object. |
PNSetStateResult contains the following property:
| Property Name | Type | Description |
|---|---|---|
State | Dictionary<string, object> | Dictionary of UUIDs and the user states. |
The GetPresenceState() operation returns a PNResult<PNGetStateResult> which contains the following properties:
| Property Name | Type | Description |
|---|---|---|
Result | PNGetStateResult | Returns a PNGetStateResult object. |
Status | PNStatus | Returns a PNStatus object. |
PNGetStateResult contains the following property:
| Property Name | Type | Description |
|---|---|---|
StateByUUID | Dictionary<string, object> | Dictionary of UUIDs and the user states. |
Other examples
Set state synchronously
1
Get state synchronously
1
Set state for channels in channel group
1
The above code would return the following response to the client:
1{
2 first : "Robert",
3 last : "Plant",
4 age : 59,
5 region : "UK"
6}