API reference — Transports

Nuvora.Nexus.Relay.Messaging.Nats

NATS JetStream message transport for Nuvora Nexus Relay

dotnet add package Nuvora.Nexus.Relay.Messaging.Nats

Nuvora.Nexus.Relay.Messaging.Nats

NatsJetStreamDeadLetterReplayer

public sealed class NatsJetStreamDeadLetterReplayer : IDeadLetterQueueReplayer, IAsyncDisposable

IDeadLetterQueueReplayer for NATS JetStream: drains a queue’s dead-letter subject (from the companion dead-letter stream) and republishes each message to its original event subject so it is redelivered after the underlying defect is fixed. Each message is re-published (and the JetStream publish confirmed) before its dead-letter copy is acked, so a crash mid-replay duplicates rather than loses — duplicates are absorbed by the consumer’s inbox dedup.

Constructors

NatsJetStreamDeadLetterReplayer(IOptions<NatsOptions> options, ILogger<NatsJetStreamDeadLetterReplayer> logger)

Methods

Task<int> ReplayAsync(string queueName, int maxMessages = 100, CancellationToken cancellationToken = default(CancellationToken))

Replays up to maxMessages messages from queueName’s dead-letter queue back onto queueName. Each message is re-enqueued (confirmed by the broker) before its DLQ copy is acknowledged, so a crash mid-replay duplicates rather than loses — duplicates are absorbed by the consumer’s inbox dedup. Returns the number replayed.

ValueTask DisposeAsync()

Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources asynchronously.

NatsJetStreamMessageBroker

public sealed class NatsJetStreamMessageBroker : IMessageBroker, IAsyncDisposable

IMessageBroker over NATS JetStream. Publishes events to subjects under NatsOptions.EventSubjectRoot with the routing key in the trailing tokens; a JetStream publish only completes once the server has stored (acked) the message, so a failed publish throws and the outbox records it rather than silently dropping the event.

Durable retry/dead-lettering is owned by the outbox, so this performs only a short in-call retry for transient blips. There is no per-consumer “unroutable” signal — a message stored in the stream with no matching consumer is simply retained — so DetectsUnroutableMessages is false.

Constructors

NatsJetStreamMessageBroker(IOptions<NatsOptions> options, ILogger<NatsJetStreamMessageBroker> logger)

Methods

Task PublishAsync(MessageBrokerMessage message, CancellationToken cancellationToken = default(CancellationToken))

Publish a message to the message broker

Task PublishManyAsync(IEnumerable<MessageBrokerMessage> messages, CancellationToken cancellationToken = default(CancellationToken))

Publish multiple messages to the message broker

Task<bool> PingAsync(CancellationToken cancellationToken = default(CancellationToken))

Connectivity probe for the health check: round-trips a NATS PING to the server.

ValueTask DisposeAsync()

Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources asynchronously.

NatsJetStreamMessageConsumer

public sealed class NatsJetStreamMessageConsumer : IMessageConsumer, IAsyncDisposable

IMessageConsumer over NATS JetStream. A Relay “queue” is a durable JetStream consumer on the event stream whose filter subjects are the routing keys (no routing keys ⇒ the > catch-all). Consumes with explicit ack: a message is acked only after the handler succeeds, so combined with the inbox it yields exactly-once processing on top of at-least-once delivery.

Poison messages. JetStream tracks delivery attempts natively (NumDelivered). On handler failure the message is nak’d (redelivered, with the native delay used for scheduled retries) until NatsOptions.MaxDeliveryAttempts is reached, then — JetStream has no native dead-letter queue — republished to a dead-letter subject (captured by the companion dead-letter stream) and acked. The attempt cap is enforced here in app code (the JetStream consumer is created with unlimited MaxDeliver) so backpressure naks and requeues can never exhaust the broker-side budget and strand a message unacked. The registered IMessageErrorPolicy, consume filters and IConsumerRedeliveryScheduler compose exactly as for the other transports.

