cloops.nats
1.4.13
See the version list below for details.
dotnet add package cloops.nats --version 1.4.13
NuGet\Install-Package cloops.nats -Version 1.4.13
<PackageReference Include="cloops.nats" Version="1.4.13" />
<PackageVersion Include="cloops.nats" Version="1.4.13" />
<PackageReference Include="cloops.nats" />
paket add cloops.nats --version 1.4.13
#r "nuget: cloops.nats, 1.4.13"
#:package cloops.nats@1.4.13
#addin nuget:?package=cloops.nats&version=1.4.13
#tool nuget:?package=cloops.nats&version=1.4.13
CLOOPS NATS SDK
A modern, annotation-based SDK for building reliable distributed systems with NATS. Define your message consumers with simple attributes and let the framework handle the complexity.
Why NATS?
NATS is a powerful messaging system that enables you to build sophisticated, fault-tolerant distributed systems that are location-transparent and globally distributed.
- Faster than HTTP - Binary protocol with lower overhead
- Fewer hops - Direct communication without load balancers or API gateways
- Globally distributed - Deploy applications worldwide without DNS or complex load balancing. Learn more about NATS super clusters
π‘ Learn more: Check out this podcast from nats.fm for insights into NATS architecture.
Why This SDK?
Modern developers expect annotation-based definitions and dependency injectionβthe same developer experience you get with REST frameworks like ASP.NET Core. This SDK brings that same simplicity to NATS, so you can focus on your business logic instead of boilerplate.
What You Get
- β¨ Annotation-based consumers - Define subscribers with simple attributes
- π‘οΈ Built-in backpressure - Automatic handling of traffic spikes
- βοΈ Flexible load balancing - Choose between broadcasting or load balancing strategies
- π JetStream support - Build temporally decoupled systems with persistent messaging
- π§ Dependency injection - Seamless integration with .NET's DI container
- β
Automatic message validation - Messages with a
Validate()method are automatically validated before processing. Invalid messages are never sent to your handlers - πͺ Consumer interceptors - Inspect, replace, or reject messages before handlers run
- π§― Consumer exception handlers - Map handler exceptions to a
NatsAckfor consistent JetStream ack/nak and Core request/reply responses, while still logging the original exception and recording the invocation as a failure
π― Building microservices? Check out our microservices-focused SDK built on top of
cloops.natsand makes building microservices a breeze!
Quick Start
Installation
Add the cloops.nats package to your .csproj file:
<PackageReference Include="cloops.nats" Version="*" />
Run dotnet restore to install the package.
π‘ Tip: For Connection Loops standard messages and subjects, you may also need
cloops.nats.schema.
Examples
Broadcast Pattern (Kubernetes/Docker)
Ensure each pod/instance receives all messages by using runtime placeholders in the queue group name:
/// <summary>
/// Broadcast: Each pod gets a unique queue group, so all pods receive all messages
/// Supported placeholders: {POD_NAME}, {HOSTNAME}, {MACHINE_NAME}, {ENV:VAR_NAME}
/// </summary>
[NatsConsumer("test.broadcast", QueueGroupName = "pod-{POD_NAME}")]
public Task<NatsAck> BroadcastHandler(NatsMsg<string> msg, CancellationToken ct = default)
{
Console.WriteLine($"[Pod {Environment.GetEnvironmentVariable("POD_NAME")}] Received: {msg.Data}");
return Task.FromResult(new NatsAck(true));
}
Load Balancing Pattern
Distribute messages across multiple instances using the same queue group:
[NatsConsumer("test.lb", QueueGroupName = "workers")]
public async Task<NatsAck> HandleMessage(NatsMsg<string> msg, CancellationToken ct = default)
{
Console.WriteLine($"Instance received: {msg.Data}");
await Task.Delay(100, ct).ConfigureAwait(false); // Simulate work
return new NatsAck(true);
}
Runtime Placeholders
The SDK resolves placeholders dynamically:
{POD_NAME}βPOD_NAMEenv var, falls back toHOSTNAMEor machine name{HOSTNAME}βHOSTNAMEenv var, falls back to machine name{MACHINE_NAME}β Machine name{ENV:VAR_NAME}β Any environment variable (e.g.,{ENV:MY_CUSTOM_VAR})
π Note:
QueueGroupNameis optional. If omitted, an empty string is used, which still enables load balancing. JetStream subscriptions are always load-balanced (no broadcast support).
Timeouts and logging
CloopsNatsClient runs with NATS.Net defaults unless you pass a CloopsNatsClientOptions. Two of those defaults bite production services:
RequestTimeoutis 5 seconds and also bounds the wait for a JetStream publish acknowledgement. A service that publishes to JetStream from a request path and is briefly starved (thread-pool, GC) can miss that window even though the server replied, and getsNatsJSPublishNoResponseException.LoggerFactoryis aNullLoggerFactory, so NATS.Net's own connect, reconnect, ping and retry diagnostics are discarded.
Always pass a logger factory, and raise RequestTimeout to 15-30 seconds for services that publish to JetStream from request paths:
var cnc = new CloopsNatsClient(
url: "nats://dev.nats.cloops.in:4222",
name: "my-service",
creds: natsCreds,
options: new CloopsNatsClientOptions
{
RequestTimeout = TimeSpan.FromSeconds(30), // JetStream publish ack wait, request/reply
LoggerFactory = loggerFactory, // surfaces NATS.Net's internal diagnostics
// CommandTimeout, ConnectTimeout are available too; anything else via the escape hatch:
Configure = o => o with { MaxReconnectRetry = -1 },
});
logger.LogInformation("NATS RequestTimeout={Timeout}", cnc.Connection.Opts.RequestTimeout);
Every property is optional and an unset property keeps the NATS.Net default, so existing callers using the three argument constructor are unchanged. With a LoggerFactory set (directly or through Configure) the client also logs the connection lifecycle events (ConnectionOpened, ConnectionDisconnected, ReconnectFailed, MessageDropped, LameDuckModeActivated) under the CLOOPS.NATS.CloopsNatsClient category; set LogConnectionEvents = false if you subscribe to them yourself. The client sets SubPendingChannelFullMode = Wait, so a full subscription channel applies backpressure instead of dropping messages and MessageDropped will rarely if ever fire under normal operation. The effective options are exposed as cnc.Options.
Learn More
- π Examples - See real-world usage patterns
- π Full Documentation - Detailed guides, setup instructions, and API reference
Contributing
Contributions are welcome! Please feel free to submit a Pull Request.
License
This project is licensed under the MIT License - see the LICENSE file for details.
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | net9.0 is compatible. 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. |
NuGet packages (1)
Showing the top 1 NuGet packages that depend on cloops.nats:
| Package | Downloads |
|---|---|
|
cloops.microservices
Opinionated SDK to effortlessly build lean highly performant cloud native microservices |
GitHub repositories
This package is not used by any popular GitHub repositories.
| Version | Downloads | Last Updated |
|---|---|---|
| 2.0.0 | 1,489 | 10/12/2026 |
| 1.4.13 | 75 | 10/12/2026 |
| 1.3.12 | 324 | 9/13/2026 |
| 1.2.11 | 215 | 9/7/2026 |
| 1.1.9 | 2,391 | 7/26/2026 |
| 1.1.8 | 139 | 7/26/2026 |
| 1.1.7 | 868 | 7/8/2026 |
| 1.1.6 | 6,712 | 12/14/2025 |
| 1.1.5 | 236 | 12/14/2025 |
| 1.1.4 | 236 | 12/14/2025 |
| 1.1.3 | 189 | 12/14/2025 |
| 1.1.2 | 190 | 12/14/2025 |
| 1.1.1 | 196 | 12/13/2025 |
| 1.0.42 | 393 | 11/17/2025 |
| 1.0.41 | 304 | 11/9/2025 |
| 1.0.40 | 239 | 11/9/2025 |
| 1.0.39 | 181 | 11/8/2025 |
| 1.0.38 | 215 | 11/8/2025 |
| 1.0.37 | 204 | 11/8/2025 |
| 1.0.36 | 253 | 10/12/2025 |