UnityPubNub Unity SDK “3.7.9”

 
For iOS 9.0 Compatibility, you need to set SSL as true when initiating the Pubnub instance. Please see this on how to enable SSL.




Add the Scripts and ThirdParty folders from PubNub's Unity repo to your Unity projects Assets folder and then use this
using PubNubMessaging.Core;
Pubnub pubnub = new Pubnub( "demo", "demo");

pubnub.Subscribe<string>(
	"my_channel", 
	DisplaySubscribeReturnMessage, 
	DisplaySubscribeConnectStatusMessage, 
	DisplayErrorMessage); 

void DisplaySubscribeConnectStatusMessage(string connectMessage)
{
    UnityEngine.Debug.Log("SUBSCRIBE CONNECT CALLBACK");
    pubnub.Publish<string>(
		"my_channel", 
		"Hello from the PubNub C# SDK", 
		DisplayReturnMessage, 
		DisplayErrorMessage); 
}

void DisplaySubscribeReturnMessage(string result) {
    UnityEngine.Debug.Log("SUBSCRIBE REGULAR CALLBACK:"); 
    UnityEngine.Debug.Log(result);
    if (!string.IsNullOrEmpty(result) && !string.IsNullOrEmpty(result.Trim()))
            {
                List<object> deserializedMessage = pubnub.JsonPluggableLibrary.DeserializeToListOfObject(result);
                if (deserializedMessage != null && deserializedMessage.Count > 0)
                {
                    object subscribedObject = (object)deserializedMessage[0];
                    if (subscribedObject != null)
                    {
                        //IF CUSTOM OBJECT IS EXCEPTED, YOU CAN CAST THIS OBJECT TO YOUR CUSTOM CLASS TYPE
                        string resultActualMessage = pubnub.JsonPluggableLibrary.SerializeToJsonString(subscribedObject);
                   }
                }
            }
}

void DisplayErrorMessage(PubnubClientError pubnubError)
{
    UnityEngine.Debug.Log(pubnubError.StatusCode);
}

void DisplayReturnMessage(string result)
{
    UnityEngine.Debug.Log("PUBLISH STATUS CALLBACK");
    UnityEngine.Debug.Log(result);
}
In addition to the Hello World sample code, we also provide some copy and paste snippets of common API functions:
Instantiate a new Pubnub instance. Only the subscribeKey is mandatory. Also include publishKey if you intend to publish from this instance, and the secretKey if you wish to perform PAM administrative operations from this Unity instance.
 

It is not a best practice to include the secret key in client-side code for security reasons.

When you init with secretKey, you get root permissions for the Access Manager. With this feature you don't have to grant access to your servers to access channel data. The servers get all access on all channels.

