Skip to main content

Crate ruststream_nats

Crate ruststream_nats 

Source
Expand description

NATS broker for the RustStream messaging framework, backed by async-nats: Core NATS subjects and JetStream streams over one connection.

Core NATS hands a message to whoever is subscribed at that moment and stores nothing. JetStream stores it in a stream, which a pull consumer reads at its own pace and acknowledges message by message. Both models run on the same connection, so a service chooses per handler and per publish.

§A service

A handler is an async fn over the decoded payload, mounted on a broker inside the application object, and the attribute writes the main:

use ruststream_nats::prelude::*;
use serde::Deserialize;

#[derive(Deserialize)]
struct Order {
    id: u64,
}

#[subscriber("orders.created")]
async fn handle(order: &Order) -> HandlerOutcome {
    tracing::info!(order.id, "accepted");
    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);
        },
    )
}

cargo run -- run starts it. NatsBroker::new only records the configuration; the runtime connects the broker at startup and drains it when the service stops.

§Where things are

The guides take a NATS service from installation to production, in this order: getting started, subjects, wildcards and queue groups, streams and consumers, acknowledgement and delivery guarantees, publishing to streams, retries and dead letters, request-reply, testing and connecting to production. The architecture pages show how the crate sits between async-nats and the runtime, and the benchmarks what it costs.

Each topic below links to its component page on the site, which shows it in a service; the item’s own documentation keeps the detail.

§Broker

§Subscriptions

§Settlement

§Context

§Publishing

§Request-reply

§Tooling

§Cargo features

Both are off by default and additive. testing adds the in-process mode TestApp::start connects the broker through, and asyncapi contributes the NATS bindings to the generated document. The codecs, the macros and the harness itself are features of the core crate.

Modules§

context
Native JetStream metadata of a delivery, read by a handler through Ctx<K>.
prelude
The imports a service on NATS writes every time, in one glob.

Structs§

ClosedNatsBroker
The terminal witness returned by shutting down a ConnectedNatsBroker.
ConnectedNatsBroker
The typed witness that Broker::connect succeeded: holds the live connection.
CoreMessage
Wrapper around an async_nats::Message from a core (non-JetStream) subscription.
CoreSubject
One subject subscribed to over Core NATS.
CoreWildcard
A pattern subscribed to over Core NATS: orders.*, orders.>, >.
JetStreamMessage
Wrapper around an async_nats::jetstream::Message with ack semantics.
JetStreamOptions
What one JetStream publish states about itself, over and above its subject and payload.
JetStreamPublish
The JetStream publish policy: pure declaration, constructible anywhere.
JetStreamPublisher
The live JetStream publisher. Cheap to clone.
JetStreamSubject
A subject read through a JetStream pull consumer on one stream.
NatsBroker
A NATS broker: configuration captured, no I/O performed yet.
NatsPublish
The Core NATS publish policy: pure declaration, constructible anywhere.
NatsPublisher
The live Core NATS publisher. Cheap to clone.
NatsSubscriber
A NATS subscription.
NonZeroDuration
A Duration that is known not to be zero.
PublishAck
The acknowledgement a JetStream stream returns for an accepted publish. PublishAck is an acknowledgment received after successfully publishing a message.

Enums§

DeliverPolicy
DeliverPolicy determines how the consumer should select the first message to deliver.
NatsError
Errors surfaced by the NATS broker implementation.
NatsMessage
A NATS delivery. Two flavours: core NATS (no ack) and JetStream (real ack/nack/redelivery).

Constants§

PARTITION_KEY_HEADER
The well-known header key for per-message routing / partitioning.

Traits§

JetStreamPublishSteps
The per-message JetStream settings, as steps on the publish builder.
NatsPublishPolicy
A publish policy that pairs with a connected NATS broker without I/O.
NatsSubscription
A NATS subscription descriptor: CoreSubject, CoreWildcard or JetStreamSubject.