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: Cryptor)

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_policy()RequestRetryPolicyYesRequestRetryPolicy::NoneCustom reconnection configuration parameters.

RequestRetryPolicy is the type of policy to be used.

Available values:
  • RequestRetryPolicy::None
  • RequestRetryPolicy::Linear
  • RequestRetryPolicy::Exponential

For more information, refer to Reconnection Policy.
with_cryptor()T: CryptorOptionalStruct implemeting the Cryptor trait used to encrypt/decrypt messages and signals during publish subscribe operations.
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.

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

In the Rust SDK, a subscription is a stream (implements the stream trait) that can return the individual elements it receives. It is within the subscription entity that you add the logic on how to handle the received data.

Add Listeners

All available listeners:

tokio::spawn( |event| async move {
match event {
Presence::Join { timestamp, uuid, channel, subscription, occupancy } => {
println!("\nJoin event:");
println!("Timestamp: {}", timestamp);
println!("UUID: {}", uuid);
println!("Channel: {}", channel);
println!("Subscription: {}", subscription);
println!("Occupancy: {}", occupancy);
Presence::Leave { timestamp, channel, subscription, occupancy, uuid } => {
println!("\nLeave event:");
println!("Timestamp: {}", timestamp);
println!("Channel: {}", channel);
println!("Subscription: {}", subscription);
show all 121 lines

Remove Listeners

To remove a listener, remove it from your subscription stream logic. There is no dedicated method to register or unregister listeners within your application.

Handling Disconnects

The client may disconnect due to unpredictable network conditions.

You can configure automatic reconnection using the with_retry_policy() configuration method.

Last updated on