Skip to content

Commit

Permalink
Subscribe docs updates
Browse files Browse the repository at this point in the history
  • Loading branch information
mtmk committed Nov 7, 2023
1 parent 7c12697 commit 4ac9672
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 21 deletions.
25 changes: 22 additions & 3 deletions docs/documentation/core/intro.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,11 @@ Subscribe to all `bar` [related subjects](https://docs.nats.io/nats-concepts/sub
```csharp
await using var nats = new NatsConnection();

await using sub = await nats.SubscribeAsync<Bar>("bar.>");
await foreach (var msg in sub.Msgs.ReadAllAsync())
await foreach (var msg in nats.Subscription("bar.>"))
{
if (msg.Subject == "bar.exit")
break;

Console.WriteLine($"Received {msg.Subject}: {msg.Data}\n");
}
```
Expand All @@ -49,6 +51,8 @@ for (int i = 0; i < 10; i++)
Console.WriteLine($" Publishing {i}...");
await nats.PublishAsync<Bar>($"bar.baz.{i}", new Bar { Id = i, Name = "Baz" });
}

await nats.PublishAsync("bar.exit");
```

## Logging
Expand All @@ -57,7 +61,22 @@ You should also hook your logger to `NatsConnection` to make sure all is working
to get help diagnosing any issues you might have:

```csharp
var opts = NatsOpts.Default with { LoggerFactory = new MinimumConsoleLoggerFactory(LogLevel.Error) };
// First add Nuget package Microsoft.Extensions.Logging.Console
using Microsoft.Extensions.Logging;

using var loggerFactory = LoggerFactory.Create(configure: builder =>
{
builder
.SetMinimumLevel(LogLevel.Information)
.AddSimpleConsole(options =>
{
options.SingleLine = true;
options.TimestampFormat = "yyyy-MM-dd HH:mm:ss.fff zzz ";
});
});

var opts = NatsOpts.Default with { LoggerFactory = loggerFactory };

await using var nats = new NatsConnection(otps);
```

Expand Down
19 changes: 14 additions & 5 deletions docs/documentation/core/pub-sub.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,25 @@ receives the message.
```csharp
await using var nats = new NatsConnection();

await using sub = await nats.SubscribeAsync<int>("foo");
var sub = Task.Run(async () =>
{
await foreach(var msg in nats.SubscribeAsync<int>("foo"))
{
Console.WriteLine($"Received {msg.Subject}: {msg.Data}\n");

if (msg.Data == -1)
break;
}
});

for (int i = 0; i < 10; i++)
{
Console.WriteLine($" Publishing {i}...");
await nats.PublishAsync<int>("foo", i);
await Task.Delay(1000);
}

await foreach (var msg in sub.Msgs.ReadAllAsync())
{
Console.WriteLine($"Received {msg.Subject}: {msg.Data}\n");
}
await nats.PublishAsync<int>("foo", -1);

await sub;
```
12 changes: 4 additions & 8 deletions docs/documentation/core/queue.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,20 +17,17 @@ await using var nats = new NatsConnection();

var subs = new List<NatsSubBase>();
var replyTasks = new List<Task>();
var cts = new CancellationTokenSource();

for (int i = 0; i < 3; i++)
{
// Create three subscriptions all on the same queue group
var sub = await nats.SubscribeAsync<int>("math.double", queueGroup: "maths-service");

subs.Add(sub);

// Create a background message loop for every subscription
var replyTaskId = i;
replyTasks.Add(Task.Run(async () =>
{
// Retrieve messages until unsubscribed
await foreach (var msg in sub.Msgs.ReadAllAsync())
await foreach (var msg in nats.SubscribeAsync<int>("math.double", queueGroup: "maths-service", cancellationToken: cts.Token))
{
Console.WriteLine($"[{replyTaskId}] Received request: {msg.Data}");
await msg.ReplyAsync($"Answer is: {2 * msg.Data}");
Expand All @@ -49,9 +46,8 @@ for (int i = 0; i < 10; i++)

Console.WriteLine("Stopping...");

// Unsubscribing or disposing will complete the message loops
foreach (var sub in subs)
await sub.UnsubscribeAsync();
// Cancellation token will unsubcribe and complete the message loops
cts.Cancel();

// Make sure all tasks finished cleanly
await Task.WhenAll(replyTasks);
Expand Down
4 changes: 1 addition & 3 deletions docs/documentation/core/req-rep.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,7 @@ Create a service that will be responding to requests:
```csharp
await using var nats = new NatsConnection();

await using var sub = await conn.SubscribeAsync<int>("math.double");

await foreach (var msg in sub.Msgs.ReadAllAsync())
await foreach (var msg in conn.SubscribeAsync<int>("math.double"))
{
Console.WriteLine($"Received request: {msg.Data}");

Expand Down
4 changes: 2 additions & 2 deletions docs/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ The NATS.NET V2 client is in preview and not recommended for production use yet.
- [x] Object Store initial support
- [x] Service API initial support
- [x] .NET 8.0 support (Native AOT)
- [ ] Implementation of missing major features (e.g. JetStream ordered consumers)
- [ ] Beta phase
- [x] Implementation of missing major features (e.g. JetStream ordered consumers)
- [x] Beta phase
- [ ] Testing and bug fixing
- [ ] General Availability

Expand Down

0 comments on commit 4ac9672

Please sign in to comment.