JavaJava V4 Configuration API Reference for Realtime Apps

Java 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 Java V4 SDK:
  1. PropertiesTypeRequiredDefaultsDescription
    setSubscribeKeyStringYessubscribeKey from admin panel.
    setPublishKeyStringOptionalpublishKey from admin panel (only required if publishing).
    setSecretKeyStringOptionalsecretKey (only required for access operations, keep away from Android).
    setCipherKeyStringOptionalIf cipher is passed, all communicatons to/from PubNub will be encrypted.
    setUuidStringOptionalSDK generated uuidUUID to be used as a device identifier, a default UUID is generated if not passed.
    setLogVerbosityPNLogVerbosityOptionalPNLogVerbosity.NONESet PNLogVerbosity.BODY to enable debugging. To disable debugging use the option PNLogVerbosity.NONE
    setAuthKeyStringOptionalNot setIf Access Manager is utilized, client will use this authKey in all restricted requests.
    setCacheBustingBooleanOptionalIf operating behind a misbehaving proxy, allow the client to shuffle the subdomains.
    setSecureBooleanOptionaltrueUse SSL.
    setConnectTimeoutIntOptional5How long to wait before giving up connection to client.
    setSubscribeTimeoutIntOptional310How long to keep the subscribe loop running before disconnect.
    setNonSubscribeRequestTimeoutIntOptional10

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

    setFilterExpressionStringOptionalNot setFeature to subscribe with a custom filter expression.
    setHeartbeatNotificationOptionsPNHeartbeatNotificationOptionsOptionalPNHeartbeatNotificationOptions.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.

    setOriginStringOptionalCustom origin if needed.
    setReconnectionPolicyPNReconnectionPolicyOptionalPNReconnectionPolicy.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.

    setPresenceTimeoutIntOptional0The setting with set the custom presence server timeout.
    setPresenceTimeoutWithCustomIntervalIntOptional0The setting with set the custom presence server timeout along with the custom interval to send the ping back to the server.
    setProxyProxyOptionalInstruct the SDK to use a proxy configuration when communicating with PubNub servers. For more details see this https://docs.oracle.com/javase/7/docs/api/java/net/Proxy.html
    setMaximumReconnectionRetriesIntOptionalinfinityThe config sets how many times to retry to reconnect before giving up.
    setProxySelectorProxySelectorOptionalSet Java ProxySelector, for more details see this https://docs.oracle.com/javase/7/docs/api/java/net/ProxySelector.html
    setProxyAuthenticatorAuthenticatorOptionalSet Java Authenticator, for more details see this https://docs.oracle.com/javase/7/docs/api/java/net/Authenticator.html
    setGoogleAppEngineNetworkingBooleanOptionalEnable Google App Engine networking.
PNConfiguration pnConfiguration = new PNConfiguration();
// subscribeKey from admin panel
pnConfiguration.setSubscribeKey("SubscribeKey"); // required
// publishKey from admin panel (only required if publishing)
pnConfiguration.setPublishKey("PublishKey"); 
// secretKey (only required for access operations, keep away from Android)
pnConfiguration.setSecretKey("SecretKey");
// if cipherKey is passed, all communicatons to/from pubnub will be encrypted
pnConfiguration.setCipherKey("cipherKey");
// UUID to be used as a device identifier, a default UUID is generated 
// if not passed
pnConfiguration.setUuid("customUUID");
// Enable Debugging
pnConfiguration.setLogVerbosity(PNLogVerbosity.BODY);
// if Access Manager is utilized, client will use this authKey in all restricted
// requests
pnConfiguration.setAuthKey("authKey");
// use SSL.
pnConfiguration.setSecure(true);
// PSV2 feature to subscribe with a custom filter expression
pnConfiguration.setFilterExpression("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.setHeartbeatNotificationOptions(PNHeartbeatNotificationOptions.All);
pnConfiguration.setPresenceTimeout(120);
Add PubNub to your project using one of the procedures defined under How to Get It.
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 Java V4 SDK:
  1. ParameterTypeRequiredDescription
    pnConfigurationPNConfigurationYesGoto Configuration for more details.
PNConfiguration pnConfiguration = new PNConfiguration();
pnConfiguration.setSubscribeKey("my_subkey");
pnConfiguration.setPublishKey("my_pubkey");
pnConfiguration.setSecure(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.setSubscribeKey("SubscribeKey");
    pnConfiguration.setPublishKey("PublishKey");
    pnConfiguration.setSecure(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.setSubscribeKey("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.setSubscribeKey("my_subkey");
    pnConfiguration.setPublishKey("my_pubkey");
    pnConfiguration.setUuid("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.setSubscribeKey("my_subkey");
    pnConfiguration.setPublishKey("my_pubkey");
    pnConfiguration.setSecure(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.setSubscribeKey("my_subkey");
    pnConfiguration.setPublishKey("my_pubkey");
    pnConfiguration.setSecretKey("my_secretkey");
    pnConfiguration.setSecure(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.
  6. PNConfiguration pnConfiguration = new PNConfiguration();
    pnConfiguration.setSubscribeKey("mySubscribeKey");
    pnConfiguration.setPublishKey("myPublishKey");
    pnConfiguration.setSecretKey("mySecretKey");
    pnConfiguration.setUuid("myServerUUID");
    Proxy proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress("http://mydomain.com", 8080));
    pnConfiguration.setProxy(proxy);
    PubNub pubNub = new PubNub(pnConfiguration);
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 Java V4 SDK
  1. ParameterTypeRequiredDefaultsDescription
    uuidStringYesSDK generated uuidUUID 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.setUuid("customUUID");
PNConfiguration pnConfiguration = new PNConfiguration();
pnConfiguration.getUuid();
Setter and getter for users auth key.
  1. ParameterTypeRequiredDescription
    Auth KeyStringYesIf Access Manager is utilized, client will use this authKey in all restricted requests.
  2. This method doesn't take any arguments.
PNConfiguration pnConfiguration = new PNConfiguration();
pnConfiguration.setAuthKey("authKey");
pnConfiguration.getAuthKey();
None.
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. ParameterTypeRequiredDescription
    filterExpressionStringYesPSV2 feature to subscribe with a custom filter expression
  2. This method doesn't take any arguments.

PNConfiguration pnConfiguration = new PNConfiguration();
pnConfiguration.setFilterExpression("such=wow");
pnConfiguration.getFilterExpression();

Go to Publish & Subscribe