-
Notifications
You must be signed in to change notification settings - Fork 39
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
134 additions
and
38 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
// Install NuGet packages `NATS.Net` and `Microsoft.Extensions.Logging.Console`. | ||
using NATS.Client.Core; | ||
using NATS.Client.JetStream; | ||
using NATS.Client.JetStream.Models; | ||
|
||
// `NATS_URL` environment variable can be used to pass the locations of the NATS servers. | ||
var url = Environment.GetEnvironmentVariable("NATS_URL") ?? "127.0.0.1:4222"; | ||
|
||
// Connect to NATS server. Since connection is disposable at the end of our scope we should flush | ||
// our buffers and close connection cleanly. | ||
var opts = new NatsOpts | ||
{ | ||
Url = url, | ||
Name = "NATS-by-Example", | ||
}; | ||
await using var nats = new NatsConnection(opts); | ||
var js = new NatsJSContext(nats); | ||
|
||
// ### Stream Setup | ||
var stream = "list-subjects"; | ||
|
||
// Remove the stream first!, so we have a clean starting point. | ||
try | ||
{ | ||
await js.DeleteStreamAsync(stream); | ||
} | ||
catch (NatsJSApiException e) when (e is { Error.Code: 404 }) | ||
{ | ||
} | ||
|
||
// Create the stream with a variety of subjects | ||
var streamConfig = new StreamConfig(stream, ["plain", "greater.>", "star.*"]) | ||
{ | ||
Storage = StreamConfigStorage.Memory, | ||
}; | ||
await js.CreateStreamAsync(streamConfig); | ||
|
||
// ### GetStreamAsync with StreamInfoRequest | ||
// Get the subjects via the GetStreamAsync call. | ||
// Since this is "state" there are no subjects in the state unless | ||
// there are messages in the subject. | ||
// To get the subjects map, you must provide a SubjectsFilter | ||
// Use the > to filter for all subjects | ||
var jsStream = await js.GetStreamAsync(stream, new StreamInfoRequest() { SubjectsFilter = ">" }); | ||
Console.WriteLine($"Before publishing any messages, there are 0 subjects: {jsStream.Info.State.Subjects?.Count}"); | ||
|
||
// Publish a message | ||
await js.PublishAsync("plain", "plain-data"); | ||
|
||
jsStream = await js.GetStreamAsync(stream, new StreamInfoRequest() { SubjectsFilter = ">" }); | ||
Console.WriteLine("After publishing a message to a subject, it appears in state:"); | ||
if (jsStream.Info.State.Subjects != null) | ||
{ | ||
foreach (var (subject, count) in jsStream.Info.State.Subjects) | ||
{ | ||
Console.WriteLine($" Subject '{subject}', Count {count}"); | ||
} | ||
} | ||
|
||
// Publish some more messages, this time against wildcard subjects | ||
await js.PublishAsync("greater.A", "gtA"); | ||
await js.PublishAsync("greater.A.B", "gtAB"); | ||
await js.PublishAsync("greater.A.B.C", "gtABC"); | ||
await js.PublishAsync("greater.B.B.B", "gtBBB"); | ||
await js.PublishAsync("star.1", "star1"); | ||
await js.PublishAsync("star.2", "star2"); | ||
|
||
jsStream = await js.GetStreamAsync(stream, new StreamInfoRequest() { SubjectsFilter = ">" }); | ||
Console.WriteLine("Wildcard subjects show the actual subject, not the template:"); | ||
if (jsStream.Info.State.Subjects != null) | ||
{ | ||
foreach (var (subject, count) in jsStream.Info.State.Subjects) | ||
{ | ||
Console.WriteLine($" Subject '{subject}', Count {count}"); | ||
} | ||
} | ||
|
||
// ### Specific Subject Filtering | ||
// You can filter for a more specific subject | ||
jsStream = await js.GetStreamAsync(stream, new StreamInfoRequest() { SubjectsFilter = "greater.>" }); | ||
Console.WriteLine("Filtering the subject returns only matching entries ['greater.>']"); | ||
if (jsStream.Info.State.Subjects != null) | ||
{ | ||
foreach (var (subject, count) in jsStream.Info.State.Subjects) | ||
{ | ||
Console.WriteLine($" Subject '{subject}', Count {count}"); | ||
} | ||
} | ||
|
||
jsStream = await js.GetStreamAsync(stream, new StreamInfoRequest() { SubjectsFilter = "greater.A.>" }); | ||
Console.WriteLine("Filtering the subject returns only matching entries ['greater.A.>']"); | ||
if (jsStream.Info.State.Subjects != null) | ||
{ | ||
foreach (var (subject, count) in jsStream.Info.State.Subjects) | ||
{ | ||
Console.WriteLine($" Subject '{subject}', Count {count}"); | ||
} | ||
} |
15 changes: 15 additions & 0 deletions
15
examples/jetstream/list-subjects/dotnet2/list-subjects.csproj
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<OutputType>Exe</OutputType> | ||
<TargetFramework>net8.0</TargetFramework> | ||
<ImplicitUsings>enable</ImplicitUsings> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<PackageReference Include="NATS.Net" Version="2.3.1"/> | ||
<PackageReference Include="NATS.Client.Serializers.Json" Version="2.0.0"/> | ||
<PackageReference Include="Microsoft.Extensions.Logging.Console" Version="8.0.0" /> | ||
</ItemGroup> | ||
|
||
</Project> |
8e3cec1
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Deploy preview for nats-by-example ready!
✅ Preview
https://nats-by-example-qnqplp8hd-connecteverything.vercel.app
Built with commit 8e3cec1.
This pull request is being automatically deployed with vercel-action