Configuration API for Swift Native SDK
Use this configuration object to define how a PubNub instance behaves.
Initializer(s)
Create a configuration with your 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, use:
PubNubConfiguration(
publishKey: String?,
subscribeKey: String,
userId: String,
cryptoModule: CryptoModule? = nil,
authKey: String? = nil,
authToken: String? = nil,
useSecureConnections: Bool = true,
origin: String = "ps.pndsn.com",
useInstanceId: Bool = false,
useRequestId: Bool = false,
automaticRetry: AutomaticRetry? = .default,
durationUntilTimeout: UInt = 300,
heartbeatInterval: UInt = 0,
supressLeaveEvents: Bool = false,
show all 20 linesParameter | Description |
---|---|
publishKey Type: String? Default: nil | Specifies the PubNub Publish Key to be used when publishing messages to a channel. |
subscribeKey *Type: String Default: nil | Specifies the PubNub Subscribe Key to be used when subscribing to a channel. |
userId *Type: String Default: n/a | 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. |
cryptoModule Type: CryptoModule? Default: 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. |
authKey Type: String? Default: nil | If Access Manager is enabled, client will use authKey on all requests. |
authToken Type: String? Default: nil | If Access Manager is enabled, client will use authToken instead of authKey on all requests. |
useSecureConnections Type: Bool Default: true | If true , requests will be made over HTTPS; otherwise they will use HTTP (for cases when HTTPS might not be necessary or feasible, like development or testing purposes). You will still need to disable ATS for the system to allow insecure network traffic. See Apple's documentation for further details. |
origin Type: String Default: "ps.pndsn.com" | Domain name used for requests. To request a custom domain, contact support and follow the request process. |
useInstanceId Type: Bool Default: false | Whether a PubNub object instanceId should be included on outgoing requests. |
useRequestId Type: Bool Default: false | Whether a request identifier should be included on outgoing requests. |
automaticRetry Type: AutomaticRetry? Default: ReconnectionPolicy.exponential (subscribe only) | Custom reconnection configuration parameters. For more information, refer to the automaticRetry section. |
durationUntilTimeout Type: Int Default: 300 | Defines how long the server considers the client alive for presence. This property works similarly to the concept of long polling by sending periodic requests to the PubNub server every 300 seconds by default. These requests ensure the client remains active on subscribed channels. If no heartbeat is received within the timeout period, the client is marked inactive, triggering a "timeout" event on the presence channel. Minimum value is 20 . |
heartbeatInterval Type: UInt Default: 0 | Specifies how often the client will send heartbeat signals to the server. This property offers more granular control over client activity tracking than durationUntilTimeout . Configure this property to achieve a shorter presence timeout if needed, with the interval typically recommended to be (durationUntilTimeout / 2) - 1 . The default value is 0 and it means that the SDK doesn't send explicit heartbeat messages to the server. Due to server constraints, don't set the value below 3 when configuring this parameter. |
suppressLeaveEvents Type: Bool Default: false | Whether to send out the leave requests. |
requestMessageCountThreshold Type: UInt Default: 100 | The number of messages into the payload before emitting RequestMessageCountExceeded . |
filterExpression Type: String? Default: nil | PSV2 feature to subscribe with a custom filter expression. |
enableEventEngine Type: Bool Default: true | Whether to use the recommended standardized workflows for subscribe and presence, optimizing how the SDK internally handles these operations and which statuses it emits. |
maintainPresenceState Type: Bool Default: true | This option works only when enableEventEngine is set to true . Whether the custom presence state information set using pubnub.setPresence() should be sent every time the SDK sends a subscribe call. |
cipherKey Type: Default: nil | This way of setting this parameter is deprecated, pass it to cryptoModule instead. |
uuid Type: Default: n/a | This parameter is deprecated, use userId instead.UUID , you won't be able to connect to PubNub. |
cryptoModule
cryptoModule
encrypts and decrypts messages and files. From 6.1.0, you can choose the algorithms.
Each SDK includes two options: legacy 128-bit encryption and recommended 256-bit AES-CBC. For background, see Message Encryption and File Encryption.
If you don't set cryptoModule
and have cipherKey
in PubNub config, the client uses legacy encryption.
For configuration details, utilities, and examples, see 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.
automaticRetry
automaticRetry
lets the client retry requests automatically. It has these parameters:
Parameter | Description |
---|---|
retryLimit Type: UInt | Maximum number of times a request can be retried before failing. |
policy Type: ReconnectionPolicy | The policy to be used. Available values include:
ReconnectionPolicy.exponential is the default value for subscribe connections. |
retryableHTTPStatusCodes Type: Set<Int> | One or more HTTP status codes for which the retry policy will be applied. |
retryableURLErrorCode Type: Set<URLError.Code> | One or more URL error codes for which the retry policy will be applied. |
excluded Type: [AutomaticRetry.Endpoint] | One or more endpoints for which the retry policy won't be applied. |
For more information, refer to SDK connection lifecycle.
Request configuration
Use PubNub.RequestConfiguration
to customize one request without changing global settings.
Parameter | Description |
---|---|
customSession Type: SessionReplaceable? Default: nil | A custom network session that implements the SessionReplaceable protocol, which provides session management capabilities including request routing, task execution, and session lifecycle control. |
customConfiguration Type: RouterConfiguration? Default: nil | The endpoint configuration used by the request. |
responseQueue Type: DispatchQueue Default: n/a | The queue that will be used for dispatching a response. |
SessionReplaceable
SessionReplaceable
defines a custom network session interface. It manages routing, task execution, and the session lifecycle.
Property | Description |
---|---|
sessionID Type: UUID | The unique identifier for the session object |
session Type: URLSessionReplaceable | The underlying URLSession used to execute network tasks |
sessionQueue Type: DispatchQueue | The dispatch queue used to execute session operations |
defaultRequestOperator Type: RequestOperator? | The default request operator attached to every request (settable) |
sessionStream Type: SessionStream? | Optional session stream for real-time communication (settable) |
RouterConfiguration
RouterConfiguration
defines the base settings for PubNub endpoints. It includes authentication, connection security, encryption, and other behavior used to build and execute HTTP requests.
Property | Description |
---|---|
publishKey Type: String? | Refer to the publishKey section. |
subscribeKey Type: String | Refer to the subscribeKey section. |
uuid Type: String | Unique device identifier for the client (equivalent to userId in Configuration) |
useSecureConnections Type: Bool | Refer to the useSecureConnections section. |
origin Type: String | Refer to the origin section. To request a custom domain, contact support and follow the request process. |
authKey Type: String? | Refer to the authKey section. |
authToken Type: String? | Refer to the authToken section. |
cryptoModule Type: CryptoModule? | Refer to the cryptoModule section. |
useRequestId Type: Bool | Refer to the useRequestId section. |
consumerIdentifiers Type: [String: String] | Key-value pairs identifying various consumers for request tracking |
enableEventEngine Type: Bool | Refer to the enableEventEngine section. |
maintainPresenceState Type: Bool | Refer to the maintainPresenceState section. |
urlScheme Type: String | URL scheme derived from useSecureConnections ("https " or "http "). |
subscribeKeyExists Type: Bool | Whether subscribeKey is valid and not empty. |
publishKeyExists Type: Bool | Whether publishKey is valid and not empty. |
DispatchQueue
DispatchQueue
specifies which queue handles response callbacks.
Method | Description |
---|---|
currentLabel Type: String | Returns the label of the current DispatchQueue or "Unknown Queue" if no label was set |
Official Apple Documentation
For standard DispatchQueue
properties and methods, refer to Apple's DispatchQueue documentation.