The NATS.NET V2 client is in preview and not recommended for production use. Codebase is still under heavy development and currently we only have implementations for core NATS features.
Please test and provide feedback by visiting our Slack channel.
- Only support Async I/O
- Target latest .NET LTS Release (currently
net6.0
)
- NATS.Client.Core: core NATS
- NATS.Client.Hosting: extension to configure DI container
- NATS.Client.JetStream: JetStream not yet implemented
Download the latest nats-server
for your platform and run it without any arguments. nats-server
will listen
on its default TCP port 4222.
Given that we have a plain class Bar
, we can publish and subscribe to our nats-server
sending
and receiving Bar
objects:
public record Bar
{
public int Id { get; set; }
public string Name { get; set; }
}
Subscribe to all bar
related subjects:
await using var nats = new NatsConnection(options);
await using sub = await nats.SubscribeAsync<Bar>("bar.>");
await foreach (var msg in sub.Msgs.ReadAllAsync())
{
Console.WriteLine($"Received {msg.Subject}: {msg.Data}\n");
}
Publish Bar
objects to related bar
subjects:
await using var nats = new NatsConnection();
for (int i = 0; i < 10; i++)
{
Console.WriteLine($" Publishing {i}...");
await nats.PublishAsync<Bar>($"bar.baz.{i}", new Bar { Id = i, Name = "Baz" });
}
You should also hook your logger to NatsConnection
to make sure all is working as expected or
to get help diagnosing any issues you might have:
var options = NatsOptions.Default with { LoggerFactory = new MinimumConsoleLoggerFactory(LogLevel.Error) };
await using var nats = new NatsConnection(options);
- Run
dotnet format
at root directory of project in order to clear warnings that can be auto-formatted
This library is based on the excellent work in Cysharp/AlterNats