Configuration API for PubNub Swift Native SDK

A configuration object that defines behavior and policies for a PubNub instance.

Initializer(s)

Creates a configuration using the specified PubNub Publish and Subscribe Keys.

Privacy

MAU billing tracks users (Device and MAU) for analytics and billing. PubNub does not track customers using Transactions with random UUIDs/UserIDs.

Method(s)

To Initialize PubNub, you can use the following method(s) in the Swift SDK:

PubNubConfiguration(publishKey: String?, subscribeKey: String?)
ParameterTypeRequiredDefaultDescription
publishKeyString?OptionalnilSpecifies the PubNub Publish Key to be used when publishing messages to a channel.
subscribeKeyString?YesnilSpecifies the PubNub Subscribe Key to be used when subscribing to a channel.
authKeyStringOptionalnilIf Access Manager is enabled, client will use authKey on all requests.
userIdStringYesuserId to use. You should set a unique identifier for the user or the device that connects to PubNub.

It's a UTF-8 encoded string of up to 92 alphanumeric characters.

If you don't set the userId, you won't be able to connect to PubNub.
useSecureConnectionsBoolOptionaltrueIf true, requests will be made over HTTPS; otherwise they will use HTTP. You will still need to disable ATS for the system to allow insecure network traffic. See Apple's documentation for further details.
originStringOptional"ps.pndsn.com"Domain name used for requests.
automaticRetryAutomaticRetry?OptionalReconnectionPolicy.exponential (subscribe only)Custom reconnection configuration parameters.

For more information, refer to the automaticRetry section.
urlSessionConfigurationURLSessionConfigurationYesURLSessionConfiguration.pubnubURLSessionConfiguration used for URLSession network events.
durationUntilTimeoutIntOptional300How long (in seconds) the server will consider the client alive for presence. Minimum value is 20.
heartbeatIntervalUIntOptional0How often (in seconds) the client will announce itself to server. Minimum value is 0.
suppressLeaveEventsBoolOptionalfalseWhether to send out the leave requests.
requestMessageCountThresholdUIntOptional100The number of messages into the payload before emitting RequestMessageCountExceeded.
filterExpressionString?OptionalnilPSV2 feature to subscribe with a custom filter expression.
cryptoModulecryptoModule: CryptoModule.aesCbcCryptoModule(with: cipherKey)

cryptoModule: CryptoModule.legacyCryptoModule(with: cipherKey)
OptionalnilThe cryptography module used for encryption and decryption of messages and files. Takes the cipherKey parameter as argument.

For more information, refer to the cryptoModule section.
enableEventEngineBoolOptionaltrueWhether to use the standardized workflows for subscribe and presence.
maintainPresenceStateBoolOptionaltrueWhether the state set using PubNub.setPresence() should be maintained for the current userId. This option works only when enableEventEngine is set to true.
cipherKeyCrypto?OptionalnilThis way of setting this parameter is deprecated, pass it to cryptoModule instead.

If set, all communication will be encrypted with this key.
uuidStringYesThis parameter is deprecated, use userId instead.

UUID to use. You should set a unique UUID to identify the user or the device that connects to PubNub. If you don't set the UUID, you won't be able to connect to PubNub.

cryptoModule

cryptoModule provides encrypt/decrypt functionality for messages and files. From the 6.1.0 release on, you can configure how the actual encryption/decryption algorithms work.

Each PubNub SDK is bundled with two ways of encryption: the legacy encryption with 128-bit cipher key entropy and the recommended 256-bit AES-CBC encryption. For more general information on how encryption works, refer to the Message Encryption and File Encryption sections.

If you do not explicitly set the cryptoModule in your app and have the cipherKey param set in PubNub config, the client defaults to using the legacy encryption.

Legacy encryption with 128-bit cipher key entropy

You don't have to change your encryption configuration if you want to keep using the legacy encryption. If you want to use the recommended 256-bit AES-CBC encryption, you must explicitly set that in PubNub config.

cryptoModule configuration

To configure the cryptoModule to encrypt all messages/files, you can use the following methods in the Swift SDK:

