Announcing the PubNub Rust SDK
Tags: rust, pubnub, realtime, news
Published: 2023-10-13 06:06:27

Hello, fellow Rustaceans! We are excited to announce the PubNub Rust SDK, the easiest way for developers to leverage PubNub’s [real-time features](https://www.pubnub.com/products/pubnub-platform/#features) in any kind of Rust applications, such as [chat](https://www.pubnub.com/solutions/chat/), [live audience engagement](https://www.pubnub.com/solutions/live-audience-engagement/), [multi-user collaboration](https://www.pubnub.com/solutions/multiuser-collaboration/), [IoT device control](https://www.pubnub.com/solutions/iot-device-control/), [gaming](https://www.pubnub.com/solutions/gaming/), and more. This isn’t just about a new language; it’s a leap toward an efficient, cost-effective, robust, and secure real-time experience.

## What is Rust?

[Rust](https://www.rust-lang.org/) is an open-source programming language focusing on memory safety and preventing segfaults with its strict compiler. Based on the design philosophy of C and C++, its concurrency model, zero-cost abstractions, and non-reliance on garbage collection at runtime make it an excellent choice for real-time applications, as it offers the control of a low-level language with the helpful abstractions of a high-level one. It’s even been found that applications developed in Rust that run on ARM systems [improve efficiency](https://www.pubnub.com/blog/the-exciting-world-of-rust-arm-processors/) by providing better performance at a lower price. 

In fact, according to the [2022 Developer Survey by Stack Overflow](https://insights.stackoverflow.com/survey/2022), Rust is the community’s most loved language for the seventh year in a row, “with 87% of developers saying they want to continue using it” and “ties with Python as the most wanted technology with TypeScript running a close second.”

If you’re curious about Rust but have never tried it before, worry not - Rust’s syntax and modules will make the transition from other languages, such as Python, Java, or JavaScript, much easier than C/C++. It supports WebAssembly, making frontend tasks smoother, and the ‘cargo’ package manager ensures easy management of dependencies. To delve deeper into Rust, check out their [official documentation](https://doc.rust-lang.org/book/). It offers comprehensive installation guidelines, detailed documentation, and practical tutorials to get you started.

## Rust Improves Performance and Lowers Costs at PubNub

Aside from its popularity, Rust has provided the PubNub platform with significant performance boosts, improved safety, and reduced operational costs for our architecture. We never want to become stagnant and decided to re-architect some of our services and applications with Rust to develop high-performing and reliable APIs that can handle the demands of modern real-time applications. We saw a workflow improvement of up to 50% and significant operational cost savings meaning we can now deliver real-time updates with even lower latencies and costs. This makes perfect sense with the PubNub platform, as we provide low-latency communications for hundreds of millions of messages monthly. We’ve even created a Rust CLI to ensure a smooth transition between old and new systems with these new changes!

You can learn more in-depth about how Rust helps us deliver [best-in-class APIs](https://www.pubnub.com/blog/pubnub-pursues-rust-to-deliver-best-in-class-apis/).

## Empowering Rust Developers with PubNub

Based on our love for Rust and how it helped us improve our platform, we wanted to make it easier for developers in our community to use PubNub features to power their applications. The PubNub Rust SDK isn't just another tool—it’s a game-changer. With its async operations, it's optimized for real-time applications, be it chat, IoT device control, geolocation solutions, gaming, blockchain & Web3, data streaming, or enterprise software.

While Rust developers in the past were able to make use of the PubNub platform by using our [REST API](https://www.pubnub.com/docs/sdks/rest-api), Rustaceans can now use PubNub Rust SDK to power the following functionality in their apps directly or in conjunction with other PubNub services:

- **Publish/Subscribe:** Our Publish and Subscribe APIs make it easy to deliver high volume, in-app messages 1:1 or to an unlimited number of Channels, anywhere around the globe, in less than 100ms.
  
- **Presence:** Track and alert on online/offline events for users and devices, as well as track accurate occupancy counts across channels with millions of connected devices.

- **Access Manager:** Fine-grained access control that offers a cryptographic, token-based permission administrator that allows you to regulate clients' access to PubNub resources.

- **Functions:** Run your code within our network or leverage existing integrations to transform, re-route, augment, filter, aggregate messages, run code triggered by user presence events, and more, all computed at the edge.

- **Events & Actions:** Enables business users to filter events to manage event listeners and associate actions with no code.

- **PubNub Insights:** Aggregated metrics and usable analytics built from your PubNub data & utilization.

## Getting Started with the PubNub Rust SDK

To begin implementing these features in your own Rust application, you’ll need to import and add PubNub to your Rust project in the `Cargo.toml` file:

```toml
# default features
[dependencies]
pubnub = "0.2.1"

# all features
[dependencies]
pubnub = { version = "0.2.1", features = ["full"] }
```
Then, you’ll need to configure PubNub to be able to use all of the features we described earlier. Here is a template you can use:
use pubnub::{Keyset, PubNubClientBuilder};

```rust
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
    let publish_key = "my_publish_key";
    let subscribe_key = "my_subscribe_key";
    let client = PubNubClientBuilder::with_reqwest_transport()
        .with_keyset(Keyset {
            subscribe_key,
            publish_key: Some(publish_key),
            secret_key: None,
        })
        .with_user_id("user_id")
        .build()?;

    client
        .publish_message("hello world!")
        .channel("my_channel")
        .r#type("text-message")
        .execute()
        .await?;
    Ok(())
}
```

Please be sure to go to our [official docs](https://www.pubnub.com/docs/sdks/rust) for more details. You’ll need to replace the `my_publish_key` and `my_subscribe_key` parameters with your publish keys and subscribe keys, respectively, from your free PubNub account, which ensures proper authentication for your application to connect to the PubNub network.

Our SDK integrates easily with major providers like AWS, Microsoft Azure, GCP, IBM Cloud, Oracle, and Kubernetes, as well as frameworks such as React. Plus, it's open-source on github.com! Developers can freely tweak, optimize, and debug using familiar tools. And remember - the PubNub network is platform and device agnostic, so you never have to worry if implementing Rust into your applications will hurt your current ecosystem.

## What's Next
PubNub Rust is a powerful language that continues to excite developers worldwide with its capabilities by improving their systems and applications - including us! We wanted to create this SDK so other developers can easily and effectively integrate PubNub into their Rust applications.

To get started with the PubNub Rust SDK, check out the following resources:

- [Learn the Essentials of PubNub in Five Minutes](https://www.pubnub.com/tour/introduction/)
- [PubNub Rust SDK Documentation](https://www.pubnub.com/docs/sdks/rust)
- [PubNub Pursues Rust to Deliver Best-in-Class APIs](https://www.pubnub.com/blog/pubnub-pursues-rust-to-deliver-best-in-class-apis/)
- [The exciting world of Rust + ARM processors](https://www.pubnub.com/blog/the-exciting-world-of-rust-arm-processors/)
- [Learn how PubNub powers over 2000 customers](https://www.pubnub.com/blog/the-exciting-world-of-rust-arm-processors/)

We want to reiterate our commitment to improving the experience of Rustaceans using the PubNub Rust SDK. We encourage you to download and try out the new SDK for your real-time projects and provide us with your valuable feedback or questions anytime at devrel@pubnub.com and fork the SDK repository to implement your features to help us further refine and enhance the SDK. Be on the lookout for upcoming guides and tutorials for implementing PubNub into Rust applications. Happy coding, Rustaceans!