Skip to content

to11mtm/nats.net.v2

 
 

Repository files navigation

NATS.NET V2

Preview

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.

NATS.NET V2 Goals

  • Only support Async I/O
  • Target latest .NET LTS Release (currently net6.0)

Packages

  • NATS.Client.Core: core NATS
  • NATS.Client.Hosting: extension to configure DI container
  • NATS.Client.JetStream: JetStream not yet implemented

Basic Usage

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);

Contributing

  • Run dotnet format at root directory of project in order to clear warnings that can be auto-formatted

Attribution

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

About

Full Async C# / .NET client for NATS

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • C# 98.4%
  • Other 1.6%