57 releases (14 breaking)

0.15.2 Sep 15, 2021
0.14.0 Sep 1, 2021
0.10.1 Jul 21, 2021
0.9.8 Mar 22, 2021
0.1.1 Sep 8, 2015

#8 in Network programming

Download history 3754/week @ 2021-07-03 3628/week @ 2021-07-10 3427/week @ 2021-07-17 3535/week @ 2021-07-24 2823/week @ 2021-07-31 3787/week @ 2021-08-07 3731/week @ 2021-08-14 4119/week @ 2021-08-21 3570/week @ 2021-08-28 3473/week @ 2021-09-04 4916/week @ 2021-09-11 4800/week @ 2021-09-18 5334/week @ 2021-09-25 5033/week @ 2021-10-02 5363/week @ 2021-10-09 5782/week @ 2021-10-16

15,398 downloads per month
Used in 34 crates (20 directly)



A Rust client for the NATS messaging system.


License Apache 2 Crates.io Documentation Build Status


Rust may be the most interesting new language 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 a first class citizen in Rust. We also wanted to be aligned with the NATS philosophy of simple, secure, and fast!


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.

let nc = nats::connect("demo.nats.io")?;

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

let nc3 = nats::Options::with_credentials("path/to/my.creds")

let nc4 = nats::Options::new()


nc.publish("my.subject", "Hello World!")?;

nc.publish("my.subject", "my message")?;

// Publish a request manually.
let reply = nc.new_inbox();
let rsub = nc.subscribe(&reply)?;
nc.publish_request("my.subject", &reply, "Help me!")?;


let sub = nc.subscribe("foo")?;
for msg in sub.messages() {}

// Using next.
if let Some(msg) = sub.next() {}

// Other iterators.
for msg in sub.try_iter() {}
for msg in sub.timeout_iter(Duration::from_secs(10)) {}

// Using a threaded handler.
let sub = nc.subscribe("bar")?.with_handler(move |msg| {
    println!("Received {}", &msg);

// Queue subscription.
let qsub = nc.queue_subscribe("foo", "my_group")?;


let resp = nc.request("foo", "Help me?")?;

// With a timeout.
let resp = nc.request_timeout("foo", "Help me?", Duration::from_secs(2))?;

// With multiple responses.
for msg in nc.request_multi("foo", "Help")?.iter() {}

// Publish a request manually.
let reply = nc.new_inbox();
let rsub = nc.subscribe(&reply)?;
nc.publish_request("foo", &reply, "Help me!")?;
let response = rsub.iter().take(1);

Minimum Supported Rust Version (MSRV)

The minimum supported Rust version is 1.51.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.


The following is a list of features currently supported and planned for the near future.

  • Basic Publish/Subscribe
  • Request/Reply - Singelton and Streams
  • Authentication
    • Token
    • User/Password
    • Nkeys
    • User JWTs (NATS 2.0)
  • Reconnect logic
  • TLS support
  • Direct async support
  • Crates.io listing
  • Header Support

Miscellaneous TODOs

  • Ping timer
  • msg.respond
  • Drain mode
  • COW for received messages
  • Sub w/ handler can't do iter()
  • Backup servers for option
  • Travis integration


~372K SLoC