Home

Awesome

License Apache 2.0 NuGet

NATS .NET

NATS .NET is a client library designed to connect to the NATS messaging server, fully supporting all NATS features. It integrates seamlessly with modern .NET asynchronous interfaces such as async enumerables and channels, and leverages advanced .NET memory, buffer and IO features.

Check out NATS .NET client library documentation for guides and examples.

[!NOTE] Don't confuse NuGet packages! NATS .NET package on NuGet is called NATS.Net. There is another package called NATS.Client which is the older version of the client library and will be deprecated eventually.

[!TIP] NATS .NET now supports .NET Standard 2.0 and 2.1 along with .NET 6.0 and 8.0, which means you can also use it with .NET Framework 4.6.2+ and Unity 2018.1+.

What is NATS?

NATS is a high-performance, secure, distributed messaging system. It's a connective technology tailored for modern distributed systems, facilitating efficient addressing, discovery, and message exchange. It supports dynamic service and stream processing across various locations and devices, enhancing mobility, security, and independence from traditional constraints such as DNS.

Head over to NATS documentation for more information.

Quick Start

Basic messaging:

// NATS core M:N messaging example
await using var nc = new NatsClient();

// Subscribe on one terminal
await foreach (var msg in nc.SubscribeAsync<string>(subject: "foo"))
{
    Console.WriteLine($"Received: {msg.Data}");
}

// Start publishing to the same subject on a second terminal
await nc.PublishAsync(subject: "foo", data: "Hello, World!");

Persistance with JetStream:

await using var nc = new NatsClient();
var js = nc.CreateJetStreamContext();

// Create a stream to store the messages
await js.CreateStreamAsync(new StreamConfig(name: "ORDERS", subjects: new[] { "orders.*" }));

// Publish a message to the stream. The message will be stored in the stream
// because the published subject matches one of the the stream's subjects.
var ack = await js.PublishAsync(subject: "orders.new", data: "order 1");
ack.EnsureSuccess();

// Create a consumer on a stream to receive the messages
var consumer = await js.CreateOrUpdateConsumerAsync("ORDERS", new ConsumerConfig("order_processor"));

await foreach (var jsMsg in consumer.ConsumeAsync<string>())
{
    Console.WriteLine($"Processed: {jsMsg.Data}");
    await jsMsg.AckAsync();
}

See more details, including how to download and start NATS server and JetStream in our documentation.

Additionally check out NATS by example - An evolving collection of runnable, cross-client reference examples for NATS.

NATS .NET Goals

Packages

Contributing

You are welcome to contribute to this project. Here are some steps to get you started:

Reporting Bugs and Feature Requests

You can report bugs and request features by opening an issue on GitHub.

Join the Community

You can join the community asking questions, sharing ideas, and helping others:

Contributing Code

Please also check out the Contributor Guide and Code of Conduct.

Attribution

This library is based on the excellent work in Cysharp/AlterNats