GitHub - nats-io/nats.net: Async .NET client for NATS: pub/sub, request/reply, JetStream, KV, Object Store, Services (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("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("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();

Note

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.

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.

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.

Join the Community

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

Contributing Code

Note

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