UnityUnity V4 Presence API Reference for Realtime Apps

Go to Publish & Subscribe


 

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

If you have questions about the PubNub for Unity 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 Unity V4 SDK:
  1. ParameterTypeRequiredDefaultsDescription
    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.
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));
		}
		Debug.Log(status.Error);
	});
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.
MethodTypeDescription
ChannelNamestringChannel name.
OccupancyintOccupancy of the channel.
OccupantsList<PNHereNowOccupantData>A list of PNHereNowOccupantData, see PNHereNowOccupantData for more details.
MethodTypeDescription
UUIDstringUUIDs of the user.
StateobjectState 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.
    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);
        });
    
    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());
                            }
                        }
                    } else {
                        Debug.Log ("in HereNow hereNowOccupantData null");
                    }
                } else {
                    Debug.Log ("in HereNow hereNowChannelData null");
                }
            } else {
                Debug.Log ("in HereNow kvp null");
            }
        }
    } else {
        Debug.Log ("in HereNow channels null");   
    }
  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:
    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);
        });
    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);
  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:
    pubnub.HereNow()
        .Channels(new List<string> (){"my_channel"})
        .IncludeState(false)
        .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);
        });
    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());
                            }
                        }
                    } else {
                        Debug.Log ("in HereNow hereNowOccupantData null");
                    }
                } else {
                    Debug.Log ("in HereNow hereNowChannelData null");
                }
            } else {
                Debug.Log ("in HereNow kvp null");
            }
        }
    } else {
        Debug.Log ("in HereNow channels null");   
    }
    
  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
    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}, Channels: {1} {2}", DateTime.UtcNow , result.TotalChannels, result.TotalOccupancy));
            }
            Debug.Log (status.Error);
        });
    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());
                            }
                        }
                    } else {
                        Debug.Log ("in HereNow hereNowOccupantData null");
                    }
                } else {
                    Debug.Log ("in HereNow hereNowChannelData null");
                }
            } else {
                Debug.Log ("in HereNow kvp null");
            }
        }
    } else {
        Debug.Log ("in HereNow channels null");   
    }
    
  5. 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);
        });
    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);
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 Unity V4 SDK:
  1. 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.
You simply need to define the uuid and the callback function to be used to send the data to as in the example below.
pubnub.WhereNow()
    .Async((result, status) => {
        if (result.Channels!=null) {
            Debug.Log(string.Join(",", result.Channels.ToArray());
        } 
    });
The WhereNow() operation returns a PNWhereNowResult which contains the following operations:
MethodTypeDescription
Channels()List<string>The list of channels where the UUID is present.
pubnub.WhereNow()
    .Uuid("some-other-uuid")
    .Async ((result, status) => {
        if(result.Channels!=null){
            Debug.Log(result.Channels.ToArray());
        } 
    });
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 Generic Dictionary object(Dictionary<string, object>) of key/value pairs.
  1. 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.
  1. 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.
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)));
                    }
                }
            }
        }
    });
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)));
                    }
                }
            }
        }
    });
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.

Go to Channel Groups