Table of Contents

Class NatsEventBus

Namespace
Savvyio.Extensions.NATS.EventDriven
Assembly
Savvyio.Extensions.NATS.dll

Provides a NATS implementation of the IPublishSubscribeChannel<TRequest> for integration event messages.

public class NatsEventBus : NatsMessage, IDisposable, IAsyncDisposable, IHealthCheckProvider<INatsConnection>, IPublishSubscribeChannel<IIntegrationEvent>, IPublisher<IIntegrationEvent>, ISubscriber<IIntegrationEvent>
Inheritance
NatsEventBus
Implements
IHealthCheckProvider<INatsConnection>
Derived
Inherited Members

Examples

NatsEventBus is the NATS JetStream event bus that publishes and subscribes to IMessage<IIntegrationEvent> envelopes. Configure it with NatsEventBusOptions and IMarshaller.

using System;
using Savvyio.EventDriven;
using Savvyio.Extensions.NATS.EventDriven;
using Savvyio.Messaging;

namespace ExampleApp;

public class NatsEventBusConfig
{
    public static NatsEventBusOptions CreateOptions()
    {
        return new NatsEventBusOptions
        {
            NatsUrl = new Uri("nats://localhost:4222"),
            Subject = "integration-events"
        };
    }

    public static IPublishSubscribeChannel<IIntegrationEvent> AsChannel(NatsEventBus bus) => bus;
}

Constructors

NatsEventBus(IMarshaller, NatsEventBusOptions)

Initializes a new instance of the NatsEventBus class.

public NatsEventBus(IMarshaller marshaller, NatsEventBusOptions options)

Parameters

marshaller IMarshaller

The marshaller used for serializing and deserializing messages.

options NatsEventBusOptions

The NatsEventBusOptions used to configure this instance.

Exceptions

ArgumentNullException

marshaller cannot be null - or - options cannot be null.

ArgumentException

options are not in a valid state.

Methods

PublishAsync(IMessage<IIntegrationEvent>, Action<AsyncOptions>)

Publishes the specified integration event message asynchronously to the configured NATS subject.

public Task PublishAsync(IMessage<IIntegrationEvent> message, Action<AsyncOptions> setup = null)

Parameters

message IMessage<IIntegrationEvent>

The message to publish.

setup Action<AsyncOptions>

The AsyncOptions which may be configured.

Returns

Task

A Task that represents the asynchronous operation.

PublishMessageAsync(string, string, NatsHeaders, CancellationToken)

Publishes a serialized message to NATS.

protected virtual Task PublishMessageAsync(string subject, string message, NatsHeaders headers, CancellationToken cancellationToken)

Parameters

subject string

The subject to publish to.

message string

The serialized message payload.

headers NatsHeaders

The message headers.

cancellationToken CancellationToken

The cancellation token of the asynchronous operation.

Returns

Task

A task that represents the asynchronous operation.

SubscribeAsync(Func<IMessage<IIntegrationEvent>, CancellationToken, Task>, Action<SubscribeAsyncOptions>)

Subscribes to integration event messages from the configured NATS subject and invokes the specified asynchronous handler for each received message.

public Task SubscribeAsync(Func<IMessage<IIntegrationEvent>, CancellationToken, Task> asyncHandler, Action<SubscribeAsyncOptions> setup = null)

Parameters

asyncHandler Func<IMessage<IIntegrationEvent>, CancellationToken, Task>

The function delegate that will handle the message.

setup Action<SubscribeAsyncOptions>

The SubscribeAsyncOptions which may be configured.

Returns

Task

A Task that represents the asynchronous operation.

SubscribeMessagesAsync(string, NatsSubOpts, CancellationToken)

Subscribes to NATS messages.

protected virtual IAsyncEnumerable<NatsEventBus.ReceivedNatsMessage> SubscribeMessagesAsync(string subject, NatsSubOpts options, CancellationToken cancellationToken)

Parameters

subject string

The subject to subscribe to.

options NatsSubOpts

The subscription options.

cancellationToken CancellationToken

The cancellation token of the asynchronous operation.

Returns

IAsyncEnumerable<NatsEventBus.ReceivedNatsMessage>

An asynchronous sequence of received NATS messages.

See Also