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.
Start a NATS server:
docker run -p 4222:4222 natsCreate a subscriber app:
dotnet new console -n Sub && cd Sub && dotnet add package NATS.Netusing 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.Netusing NATS.Net;
await using var nc = new NatsClient();
await nc.PublishAsync("greet", "Hello, NATS!");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();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+.
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.
- Only support Async I/O (async/await)
- Target .NET Standard 2.0, 2.1, and the two most recent .NET LTS releases (currently .NET 6.0 & .NET 8.0)
- NATS.Net: Meta package that includes all other packages except extensions
- NATS.Client.Core: Core NATS
- NATS.Client.JetStream: JetStream
- NATS.Client.KeyValueStore: Key/Value Store
- NATS.Client.ObjectStore: Object Store
- NATS.Client.Services: Services
- NATS.Client.Simplified: simplify common use cases especially for beginners
- NATS.Client.Serializers.Json: JSON serializer for ad-hoc types
- NATS.Extensions.Microsoft.DependencyInjection: extension to configure DI container
You are welcome to contribute to this project. Here are some steps to get you started:
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:
- Join the NATS Slack and find us on the
#dotnetchannel - Join the discussion on GitHub Discussions
- Follow us on X @nats_io
Note
Please make sure to sign your commits. All commits must be signed before a Pull Request can be merged.
- Read the Contributor Guide
- Fork the repository and create a branch
- Open
NATS.Net.slnsolution in Visual Studio, Rider or VS Code (or any other editor of your choice) - Make changes and write tests
- Run tests against a locally installed NATS server in your PATH
- Note that some tests are still not reliable locally, so CI will run all tests
- For a quick check, run
NATS.Client.Platform.Windows.Testswhich is a subset of tests that should pass on Windows - You can also locally run
NATS.Client.CoreUnit.TestsandNATS.Client.Core2.Testswhich are more stable - Run
dotnet formatat root directory of project to clear warnings that can be auto-formatted - Run
dotnet buildat root directory and make sure there are no errors or warnings - Submit a pull request
Please also check out the Contributor Guide and Code of Conduct.
This library is based on the excellent work in Cysharp/AlterNats