NATS.Net 2.8.1 (original) (raw)

License Apache 2.0 NuGet Doc Test Linux Test Windows Slack

NATS .NET is the .NET client for NATS, a distributed messaging system. It provides pub/sub and request/reply (Core NATS), streaming and persistence (JetStream), Key-Value Store, Object Store, and Services.

Check out DOCS for guides and examples.

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

Quick Start

Start a NATS server:

docker run -p 4222:4222 nats

Create a subscriber app:

dotnet new console -n Sub && cd Sub && dotnet add package NATS.Net
using NATS.Net;

await using var nc = new NatsClient();

await foreach (var msg in nc.SubscribeAsync<string>("greet"))
    Console.WriteLine($"Received: {msg.Data}");

In another terminal, create a publisher app:

dotnet new console -n Pub && cd Pub && dotnet add package NATS.Net
using NATS.Net;

await using var nc = new NatsClient();

await nc.PublishAsync("greet", "Hello, NATS!");

API at a Glance

using NATS.Net;

await using var nc = new NatsClient();

// Publish a message
await nc.PublishAsync("orders.new", new Order(Id: 1, Item: "widget"));

// Subscribe with async enumerable
await foreach (var msg in nc.SubscribeAsync<Order>("orders.>"))
    Console.WriteLine($"Received order: {msg.Data}");

// Request-reply
var order = new Order(Id: 2, Item: "gadget");
var reply = await nc.RequestAsync<Order, Confirmation>("orders.create", order);

// JetStream (persistent messaging)
var js = nc.CreateJetStreamContext();

// Key/Value Store
var kv = nc.CreateKeyValueStoreContext();

// Object Store
var obj = nc.CreateObjectStoreContext();

// Services
var svc = nc.CreateServicesContext();

We are not testing with .NET 6.0 target anymore even though it is still targeted by the library. This is to reduce the number of test runs and speed up the CI process as well as to prepare for the next major version, possibly dropping .NET 6.0 support.

**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.

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.

NATS .NET Goals

Packages

Client and Orbit

NATS client functionality is split across two layers: the core client(NATS.Net, this repo) and Orbit, a separate set of packages with higher-level utilities.

The split exists so the core can stay small, stable, and consistent across NATS clients in every language, while Orbit can iterate quickly on opinionated abstractions without dragging the core API along for the ride.

Core client (NATS.Net)

Orbit (orbit.net)

What goes where?

Concern Core (NATS.Net) Orbit
Connect, publish, subscribe, request/reply
JetStream publish, consumers, streams, KV, OS
Service API (request/reply micro-services)
Wire-protocol coverage, auth, TLS, reconnection
Cross-client parity, conservative semver
Opinionated helpers / sugar over core APIs
New experimental patterns (e.g. partitioned groups)
KV codecs, distributed counters, NATS contexts
.NET-idiomatic abstractions with no parity mandate
Per-utility versioning, faster API churn allowed

Rule of thumb: if it is a thin mapping of something nats-serveralready speaks and every official client must expose it, it belongs in core. If it is a pattern, helper, or abstraction layered on top, it belongs in Orbit.

Layering

   ┌──────────────────────────────────────────────────────┐
   │  Application code                                    │
   └──────────────┬───────────────────────────┬───────────┘
                  │                           │
                  ▼                           ▼
        ┌───────────────────┐       ┌───────────────────┐
        │ Orbit packages    │  uses │ NATS.Net (core)   │
        │ (opinionated,     │──────▶│ (parity, stable,  │
        │  per-pkg semver)  │       │  protocol-level)  │
        └───────────────────┘       └─────────┬─────────┘
                                              │
                                              ▼
                                       ┌─────────────┐
                                       │ nats-server │
                                       └─────────────┘

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.

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

Contributing Code

Please make sure to sign your commits. All commits must be signed before a Pull Request can be merged.

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

Attribution

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

Product Compatible and additional computed target framework versions.
.NET net5.0 was computed. net5.0-windows was computed. net6.0 is compatible. net6.0-android was computed. net6.0-ios was computed. net6.0-maccatalyst was computed. net6.0-macos was computed. net6.0-tvos was computed. net6.0-windows was computed. net7.0 was computed. net7.0-android was computed. net7.0-ios was computed. net7.0-maccatalyst was computed. net7.0-macos was computed. net7.0-tvos was computed. net7.0-windows was computed. net8.0 is compatible. net8.0-android was computed. net8.0-browser was computed. net8.0-ios was computed. net8.0-maccatalyst was computed. net8.0-macos was computed. net8.0-tvos was computed. net8.0-windows was computed. net9.0 was computed. net9.0-android was computed. net9.0-browser was computed. net9.0-ios was computed. net9.0-maccatalyst was computed. net9.0-macos was computed. net9.0-tvos was computed. net9.0-windows was computed. net10.0 was computed. net10.0-android was computed. net10.0-browser was computed. net10.0-ios was computed. net10.0-maccatalyst was computed. net10.0-macos was computed. net10.0-tvos was computed. net10.0-windows was computed.
.NET Core netcoreapp2.0 was computed. netcoreapp2.1 was computed. netcoreapp2.2 was computed. netcoreapp3.0 was computed. netcoreapp3.1 was computed.
.NET Standard netstandard2.0 is compatible. netstandard2.1 is compatible.
.NET Framework net461 was computed. net462 was computed. net463 was computed. net47 was computed. net471 was computed. net472 was computed. net48 was computed. net481 was computed.
MonoAndroid monoandroid was computed.
MonoMac monomac was computed.
MonoTouch monotouch was computed.
Tizen tizen40 was computed. tizen60 was computed.
Xamarin.iOS xamarinios was computed.
Xamarin.Mac xamarinmac was computed.
Xamarin.TVOS xamarintvos was computed.
Xamarin.WatchOS xamarinwatchos was computed.