Crate nats[][src]

Expand description

A Rust client for the ecosystem.

git clone is a simple, secure and high performance open source messaging system for cloud native applications, IoT messaging, and microservices architectures.

For async API refer to the async-nats crate.

For more information see


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

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

let nc = nats::connect("")?;

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()


let nc = nats::connect("")?;
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 nc = nats::connect("")?;
let sub = nc.subscribe("foo")?;
for msg in sub.messages() {}

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

// 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 nc = nats::connect("")?;
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);


pub use rustls;


Async-enabled NATS client. An async Rust client for the ecosystem.

JetStream stream management and consumers. Support for the JetStream at-least-once messaging system.


A NATS connection.

A multi-map from header name to a set of values for that header

A message received on a subject.

Connect options.

A Subscription receives Messages published to specific NATS Subjects.


Connect to a NATS server at the given url.