Deliveries are processed sequentially per consumer (acks are order-independent); raise throughput by running multiple consumers. The NATS client recovers connections internally, so this does not implement IMessageConsumerLifecycle — the host waits passively.

Constructors

NatsJetStreamMessageConsumer(IOptions<NatsOptions> options, ILogger<NatsJetStreamMessageConsumer> logger, IMessageErrorPolicy? errorPolicy = null, IEnumerable<IConsumeFilter>? consumeFilters = null, IConsumerRedeliveryScheduler? redeliveryScheduler = null)

Methods

Task SubscribeAsync(string queueName, IReadOnlyCollection<string> routingKeys, Func<MessageBrokerMessage, CancellationToken, Task> handler, CancellationToken cancellationToken = default(CancellationToken))
ValueTask DisposeAsync()

Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources asynchronously.

NatsOptions

public sealed class NatsOptions : MessageBrokerOptions

Connection and topology options for the NATS JetStream transport.

Relay’s exchange/routing-key SPI maps onto JetStream as: events publish to a NATS subject <SubjectPrefix>.<routingKey>; a durable JetStream stream (NatsOptions.StreamName) captures <SubjectPrefix>.>; a Relay “queue” is a durable consumer on that stream whose filter subjects are the routing keys (no routing keys ⇒ the > catch-all). JetStream has no native dead-letter queue, so poison messages are republished to a dead-letter subject captured by a companion NatsOptions.DeadLetterStreamName.

Properties

bool AutoCreateStreams { get; set; }

Whether to create the streams and consumers if they don’t exist.

int AckWaitSeconds { get; set; }

How long the server waits for an ack before redelivering (seconds). Bounds redelivery latency when a consumer dies mid-handler without nacking.

int MaxConcurrentCalls { get; set; }

Maximum messages a single consumer processes concurrently. Composes with the concurrency-limiter consume filter.

int MaxDeliveryAttempts { get; set; }

Maximum delivery attempts before a message is routed to its dead-letter subject. Maps onto the consumer’s native MaxDeliver and the server’s NumDelivered tracking.

string DeadLetterStreamName { get; set; }

Companion stream capturing the dead-letter subjects (<SubjectPrefix>.dlq.>).

string StreamName { get; set; }

JetStream stream that captures the event subjects.

string SubjectPrefix { get; set; }

Subject prefix events publish under; the stream captures <SubjectPrefix>.>.

string Url { get; set; }

NATS server URL(s), comma-separated (e.g. nats://localhost:4222).

string? ClientName { get; set; }

Connection name shown on the NATS server, for diagnostics.

Nuvora.Nexus.Relay.Messaging.Nats.DependencyInjection

NatsServiceCollectionExtensions

public static class NatsServiceCollectionExtensions

Registration helpers for the NATS JetStream message transport. Interchangeable with AddRelayRabbitMq / AddRelayAzureServiceBus / AddRelayInMemoryTransport — the outbox/inbox processors work unchanged.

Methods

static IServiceCollection AddRelayNats(this IServiceCollection services, Action<NatsOptions> configure)

Nuvora.Nexus.Relay.Messaging.Nats.HealthChecks

NatsHealthCheck

public sealed class NatsHealthCheck : IHealthCheck

Health check for NATS JetStream connectivity: probes the broker with a bounded timeout, so an unreachable broker surfaces as Unhealthy instead of only as outbox publish failures.

Constructors

NatsHealthCheck(IMessageBroker broker)

Methods

Task<HealthCheckResult> CheckHealthAsync(HealthCheckContext context, CancellationToken cancellationToken = default(CancellationToken))

Runs the health check, returning the status of the component being checked.

NatsHealthCheckBuilderExtensions

public static class NatsHealthCheckBuilderExtensions

Health-check registration for the NATS JetStream broker connection.

Methods

static IHealthChecksBuilder AddRelayNats(this IHealthChecksBuilder builder, string name = "relay-nats", IEnumerable<string>? tags = null)

Adds the NATS JetStream connectivity health check (5s bound). Wire it into the readiness endpoint: services.AddHealthChecks().AddRelayNats().