Feedback

C#C# V4 Configuration API Reference for Realtime Apps

 

These docs are for PubNub 4.0 for C# which is our latest and greatest! For the docs of the 3.x versions of the SDK, please check the links: C#, Windows 8, Windows 8.1, ASP.Net, Windows Phone 8, Windows Phone 8.1, Xamarin.iOS, Xamarin.Android, Xamarin.Mac and C# PCL.

If you have questions about the PubNub for C# SDK, please contact us at support@pubnub.com.

C# 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 C# V4 SDK:
  1. PropertiesTypeRequiredDescription
    SubscribeKeystringYesSubscribeKey from admin panel.
    PublishKeystringOptionalPublishKey from admin panel (only required if publishing).
    SecretKeystringOptionalSecretKey (only required for access operations, keep away from Android).
    CipherKeystringOptionalIf cipher is passed, all communicatons to/from PubNub will be encrypted.
    UuidstringOptionalUuid to be used as a device identifier, a default Uuid is generated if not passed.
    LogVerbosityPNLogVerbosityOptionalSet PNLogVerbosity.BODY to enable debugging. To disable debugging use the option PNLogVerbosity.NONE
    AuthKeystringOptionalIf Access Manager is utilized, client will use this AuthKey in all restricted requests.
    SecureboolOptionalUse SSL.
    SubscribeTimeoutintOptionalHow long to keep the subscribe loop running before disconnect.
    NonSubscribeRequestTimeoutintOptionalOn non subscribe operations, how long to wait for server response.
    FilterExpressionstringOptionalFeature to subscribe with a custom filter expression.
    HeartbeatNotificationOptionPNHeartbeatNotificationOptionOptional

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

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

    OriginstringOptionalCustom Origin if needed.
    ReconnectionPolicyPNReconnectionPolicyOptional

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

    Use option PNReconnectionPolicy.EXPONENTIAL to set exponential retry interval.

    PresenceTimeoutintOptionalThe setting with set the custom presence server timeout.
    PresenceIntervalintOptionalThe setting with set the custom presence server timeout along with the custom interval to send the ping back to the server.
    ProxyProxyOptionalInstruct the SDK to use a Proxy configuration when communicating with PubNub servers.
    PubnubLogIPubnubLogOptionalPass the instance of a class that implements IPubnubLog to capture logs for troubleshooting.
    UseClassicHttpWebRequestboolOptionalUse HttpWebRequest to support ASP.NET/MVC and other IIS hosting applications.
PNConfiguration pnConfiguration = new PNConfiguration();
// subscribeKey from admin panel
pnConfiguration.SubscribeKey = "SubscribeKey"; // required
// publishKey from admin panel (only required if publishing)
pnConfiguration.PublishKey = "PublishKey";
// secretKey (only required for access operations)
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 = "customUUID";
// Enable Debugging
pnConfigurationr.LogVebosity = PNLogVerbosity.BODY;
// if Access Manager is utilized, client will use this AuthKey in all restricted
// requests
pnConfiguration.AuthKey = "authKey";
// use SSL.
pnConfiguration.Secure = true;
// how long to keep the subscribe loop running before disconnect
pnConfiguration.SubscribeTimeout = 310;
// on non subscribe operations, how long to wait for server response
pnConfiguration.NonSubscribeRequestTimeout = 300;
// 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.SetPresenceTimeoutWithCustomInterval(120, 59);
pnConfiguration.PresenceTimeout = 120;
// Use HttpWebRequest to support ASP.NET/MVC and other IIS hosting applications
pnConfiguration.UseClassicHttpWebRequest= true;
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 C# V4 SDK:
  1. ParameterTypeRequiredDescription
    pnConfigurationPNConfigurationYesGoto Configuration for more details.
PNConfiguration pnConfiguration = new PNConfiguration();
pnConfiguration.PublishKey = "my_pubkey";
pnConfiguration.SubscribeKey = "my_subkey";
pnConfiguration.Secure = true;

Pubnub 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.PublishKey = "my_pubkey";
    pnConfiguration.SubscribeKey = "my_subkey";
    pnConfiguration.Secure = false;
    
    Pubnub 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 pubnub = new Pubnub(pnConfiguration);
  3. Under certain circumstances it useful to use a custom UUID to help in identifying your users.
    PNConfiguration pnConfiguration = new PNConfiguration();
    pnConfiguration.PublishKey = "my_pubkey";
    pnConfiguration.SubscribeKey = "my_subkey";
    pnConfiguration.Uuid = "Stephen";
    
    Pubnub pubnub = new Pubnub(pnConfiguration);
  4. This examples demonstrates how to enable PubNub Transport Layer Encryption with SSL. Just initialize the client with ssl 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.PublishKey = "my_pubkey";
    pnConfiguration.SubscribeKey = "my_subkey";
    pnConfiguration.Secure = true;
    
    Pubnub 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.PublishKey = "my_pubkey";
    pnConfiguration.SubscribeKey = "my_subkey";
    pnConfiguration.SecretKey = "my_secretkey";
    pnConfiguration.Secure = true;
    
    Pubnub 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 property(s) in C# V4 SDK
  1. PropertyTypeRequiredDescription
    UuidstringYesUuid to be used as a device identifier, a default Uuid is generated if not passed.
  2. This method doesn't take any arguments.

PNConfiguration pnConfiguration = new PNConfiguration();
pnConfiguration.Uuid = "customUUID";
var customUUID = pnConfiguration.Uuid;
Setter and getter for users auth key.
  1. PropertyTypeRequiredDescription
    AuthKeystringYesIf Access Manager is utilized, client will use this AuthKey in all restricted requests.
PNConfiguration pnConfiguration = new PNConfiguration();
pnConfiguration.AuthKey = "authKey";
var sampleAuthKey = 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.
PNConfiguration pnConfiguration = new PNConfiguration();
pnConfiguration.FilterExpression = "such=wow";
var sampleFilterExp = pnConfiguration.FilterExpression;

Go to Publish & Subscribe