UnityUnity V4 Configuration API Reference for Realtime Apps

 

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.

Unity V4 complete API reference for building Realtime Applications on PubNub, including basic usage and sample code

PNConfiguration instance is storage for user-provided information which describe further PubNub client behaviour. Configuration instance contain additional set of properties which allow to perform precise PubNub client configuration.
To create configuration instance you can use the following function in the Unity V4 SDK:
  1. PropertiesTypeRequiredDefaultsDescription
    SubscribeKeystringYesSubscribeKey from admin panel.
    PublishKeystringOptionalPublishKey from admin panel (only required if publishing).
    SecretKeystringOptionalSecretKey (only required for access operations, recommended to be used on secure servers due to security reasons ).
    CipherKeystringOptionalIf cipher is passed, all communications to/from PubNub will be encrypted.
    UUIDstringOptionalSDK generated uuidUUID to be used as a device identifier, a default UUID is generated if not passed.
    LogVerbosityPNLogVerbosityOptionalPNLogVerbosity.NONESet PNLogVerbosity.BODY to enable debugging. To disable debugging use the option PNLogVerbosity.NONE
    AuthKeystringOptionalNot setIf Access Manager is utilized, client will use this authKey in all restricted requests.
    SecureboolOptionaltrueUse SSL.
    MessageQueueOverflowCountintOptional100When the limit is exceeded by the number of messages received in a single subscribe request, a status event PNRequestMessageCountExceededCategory is fired.
    SubscribeTimeoutintOptional310

    The subscribe request timeout.


    The value is in seconds.

    NonSubscribeTimeoutintOptional10

    For non subscribe operations (publish, herenow, etc), how long to wait to connect to PubNub before giving up with a connection timeout error.


    The value is in seconds.

    FilterExpressionstringOptionalNot setUsed to subscribe with a custom filter expression.
    HeartbeatNotificationOptionPNHeartbeatNotificationOptionsOptionalPNHeartbeatNotificationOptions.FAILURES

    Heartbeat notifications, by default, the SDK will alert on failed heartbeats (equivalent to: PNHeartbeatNotificationOptions.FAILURES).

    Other options such as all heartbeats (PNHeartbeatNotificationOptions.ALL) or no heartbeats (PNHeartbeatNotificationOptions.NONE) are supported.

    OriginstringOptionalCustom origin if needed.
    ReconnectionPolicyPNReconnectionPolicyOptionalPNReconnectionPolicy.NONE

    Set to PNReconnectionPolicy.LINEAR for automatic reconnects. Use option PNReconnectionPolicy.NONE to disable automatic reconnects.


    Use option PNReconnectionPolicy.EXPONENTIAL to set exponential retry interval.

    PresenceTimeoutintOptional0

    Use to set custom presence server timeout.


    The value is in seconds.

    PresenceIntervalintOptional0

    Used to set custom interval to send the ping back to the server. This is automatically set to a recommended value when PresenceTimeout is set to a custom value.


    The value is in seconds.

    MaximumReconnectionRetriesintOptionalinfinitySets how many times to retry to reconnect before giving up.
    SuppressLeaveEventsboolOptionalWhen true the SDK doesn't send out the leave requests.
PNConfiguration pnConfiguration = new PNConfiguration();
// get subscribeKey from admin panel
pnConfiguration.SubscribeKey = "SubscribeKey";// required
// publishKey from admin panel (only required if publishing)
pnConfiguration.PublishKey = "PublishKey";
// secretKey (only required for access and history delete operations , keep away from Android/iOS)
pnConfiguration.SecretKey = "SecretKey";
// if cipherKey is passed, all communicatons to/from pubnub will be encrypted
pnConfiguration.CipherKey = "CipherKey";
// UUID to be used as a device identifier, a default UUID is generated 
// if not passed
pnConfiguration.UUID = "PubNubUnityExample";
// Enable Debugging
pnConfiguration.LogVerbosity = PNLogVerbosity.BODY; 
// if Access Manager is utilized, client will use this authKey in all restricted requests
pnConfiguration.AuthKey = "authKey";
// use SSL.
pnConfiguration.Secure = true;
// PSV2 feature to subscribe with a custom filter expression
pnConfiguration.FilterExpression = "such=wow";

// heartbeat notifications, by default, the SDK will alert on failed heartbeats.
// other options such as all heartbeats or no heartbeats are supported.
pnConfiguration.HeartbeatNotificationOption = PNHeartbeatNotificationOption.All
pnConfiguration.PresenceTimeout = 60;

pubnub = new PubNub(pnConfiguration);

Download the PubNub Unity package and import it to your Unity project by going to Assets -> Import Package -> Custom Package. Now that your PubNub Unity Package has been imported, you must enable it in the Test Runner. To enable the PubNub Unity Package, go to Window -> General -> Test Runner. Click on the mini drop down menu next to the window close button, and click Enable playmode tests for all assemblies. You will have to restart your Unity editor to finalize these changes. Congrats, you are now all setup to start using the PubNub Unity SDK!

