Configuration API for PubNub Rust SDK

Rust complete API reference for building real-time applications on PubNub, including basic usage and sample code.


Available in features



You can use the PubNubClientBuilder struct to create and initialize PubNub API clients. The client is transport-layer-agnostic, so you can use any transport layer that implements the Transport trait.


let client = PubNubClientBuilder::with_transport(Transport)
.with_keyset(Keyset {
publish_key: Some(String),
subscribe_key: String,
secret_key: String,
.with_cryptor(T: CryptoProvider)
show all 16 lines

To create a PubNub instance, you can use the following methods in the Rust SDK:

MethodArgument TypeRequiredDefaultDescription
with_transport()TransportYeswith_reqwest_transport()Transport layer to use. with_reqwest_transport() requires the reqwest feature, which is enabled by default.
with_keyset()KeysetYesA method that takes the Keyset struct with Admin Portal keys as the value. Refer to Keyset for more information.
with_user_id()StringYesUser ID 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 64 alphanumeric characters.

If you don't set the User ID, you won't be able to connect to PubNub.
with_instance_id()Into<String>OptionalClient instance ID.
with_config()PubNubConfigOptionalData provided in the builder.Struct that allows to overwrite Keyset and user_id configuration. Useful for working with multiple client builders.
with_retry_configuration()RequestRetryConfigurationOptionalRequestRetryConfiguration::NoneCustom reconnection configuration parameters. You can specify one or more endpoint groups for which the retry policy won't be applied.

RequestRetryConfiguration is the type of policy to be used.

Available values:
  • RequestRetryConfiguration::None
  • RequestRetryConfiguration::Linear {delay, max_retry, excluded_endpoints}
  • RequestRetryConfiguration::Exponential {min_delay , max_delay, max_retry, excluded_endpoints}

excluded_endpoints takes a vector of enums, for example, excluded_endpoints: Some(vec![Endpoint::Publish]). For more information, refer to Reconnection Policy.
with_cryptor()T: CryptoProviderOptionalThe cryptography module used for encryption and decryption of messages. For more information, refer to the CryptoModule section.
with_heartbeat_value()u64Optional300The presence timeout period, in seconds. The minimum value is 20 seconds.
with_heartbeat_interval()u64OptionalHow often (in seconds) the client announces itself to server. The minimum value is 0 seconds, which means the client doesn't announce itself at all.
with_suppress_leave_events()boolOptionalfalseWhether to stop sending presence leave events during the unsubscribe process.
with_filter_expression()StringOptionalString used to subscribe with a custom filter. For more information, refer to Message Filters.
build()YesCreates the PubNub instance based on the provided data and returns it.


The Keyset struct is how you provide your account credentials to the Rust SDK.

publishKeySome(String)YespublishKey from the Admin Portal.
subscribeKeyStringYessubscribeKey from the Admin Portal.
secretKeyStringNosecretKey from the Admin Portal. Required for Access Manager operations.


CryptoModule implements the CryptoProvider trait and provides encrypt/decrypt functionality for messages. From the 0.3.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 Message Encryption.

The default constructors for the bundled crypto modules take the cipher_key (string used to encrypt/decrypt) and use_random_iv (boolean, whether or not to use a random initialization vector) parameters as arguments.

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 Rust SDK:

// encrypts using 256-bit AES-CBC cipher (recommended)
// decrypts data encrypted with the legacy and the 256-bit AES-CBC ciphers
let client = PubNubClientBuilder::with_transport(Transport)
.with_cryptor(CryptoModule::new_aes_cbc_module("enigma", true)?)

// encrypts with 128-bit cipher key entropy (legacy)
// decrypts data encrypted with the legacy and the 256-bit AES-CBC ciphers
let client = PubNubClientBuilder::with_transport(Transport)
.with_cryptor(CryptoModule::new_legacy_module("enigma", true)?)

show all 22 lines

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 0.3.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.

Basic Usage

Required user_id

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


A result with PubNub instance or error if the configuration is wrong.

Other Examples

Initialize with custom origin

You can initialize the PubNub API client and use a custom domain.

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.

Last updated on