[−][src]Crate nats
A Rust client for the NATS.io ecosystem.
git clone https://github.com/nats-io/nats.rs
NATS.io is a simple, secure and high performance open source messaging system for cloud native
applications, IoT
messaging, and microservices architectures.
For more information see https://nats.io/.
Examples
> 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") .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")?;
Publish
let nc = nats::connect("demo.nats.io")?; 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!")?;
Subscribe
let nc = nats::connect("demo.nats.io")?; 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); Ok(()) }); // Queue subscription. let qsub = nc.queue_subscribe("foo", "my_group")?;
Request/Response
let nc = nats::connect("demo.nats.io")?; 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);
Re-exports
pub use subscription::Subscription; |
Modules
subscription | Functionality relating to subscribing to a subject. |
Structs
Connection | A NATS connection. |
Headers | A multi-map from header name to a set of values for that header |
Message | A |
Options | Connect options. |
Functions
connect | Connect to a NATS server at the given url. |