qm-nats 0.0.72

Nats helper functions
Documentation

NATS JetStream integration for building distributed microservices.

This crate provides utilities for connecting to NATS with JetStream support, enabling event-driven architectures with distributed locking and sequencing capabilities.

Features

  • Event Publishing: Stream events to NATS JetStream with structured subject paths
  • Distributed Locks: Acquire and manage distributed locks across services
  • Sequence Generation: Generate unique, monotonically increasing sequences
  • System Consumers: Create durable pull consumers for event processing
  • Configuration: Environment-based configuration with sensible defaults

Quick Start

use qm_nats::{Config, Nats};

#[tokio::main]
async fn main() -> anyhow::Result<()> {
    let config = Config::new()?;
    let nats = Nats::new(config).await?;

    // Create a publisher
    let publisher = nats.publisher().await?;
    publisher.publish("subject.here", &"hello").await?;

    // Or use distributed locks
    let locks = nats.distributed_locks().await?;
    let lock_manager = locks.sys_locks().await?;
    let result = lock_manager.run_locked("my-resource", async {
        // Critical section
        Ok::<_, std::convert::Infalloid>(42)
    }).await?;

    Ok(())
}

Environment Variables

Variable Description Default
NATS_HOST NATS server host 127.0.0.1
NATS_PORT NATS server port 4222
NATS_APP_NAME Application name edd-service-rs
NATS_SYS_LOCKS Key-value bucket for locks SYS_LOCKS
NATS_EVENTS_STREAM_NAME JetStream stream for events EVENTS
NATS_EVENTS_STREAM_SUBJECT Subject pattern for events ev.>