// Encrypts using 256-bit AES-CBC cipher (recommended)
// Decrypts data encrypted with the legacy and the 256-bit AES-CBC ciphers
let pubnub = PubNub(
configuration: PubNubConfiguration(
cryptoModule: CryptoModule.aesCbcCryptoModule(with: "pubnubenigma")
)
)

// Encrypts with 128-bit cipher key entropy (legacy)
// Decrypts data encrypted with the legacy and the 256-bit AES-CBC ciphers
let pubnub = PubNub(
configuration: PubNubConfiguration(
cryptoModule: CryptoModule.legacyCryptoModule(with: "pubnubenigma")
)
)

Your client can decrypt content encrypted using either of the modules. This way, you can interact with historical messages or messages sent from older clients while encoding new messages using the more secure 256-bit AES-CBC cipher.

Older SDK versions

Apps built using the SDK versions lower than 6.1.0 will not be able to decrypt data encrypted using the 256-bit AES-CBC cipher. Make sure to update your clients or encrypt data using the legacy algorithm.

automaticRetry

automaticRetry provides automatic reconnection options. The AutomaticRetry object has the following parameters:

ParameterTypeDescription
retryLimitUIntMaximum number of times a request can be retried before failing.
policyReconnectionPolicyThe policy to be used.

Available values include:

  • ReconnectionPolicy.linear(delay)
  • ReconnectionPolicy.exponential(minDelay, maxDelay)


ReconnectionPolicy.exponential is the default value for subscribe connections.
retryableHTTPStatusCodesSet<Int>One or more HTTP status codes for which the retry policy will be applied.
retryableURLErrorCodeSet<URLError.Code>One or more URL error codes for which the retry policy will be applied.
excluded[AutomaticRetry.Endpoint]One or more endpoints for which the retry policy won't be applied.

For more information, refer to Reconnection Policy.

Basic Usage

Initialize the PubNub client API

Required UserId

Always set the userId to uniquely identify the user or device that connects to PubNub. This userId should be persisted, and should remain unchanged for the lifetime of the user or the device. If you don't set the userId, you won't be able to connect to PubNub.

let config = PubNubConfiguration(
publishKey: "demo",
subscribeKey: "demo",
userId: "myUniqueUserId"
)

let pubnub = PubNub(configuration: config)

Other Examples

Initialization for a Read-Only client

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:

Required UserId

Always set the userId to uniquely identify the user or device that connects to PubNub. This userId should be persisted, and should remain unchanged for the lifetime of the user or the device. If you don't set the userId, you won't be able to connect to PubNub.

let config = PubNubConfiguration(
subscribeKey: "demo",
userId: "myUniqueUserId"
)
let pubnub = PubNub(configuration: config)

Event Listeners

PubNub SDKs provide several sources for real-time updates:

  • The PubNub client can receive updates from all subscriptions: all channels, channel groups, channel metadata, and users.
  • The Subscription object can receive updates only for the particular object for which it was created: channel, channel group, channel metadata, or user.
  • The SubscriptionsSet object can receive updates for all objects for which a list of subscription objects was created.

To facilitate working with those real-time update sources, PubNub SDKs use local representations of server entities that allow you to subscribe and add handlers on a per-entity basis. For more information, refer to Publish & Subscribe.

Overriding PubNub Configuration

All PubNubConfiguration properties are mutable, and can be changed after the object has been initialized. However, once the configuration is set on a PubNub instance those configurations are locked and can't be changed. Any changes would require a creating new PubNub instance.

userId

var config = pubnub.configuration
config.userId = "my_new_userId"

let pubnub = PubNub(configuration: config)

Authentication

var config = pubnub.configuration
config.authKey = "my_new_authkey"

let pubnub = PubNub(configuration: config)

Filter

You can override the filter expression without creating a new PubNub instance in one of two ways.

Equivalent functionality

These are functionally equivalent and will take effect on the next subscription request and persist until changed.

  1. In combination with a subscribe change:

    pubnub.subscribe(to: ["new_subscription"], filterOverride: "(senderID=='my_new_userId')")
  2. Without needing to make a subscription change:

    pubnub.subscribeFilterExpression = "(senderID=='my_new_userId')"
Last updated on