Skip to content

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:

crates/ruststream-nats/examples/nats_core.rs
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:

Where to go next

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.