ruststream-nats¶
ruststream-nats subscribes a RustStream service to
NATS subjects and publishes messages to them. A handler binds to a Core NATS subject or to a
JetStream consumer. NATS matches replies to requests, so a service can send a request and wait for
the answer. A JetStream publish states what it expects of the stream, and the stream refuses it when
that does not hold.
With the testing feature, a test runs the service's production app with NatsBroker in process,
with no NATS server, or against a running one.
[dependencies]
ruststream = { version = "0.7", features = ["macros", "json"] }
ruststream-nats = "0.7"
serde = { version = "1", features = ["derive"] }
[dev-dependencies]
ruststream-nats = { version = "0.7", features = ["testing"] }
A service mounts its handlers on a NatsBroker:
use ruststream_nats::prelude::*;
use serde::Deserialize;
#[derive(Debug, Deserialize)]
struct Order {
id: u64,
}
#[subscriber("orders.created")]
async fn handle(order: &Order) -> HandlerOutcome {
println!("got order {}", order.id);
HandlerOutcome::ack()
}
#[ruststream::app]
fn app() -> impl App {
RustStream::new(AppInfo::new("orders", "0.1.0")).with_broker(
NatsBroker::new("nats://localhost:4222"),
|b| {
b.include(handle);
},
)
}
What the crate offers¶
The crate reference is the textbook for all of it, and each topic is one section of it:
- Three subscription descriptors, one per form a NATS subscription takes: one subject, a pattern, and a JetStream pull consumer.
- Two publish policies: plain Core NATS, and one that waits for the stream to acknowledge the message.
- The deduplication id and the stream expectations one JetStream publish states about itself.
- Acknowledgement and delayed redelivery, which a JetStream consumer settles on the server and a Core subject does not settle at all.
- The AsyncAPI document
the crate fills in, and testing:
the production app under the
TestAppharness, in process or against a live server.
Where to go next¶
- NATS reference - the crate itself: descriptors, policies, per-message settings, testing.
- RustStream docs - installation, the tutorial, the list of brokers.
- Framework reference - subscribers, routing, codecs, middleware, the CLI.
How this site relates to the RustStream docs¶
This page is where NATS starts; what it is made of is in the crate reference. The framework itself is documented with the core crate, and its entry pages are on the RustStream site.