(OR if using the code from the source add files from PubNubUnity/Assets folder to the Assets folder of your project.)

using PubNubAPI;
This function is used for initializing the PubNub Client API context. This function must be called before attempting to utilize any API functionality in order to establish account level credentials such as PublishKey and SubscribeKey.
To Initialize Pubnub you can use the following method(s) in the Unity V4 SDK:
  1. ParameterTypeRequiredDescription
    pnConfigurationPNConfigurationYesGoto Configuration for more details.
PNConfiguration pnConfiguration = new PNConfiguration();
pnConfiguration.SubscribeKey = "my_subkey";
pnConfiguration.PublishKey = "my_pubkey";
pnConfiguration.Secure = true;

pubnub = new PubNub(pnConfiguration);
It returns the Pubnub instance for invoking PubNub APIs like Publish(), Subscribe(), History(), HereNow(), etc.
  1. PNConfiguration pnConfiguration = new PNConfiguration();
    pnConfiguration.SubscribeKey = "my_subkey";
    pnConfiguration.PublishKey = "my_pubkey";
    pnConfiguration.Secure = true;
    
    pubnub = new PubNub(pnConfiguration);
  2. In the case where a client will only read messages and never publish to a channel, you can simply omit the PublishKey when initializing the client:
    PNConfiguration pnConfiguration = new PNConfiguration();
    pnConfiguration.SubscribeKey = "my_subkey";
    
    pubnub = new PubNub(pnConfiguration);
  3. Set a custom UUID to identify your users.
    PNConfiguration pnConfiguration = new PNConfiguration();
    pnConfiguration.SubscribeKey = "my_subkey";
    pnConfiguration.PublishKey = "my_pubkey";
    pnConfiguration.Secure = true;
    pnConfiguration.UUID = "Stephen";
    
    pubnub = new PubNub(pnConfiguration);
  4. This examples demonstrates how to enable PubNub Transport Layer Encryption with SSL. Just initialize the client with Secure set to true. The hard work is done, now the PubNub API takes care of the rest. Just subscribe and publish as usual and you are good to go.

    PNConfiguration pnConfiguration = new PNConfiguration();
    pnConfiguration.SubscribeKey = "my_subkey";
    pnConfiguration.PublishKey = "my_pubkey";
    pnConfiguration.Secure = true;
    
    pubnub = new PubNub(pnConfiguration);
  5. Requires Access Manager add-on XRequires that the Access Manager 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.
     

    Anyone with the SecretKey can grant and revoke permissions to your app. Never let your SecretKey be discovered, and to only exchange it / deliver it securely. Only use the SecretKey on secure server-side platforms.

    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.

    For applications that will administer PAM permissions, the API is initialized with the SecretKey as in the following example:
    PNConfiguration pnConfiguration = new PNConfiguration();
    pnConfiguration.SubscribeKey = "my_subkey";
    pnConfiguration.PublishKey = "my_pubkey";
    pnConfiguration.SecretKey = "my_secretkey";
    pnConfiguration.Secure = true;
     
    pubnub = new PubNub(pnConfiguration);
    Now that the pubnub object is instantiated the client will be able to access the PAM functions. The pubnub object will use the SecretKey to sign all PAM messages to the PubNub Network.
These functions are used to set/get a user ID on the fly.
To set/get UUID you can use the following method(s) in Unity V4 SDK
  1. PropertyTypeRequiredDescription
    UUIDstringYesUuid to be used as a device identifier, a default Uuid is generated if not passed.
  2. A property in the PNConfiguration class.

PNConfiguration pnConfiguration = new PNConfiguration();
pnConfiguration.UUID = "customUUID";
pubnub = new PubNub(pnConfiguration);
string UUID = pnConfiguration.UUID;
Setter and getter for users auth key.
  1. ParameterTypeRequiredDescription
    AuthKeystringYesIf Access Manager is utilized, client will use this AuthKey in all restricted requests.
  2. A property in the PNConfiguration class.

PNConfiguration pnConfiguration = new PNConfiguration();
pnConfiguration.AuthKey = "authKey";
pubnub = new PubNub(pnConfiguration);
string AuthKey = pnConfiguration.AuthKey;
Get Auth key returns the current authentication key.
Requires Stream Controller add-on XRequires that the Stream Controller 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.
To set/get filters you can use the following methods.
  1. PropertyTypeRequiredDescription
    FilterExpressionstringYesPSV2 feature to Subscribe with a custom filter expression.
  2. A property in the PNConfiguration class.

PNConfiguration pnConfiguration = new PNConfiguration();
pnConfiguration.FilterExpression = "such=wow";
pubnub = new PubNub(pnConfiguration);
string FilterExpression = pnConfiguration.FilterExpression;

Go to Publish & Subscribe