Presence API for PubNub Unity SDK

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

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

pubnub.HereNow().Channels(List<string>).ChannelGroups(List<string>).IncludeState(bool).IncludeUUIDs(bool).QueryParam(Dictionary<string,string>).Async(Command)
ParameterTypeRequiredDefaultDescription
ChannelsList<string>OptionalThe channels to get the here now details.
ChannelGroupsList<string>OptionalThe channelGroups to get the here now details.
IncludeStateboolOptionalfalseIf true, the response will include the presence states of the users for channels/channelGroups
IncludeUUIDsboolOptionalfalseIf true, the response will include the UUIDs of the connected clients.
QueryParamDictionary<string, string>OptionalQueryParam accepts a Dictionary object, the keys and values are passed as the query string parameters of the URL called by the API.
AsyncCommandYesCommand that will execute HereNow.

Basic Usage

Get a list of UUIDs subscribed to channel

pubnub.HereNow()
.Channels(new List<string>(){
"my_channel"
})
.ChannelGroups(new List<string>() {
"my_channel_group"
})
.IncludeState(true)
.IncludeUUIDs(true)
.Async((result, status) => {
if (status.Error) {
Debug.Log(string.Format("HereNow Error: {0} {1} {2}", status.StatusCode, status.ErrorData, status.Category));
} else {
Debug.Log(string.Format("DateTime {0}, In Example, Channels: {1} {2}", DateTime.UtcNow , result.TotalChannels, result.TotalOccupancy));
}
show all 17 lines

Returns

The HereNow() operation returns a PNHereNowResult which contains the following operations:

MethodTypeDescription
TotalChannelsintTotal Channels.
TotalOccupancyintTotal Occupancy.
ChannelsDictionary<string, PNHereNowChannelData>A dictionary with values of PNHereNowChannelData for each channel. See PNHereNowChannelData for more details.

PNHereNowChannelData

MethodTypeDescription
ChannelNamestringChannel name.
OccupancyintOccupancy of the channel.
OccupantsList<PNHereNowOccupantData>A list of PNHereNowOccupantData, see PNHereNowOccupantData for more details.

PNHereNowOccupantData

MethodTypeDescription
UUIDstringUUIDs of the user.
StateobjectState of the user.

Other Examples

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.

pubnub.HereNow()
.Channels(new List<string> (){"my_channel"})
.IncludeState(true)
.IncludeUUIDs(true)
.Async((result, status) => {
if(status.Error){
Debug.Log (string.Format("HereNow Error: {0} {1} {2}", status.StatusCode, status.ErrorData, status.Category));
} else {
Debug.Log (string.Format("DateTime {0}, In Example, Channels: {1} {2}", DateTime.UtcNow , result.TotalChannels, result.TotalOccupancy));
}
Debug.Log (status.Error);
});

Example Response

if(result.Channels!= null){
foreach (KeyValuePair<string, PNHereNowChannelData> kvp in result.Channels){
if(kvp.Value != null){
PNHereNowChannelData hereNowChannelData = kvp.Value as PNHereNowChannelData;
if(hereNowChannelData != null){
List<PNHereNowOccupantData> hereNowOccupantData = hereNowChannelData.Occupants as List<PNHereNowOccupantData>;
if(hereNowOccupantData != null){
foreach(PNHereNowOccupantData pnHereNowOccupantData in hereNowOccupantData){
if(pnHereNowOccupantData.State != null){
Debug.Log ("in HereNow channel State: " + pnHereNowOccupantData.State.ToString());
}
if(pnHereNowOccupantData.UUID != null){
Debug.Log ("in HereNow channel UUID: " + pnHereNowOccupantData.UUID.ToString());
}
}
show all 28 lines
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:

pubnub.HereNow()
.Channels(new List<string> (){"my_channel"})
.IncludeState(false)
.IncludeUUIDs(false)
.Async((result, status) => {
if(status.Error){
Debug.Log (string.Format("HereNow Error: {0} {1} {2}", status.StatusCode, status.ErrorData, status.Category));
} else {
Debug.Log (string.Format("DateTime {0}, In Example, Channels: {1} {2}", DateTime.UtcNow , result.TotalChannels, result.TotalOccupancy));
}
Debug.Log (status.Error);
});

Example Response

if(status.Error){
Debug.Log (string.Format("HereNow Error: {0} {1} {2}", status.StatusCode, status.ErrorData, status.Category));
} else {
Debug.Log (string.Format("DateTime {0}, Channels: {1} {2}", DateTime.UtcNow , result.TotalChannels, result.TotalOccupancy));
}

Debug.Log (status.Error);

Here Now for Channel Groups

pubnub.HereNow()
.ChannelGroups(new List<string> (){"my_channel_group"})
.IncludeState(true)
.IncludeUUIDs(true)
.Async((result, status) => {
if(status.Error){
Debug.Log (string.Format("HereNow Error: {0} {1} {2}", status.StatusCode, status.ErrorData, status.Category));
} else {
Debug.Log (string.Format("DateTime {0}, Channels: {1} {2}", DateTime.UtcNow , result.TotalChannels, result.TotalOccupancy));
}
Debug.Log (status.Error);
});

Example Response

if(status.Error){
Debug.Log (string.Format("HereNow Error: {0} {1} {2}", status.StatusCode, status.ErrorData, status.Category));
} else {
Debug.Log (string.Format("DateTime {0}, Channels: {1} {2}", DateTime.UtcNow , result.TotalChannels, result.TotalOccupancy));
}

Debug.Log (status.Error);

Where Now

Requires Message Persistence add-on

This method requires that the Message Persistence 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 Unity V4 SDK:

pubnub.WhereNow().UUID(string).QueryParam(Dictionary<string,string>).Async(Command)
ParameterTypeRequiredDescription
UUIDstringOptionalUUID of the user we want to spy on.
QueryParamDictionary<string, string>OptionalQueryParam accepts a Dictionary object, the keys and values are passed as the query string parameters of the URL called by the API.
AsyncCommandYesExecute as Async.

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

pubnub.WhereNow()
.Async((result, status) => {
if (result.Channels!=null) {
Debug.Log(string.Join(",", result.Channels.ToArray());
}
});

Returns

The WhereNow() operation returns a PNWhereNowResult which contains the following operations:

MethodTypeDescription
Channels()List<string>The list of channels where the UUID is present.

Other Examples

Obtain information about the current list of channels of some other UUID

pubnub.WhereNow()
.Uuid("some-other-uuid")
.Async ((result, status) => {
if(result.Channels!=null){
Debug.Log(result.Channels.ToArray());
}
});

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 Generic Dictionary object(Dictionary<string, object>) of key/value pairs.

Method(s)

Set State

pubnub.SetPresenceState().Channels(List<string>).ChannelGroups(List<string>).State(Dictionary<string, object>).UUID(string).QueryParam(Dictionary<string,string>).Async()
ParameterTypeRequiredDescription
ChannelsList<string>OptionalChannels to set state.
ChannelGroupsList<string>OptionalChannelGroups to set state.
StateDictionary<string, object>OptionalState to set.
UUIDstringOptionalSet state for specific UUID.
QueryParamDictionary<string, string>OptionalQueryParam accepts a Dictionary object, the keys and values are passed as the query string parameters of the URL called by the API.
AsyncPNCallbackYesPNCallback of type PNSetStateResult.

Get State

pubnub.GetPresenceState().Channels(List<string>).ChannelGroups(List<string>).UUID(string).QueryParam(Dictionary<string,string>).Async()
ParameterTypeRequiredDescription
ChannelsList<string>OptionalChannels to get state.
ChannelGroupsList<string>OptionalChannelGroups to get state.
UUIDstringOptionalGet state for specific UUID.
QueryParamDictionary<string, string>OptionalQueryParam accepts a Dictionary object, the keys and values are passed as the query string parameters of the URL called by the API.
AsyncPNCallbackYesPNCallback of type PNGetStateResult.

Basic Usage

Set State

pubnub.SetPresenceState()
.Channels(new List<string> (){"my_channel"})
.ChannelGroups(new List<string> (){"my_channel_groups"})
.State(state)
.Async((result, status) => {
if(status.Error){
Debug.Log (string.Format("In Example, SetPresenceState Error: {0} {1} {2}", status.StatusCode, status.ErrorData, status.Category));
} else {
Debug.Log (string.Format("DateTime {0}, In Example SetPresenceState, result:", DateTime.UtcNow));
if(result != null){
if(result.StateByChannels!= null){
foreach (KeyValuePair<string, object> key in dict){
Debug.Log(string.Format("Channel:{0}, State:{1}", key.Key, pubnub.JsonLibrary.SerializeToJsonString(key.Value)));
}
}
show all 18 lines

Get State

pubnub.GetPresenceState()
.Channels(new List<string> (){"my_channel"})
.ChannelGroups(new List<string> (){"my_channel_group"})
.Async((result, status) => {

if(status.Error){
Debug.Log (string.Format(" GetPresenceState Error: {0} {1} {2}", status.StatusCode, status.ErrorData, status.Category));
} else {
Debug.Log (string.Format("DateTime {0}, In Example GetPresenceState, result:", DateTime.UtcNow));
if(result != null){
if(result.StateByChannels!= null){
foreach (KeyValuePair<string, object> key in dict){
Debug.Log(string.Format("Channel:{0}, State:{1}", key.Key, pubnub.JsonLibrary.SerializeToJsonString(key.Value)));
}
}
show all 18 lines

Returns

The SetPresenceState() operation returns a PNSetStateResult which contains the following property:

MethodTypeDescription
StateByChannelsDictionary<string, object>Dictionary of UUIDs and the user states.

The GetPresenceState() operation returns a PNGetStateResult which contains the following property:

MethodTypeDescription
StateByChannelsDictionary<string, object>Dictionary of UUIDs and the user states.
Last updated on