Skip to content

Commit

Permalink
Update README.md (#683)
Browse files Browse the repository at this point in the history
  • Loading branch information
mtmk authored Dec 4, 2024
1 parent 5859101 commit 0d0f030
Showing 1 changed file with 13 additions and 5 deletions.
18 changes: 13 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,28 +38,36 @@ Install the [NATS.Net](https://www.nuget.org/packages/NATS.Net) package from NuG
dotnet add package NATS.Net
```

Run a local [`nats-server`](https://nats-io.github.io/nats.net/documentation/intro.html?tabs=core-nats#quick-start) to use or connect to the demo server if you're not behind a firewall:

```csharp
await using var client = new NatsClient("demo.nats.io");
```

Basic messaging:

```csharp
// NATS core M:N messaging example
await using var nc = new NatsClient();
await using var client = new NatsClient();

// Subscribe on one terminal
await foreach (var msg in nc.SubscribeAsync<string>(subject: "foo"))
await foreach (var msg in client.SubscribeAsync<string>(subject: "foo"))
{
Console.WriteLine($"Received: {msg.Data}");
}

// Start publishing to the same subject on a second terminal
await nc.PublishAsync(subject: "foo", data: "Hello, World!");
await client.PublishAsync(subject: "foo", data: "Hello, World!");
```

Persistence with JetStream:

For this you need to [run the server with JetStream](https://nats-io.github.io/nats.net/documentation/jetstream/intro.html#jetstream-quick-start) enabled if you're using a local server.

```csharp
// NATS JetStream basic publish-consume example
await using var nc = new NatsClient();
var js = nc.CreateJetStreamContext();
await using var client = new NatsClient();
var js = client.CreateJetStreamContext();

// Create a stream to store the messages
await js.CreateStreamAsync(new StreamConfig(name: "ORDERS", subjects: new[] { "orders.*" }));
Expand Down

0 comments on commit 0d0f030

Please sign in to comment.