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?)
Parameter | Type | Required | Default | Description |
---|---|---|---|---|
publishKey | String? | Optional | nil | Specifies the PubNub Publish Key to be used when publishing messages to a channel. |
subscribeKey | String? | Yes | nil | Specifies the PubNub Subscribe Key to be used when subscribing to a channel. |
authKey | String | Optional | nil | If Access Manager is enabled, client will use authKey on all requests. |
userId | String | Yes | userId 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 theuserId , you won't be able to connect to PubNub. | |
useSecureConnections | Bool | Optional | true | If 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. |
origin | String | Optional | "ps.pndsn.com" | Domain name used for requests. |
automaticRetry | AutomaticRetry? | Optional | ReconnectionPolicy.exponential (subscribe only) | Custom reconnection configuration parameters. For more information, refer to the automaticRetry section. |
urlSessionConfiguration | URLSessionConfiguration | Yes | URLSessionConfiguration.pubnub | URLSessionConfiguration used for URLSession network events. |
durationUntilTimeout | Int | Optional | 300 | How long (in seconds) the server will consider the client alive for presence. Minimum value is 20 . |
heartbeatInterval | UInt | Optional | 0 | How often (in seconds) the client will announce itself to server. Minimum value is 0. |
suppressLeaveEvents | Bool | Optional | false | Whether to send out the leave requests. |
requestMessageCountThreshold | UInt | Optional | 100 | The number of messages into the payload before emitting RequestMessageCountExceeded . |
filterExpression | String? | Optional | nil | PSV2 feature to subscribe with a custom filter expression. |
cryptoModule | cryptoModule: CryptoModule.aesCbcCryptoModule(with: cipherKey) cryptoModule: CryptoModule.legacyCryptoModule(with: cipherKey) | Optional | nil | The 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. |
enableEventEngine | Bool | Optional | true | Whether to use the standardized workflows for subscribe and presence. |
maintainPresenceState | Bool | Optional | true | Whether the state set using PubNub.setPresence() should be maintained for the current userId . This option works only when enableEventEngine is set to true . |
cipherKey | nil | This way of setting this parameter is deprecated, pass it to cryptoModule instead. | ||
uuid | This parameter is deprecated, use userId instead.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:
Parameter | Type | Description |
---|---|---|
retryLimit | UInt | Maximum number of times a request can be retried before failing. |
policy | ReconnectionPolicy | The policy to be used. Available values include:
ReconnectionPolicy.exponential is the default value for subscribe connections. |
retryableHTTPStatusCodes | Set<Int> | One or more HTTP status codes for which the retry policy will be applied. |
retryableURLErrorCode | Set<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.
-
In combination with a subscribe change:
pubnub.subscribe(to: ["new_subscription"], filterOverride: "(senderID=='my_new_userId')")
-
Without needing to make a subscription change:
pubnub.subscribeFilterExpression = "(senderID=='my_new_userId')"