72 releases

new 0.25.0 Apr 22, 2024
0.24.1 Nov 8, 2023
0.24.0 Feb 13, 2023
0.23.1 Nov 11, 2022
0.1.1 Sep 8, 2015

#104 in Network programming

Download history 8028/week @ 2024-01-01 12140/week @ 2024-01-08 12660/week @ 2024-01-15 14267/week @ 2024-01-22 14206/week @ 2024-01-29 12590/week @ 2024-02-05 12647/week @ 2024-02-12 12696/week @ 2024-02-19 15564/week @ 2024-02-26 16038/week @ 2024-03-04 13603/week @ 2024-03-11 12815/week @ 2024-03-18 13964/week @ 2024-03-25 13984/week @ 2024-04-01 16533/week @ 2024-04-08 18425/week @ 2024-04-15

63,384 downloads per month
Used in 33 crates (27 directly)

Apache-2.0

425KB
7K SLoC

A Rust client for the NATS messaging system.

Status

License Apache 2 Crates.io Documentation Build Status

⚠️ Legacy Notice

This is the old legacy client. It will not get new features or updates beyond critical security fixes.

Use async nats instead.

If you would like to use the async client in sync context, check the async-nats examples.

Motivation

Rust may be one of the most interesting new languages the NATS ecosystem has seen. We believe this client will have a large impact on NATS, distributed systems, and embedded and IoT environments. With Rust, we wanted to be as idiomatic as we could be and lean into the strengths of the language. We moved many things that would have been runtime checks and errors to the compiler, most notably options on connections, and having subscriptions generate multiple styles of iterators since iterators are first-class citizens in Rust. We also wanted to be aligned with the NATS philosophy of simple, secure, and fast!

Feedback

We encourage all folks in the NATS and Rust ecosystems to help us improve this library. Please open issues, submit PRs, etc. We're available in the rust channel on the NATS slack as well!

Example Usage

> cargo run --example nats-box -- -h

Basic connections, and those with options. The compiler will force these to be correct.

fn main() -> std::io::Result<()> {
let nc = nats::connect("demo.nats.io")?;

let nc2 = nats::Options::with_user_pass("derek", "s3cr3t!")
    .with_name("My Rust NATS App")
    .connect("127.0.0.1")?;

let nc3 = nats::Options::with_credentials("path/to/my.creds")
    .connect("connect.ngs.global")?;

let nc4 = nats::Options::new()
    .add_root_certificate("my-certs.pem")
    .connect("tls://demo.nats.io:4443")?;
Ok(())
}

This will attempt to bind to an existing consumer if it exists, otherwise it will create a new internally managed consumer resource that gets destroyed when the subscription is dropped.

Minimum Supported Rust Version (MSRV)

The minimum supported Rust version is 1.58.0.

Sync vs Async

The Rust ecosystem has a diverse set of options for async programming. This client library can be used with any async runtime out of the box, such as async-std and tokio.

The async interface provided by this library is implemented as just a thin wrapper around its sync interface. Those two interface styles look very similar, and you're free to choose whichever works best for your application.

NOTE: This crate uses a thread pool from blocking crate. By default, it limits the number of threads to 500. It can be overridden by setting BLOCKING_MAX_THREADS environment variable and set between 1 and 10000. Be careful when spinning a lot of async operations, as it may drain the thread pool and block forever until it's reworked

Dependencies

~19–32MB
~583K SLoC