NATS .NET Demo
NATS team maintains a demo server you can reach at demo.nats.io
globally.
You can use this server to quickly write your first NATS .NET application without setting up a server.
Note
If you're behind a firewall, you might not be able to reach the demo server. Check out the introduction page for instructions on how to run your own server easily.
Create two console applications, one for subscribing and one for publishing messages.
The Receiver
mkdir HelloNats.Receiver
cd HelloNats.Receiver
dotnet new console
dotnet add package NATS.Net
await using NatsClient nc = new NatsClient("demo.nats.io");
Console.Write("Enter your room: ");
string? room = Console.ReadLine();
Console.WriteLine($"Listening for messages on 'hello.{room}.>'");
await foreach (NatsMsg<string> msg in nc.SubscribeAsync<string>(subject: $"hello.{room}.>"))
{
Console.WriteLine($"Received: {msg.Subject}: {msg.Data}");
}
dotnet run
The Sender
mkdir HelloNats.Sender
cd HelloNats.Sender
dotnet new console
dotnet add package NATS.Net
await using NatsClient nc = new NatsClient("demo.nats.io");
Console.Write("Enter your room: ");
string? room = Console.ReadLine();
Console.Write("Enter your name: ");
string? name = Console.ReadLine();
while (true)
{
Console.Write("Enter a message to publish: ");
string? message = Console.ReadLine();
await nc.PublishAsync(subject: $"hello.{room}.{name}", data: message);
}
dotnet run
Try running the sender from more than one terminal to have some fun. Happy chatting!
The receiver will listen to messages on the hello.my_room.>
subject and your sender application
will send messages to the matching subjects.
This subject has a wildcard >
at the end, which means it will match
any subject starting with hello.my_room.
.
What's Next
Introduction can help you start creating your application in no time. Follow our quick start guides.
API is the generated reference documentation.