Pubnub pubnub = new Pubnub("demo", "demo");
Call Time to verify the client connectivity to the origin:
pubnub.Time<string>(
    DisplayReturnMessage,
    DisplayErrorMessage
);
void DisplayReturnMessage(string result)
{
    UnityEngine.Debug.Log("TIME CALLBACK");
    UnityEngine.Debug.Log(result);
}
void DisplayErrorMessage(PubnubClientError pubnubError)
{
    UnityEngine.Debug.Log(pubnubError.StatusCode);
}
Subscribe (listen on) a channel (it's async!):
pubnub.Subscribe<string>(
   "my_channel",
    DisplaySubscribeReturnMessage,
    DisplaySubscribeConnectStatusMessage,
    DisplayErrorMessage
);
void DisplaySubscribeReturnMessage(string result) {
    UnityEngine.Debug.Log("SUBSCRIBE REGULAR CALLBACK:"); 
    UnityEngine.Debug.Log(result);
    if (!string.IsNullOrEmpty(result) && !string.IsNullOrEmpty(result.Trim()))
            {
                List<object> deserializedMessage = pubnub.JsonPluggableLibrary.DeserializeToListOfObject(result);
                if (deserializedMessage != null && deserializedMessage.Count > 0)
                {
                    object subscribedObject = (object)deserializedMessage[0];
                    if (subscribedObject != null)
                    {
                        //IF CUSTOM OBJECT IS EXCEPTED, YOU CAN CAST THIS OBJECT TO YOUR CUSTOM CLASS TYPE
                        string resultActualMessage = pubnub.JsonPluggableLibrary.SerializeToJsonString(subscribedObject);
                   }
                }
            }
}
void DisplaySubscribeConnectStatusMessage(string result)
{
    UnityEngine.Debug.Log("SUBSCRIBE CONNECT CALLBACK");
}
void DisplayErrorMessage(PubnubClientError pubnubError) {
    UnityEngine.Debug.Log(pubnubError.StatusCode);
}
Publish a message to a channel:
pubnub.Publish<string>(
      "my_channel",
      "Hello from the PubNub C# SDK!",
      DisplayReturnMessage,
      DisplayErrorMessage
);
void DisplayReturnMessage(string result)
{
    UnityEngine.Debug.Log("PUBLISH STATUS CALLBACK");
    UnityEngine.Debug.Log(result);
}
void DisplayErrorMessage(PubnubClientError pubnubError) {
    UnityEngine.Debug.Log(pubnubError.StatusCode);
}
Get occupancy of who's here now on the channel by UUID:
Requires that the Presence add-on is enabled for your key. How do I enable add-on features for my keys? - see http://www.pubnub.com/knowledge-base/discussion/644/how-do-i-enable-add-on-features-for-my-keys
pubnub.HereNow <string>(
    "my_channel",
    DisplayReturnMessage,
    DisplayErrorMessage
);
Subscribe to realtime Presence events, such as join, leave, and timeout, by UUID. Setting the presence attribute to a callback will subscribe to presents events on my_channel:
Requires that the Presence add-on is enabled for your key. How do I enable add-on features for my keys? - see http://www.pubnub.com/knowledge-base/discussion/644/how-do-i-enable-add-on-features-for-my-keys
pubnub.Presence<string>(
    "my_channel",
    DisplayPresenceReturnMessage,
    DisplayPresenceConnectStatusMessage,
    DisplayErrorMessage1);

void DisplayPresenceReturnMessage(string result) {
	UnityEngine.Debug.Log("PRESENCE REGULAR CALLBACK:");
	UnityEngine.Debug.Log(result);
}
void DisplayPresenceConnectStatusMessage(string result) {
	UnityEngine.Debug.Log("PRESENCE CONNECT CALLBACK:");
	UnityEngine.Debug.Log(result);
}
void DisplayErrorMessage1(PubnubClientError pubnubError) {
	UnityEngine.Debug.Log(pubnubError.StatusCode);
}
Retrieve published messages from archival storage:
Requires that the Storage and Playback add-on is enabled for your key. How do I enable add-on features for my keys? - see http://www.pubnub.com/knowledge-base/discussion/644/how-do-i-enable-add-on-features-for-my-keys
// retrieve last 100 messages

pubnub.DetailedHistory<string>(
	"history_channel",
	100,
	DisplayReturnMessage,
	DisplayErrorMessage);
void DisplayReturnMessage(string result)
{
	UnityEngine.Debug.Log(result);
	if (!string.IsNullOrEmpty(result) && !string.IsNullOrEmpty(result.Trim()))
	{
		List<object> deserializedMessage = pubnub.JsonPluggableLibrary.DeserializeToListOfObject(result);
		if (deserializedMessage != null && deserializedMessage.Count > 0)
		{
			object[] message = (from item in deserializedMessage select item as object).ToArray ();
			if ((message != null)&&(message.Length >= 0))
			{
				IList<object> enumerable = message [0] as IList<object>;
				foreach (object item in enumerable)
				{
					UnityEngine.Debug.Log (string.Format ("Message: {0}", item.ToString()));
					//IF CUSTOM OBJECT IS EXCEPTED, YOU CAN CAST THIS OBJECT TO YOUR CUSTOM CLASS TYPE
				}
			}
		}
	}
}
void DisplayErrorMessage(PubnubClientError pubnubError)
{
    UnityEngine.Debug.Log(pubnubError.StatusCode);
}
Stop subscribing (listening) to a channel.
pubnub.Unsubscribe<string>(
      "my_channel",
      DisplayReturnMessage,
      DisplaySubscribeConnectStatusMessage,
      DisplaySubscribeDisconnectStatusMessage,
      DisplayErrorMessage
);