MyNatsClient.Encodings.Json
0.13.0
JSON Encoding for assisting with sending and consuming JSON messages using MyNatsClient which provides a simple, effective sync and async library for interacting with NATS Server using .NET and .NET Core. It uses IObservable so it is ReactiveX (RX) friendly.
See the version list below for details.
Install-Package MyNatsClient.Encodings.Json -Version 0.13.0
dotnet add package MyNatsClient.Encodings.Json --version 0.13.0
<PackageReference Include="MyNatsClient.Encodings.Json" Version="0.13.0" />
paket add MyNatsClient.Encodings.Json --version 0.13.0
#r "nuget: MyNatsClient.Encodings.Json, 0.13.0"
// Install MyNatsClient.Encodings.Json as a Cake Addin
#addin nuget:?package=MyNatsClient.Encodings.Json&version=0.13.0
// Install MyNatsClient.Encodings.Json as a Cake Tool
#tool nuget:?package=MyNatsClient.Encodings.Json&version=0.13.0
Pub-Sub sample
Simple pub-sub sample showing one client that publishes and one that subscribes. This can of course be the same client and you can also have more clients subscribing etc.
Publisher
var cnInfo = new ConnectionInfo("192.168.1.10");
var client = new NatsClient(cnInfo);
await client.PubAsync("tick", GetNextTick());
//or using an encoding package e.g. Json
await client.PubAsJsonAsync("tickItem", new Tick { Value = GetNextTick() });
Subscriber
var cnInfo = new ConnectionInfo("192.168.1.10");
var client = new NatsClient(cnInfo);
await client.SubAsync("tick", stream => stream.Subscribe(msg => {
Console.WriteLine($"Clock ticked. Tick is {msg.GetPayloadAsString()}");
}));
//or using an encoding package e.g Json
await client.SubAsync("tickItem", stream => stream.Subscribe(msg => {
Console.WriteLine($"Clock ticked. Tick is {msg.FromJson<TestItem>().Value}");
}))
Stream.Subscribe vs Stream.SubscribeSafe
If you subscribe to e.g. the MessageOpStream
using Stream.Subscribe
and your handler is throwing an exception. That handler will get OnError
invoked and then removed.
await client.SubAsync("mySubject", stream => stream.Subscribe(msg => DoSomething(msg)));
If you instead subscribe using Stream.SubscribeSafe
any unhandled exception will get swallowed.
await client.SubAsync("mySubject", stream => stream.SubscribeSafe(msg => DoSomething(msg)));
Request-Response sample
Simple request-response sample. This sample also makes use of two clients. It can of course be the same client requesting and responding, you can also have more responders forming a queue group. Where one will be giving the answer.
Requester
var cnInfo = new ConnectionInfo("192.168.1.10");
var client = new NatsClient(cnInfo);
var response = await client.RequestAsync("getTemp", "stockholm@sweden");
Console.WriteLine($"Temp in Stockholm is {response.GetPayloadAsString()}");
Responder
var cnInfo = new ConnectionInfo("192.168.1.10");
var client = new NatsClient(cnInfo);
await client.SubAsync("getTemp", stream.Subscribe(msg => {
client.Pub(msg.ReplyTo, getTemp(msg.GetPayloadAsString()));
}));
Pub-Sub sample
Simple pub-sub sample showing one client that publishes and one that subscribes. This can of course be the same client and you can also have more clients subscribing etc.
Publisher
var cnInfo = new ConnectionInfo("192.168.1.10");
var client = new NatsClient(cnInfo);
await client.PubAsync("tick", GetNextTick());
//or using an encoding package e.g. Json
await client.PubAsJsonAsync("tickItem", new Tick { Value = GetNextTick() });
Subscriber
var cnInfo = new ConnectionInfo("192.168.1.10");
var client = new NatsClient(cnInfo);
await client.SubAsync("tick", stream => stream.Subscribe(msg => {
Console.WriteLine($"Clock ticked. Tick is {msg.GetPayloadAsString()}");
}));
//or using an encoding package e.g Json
await client.SubAsync("tickItem", stream => stream.Subscribe(msg => {
Console.WriteLine($"Clock ticked. Tick is {msg.FromJson<TestItem>().Value}");
}))
Stream.Subscribe vs Stream.SubscribeSafe
If you subscribe to e.g. the MessageOpStream
using Stream.Subscribe
and your handler is throwing an exception. That handler will get OnError
invoked and then removed.
await client.SubAsync("mySubject", stream => stream.Subscribe(msg => DoSomething(msg)));
If you instead subscribe using Stream.SubscribeSafe
any unhandled exception will get swallowed.
await client.SubAsync("mySubject", stream => stream.SubscribeSafe(msg => DoSomething(msg)));
Request-Response sample
Simple request-response sample. This sample also makes use of two clients. It can of course be the same client requesting and responding, you can also have more responders forming a queue group. Where one will be giving the answer.
Requester
var cnInfo = new ConnectionInfo("192.168.1.10");
var client = new NatsClient(cnInfo);
var response = await client.RequestAsync("getTemp", "stockholm@sweden");
Console.WriteLine($"Temp in Stockholm is {response.GetPayloadAsString()}");
Responder
var cnInfo = new ConnectionInfo("192.168.1.10");
var client = new NatsClient(cnInfo);
await client.SubAsync("getTemp", stream.Subscribe(msg => {
client.Pub(msg.ReplyTo, getTemp(msg.GetPayloadAsString()));
}));
Release Notes
https://github.com/danielwertheim/mynatsclient/blob/master/ReleaseNotes.md
Dependencies
-
.NETFramework 4.5.1
- Ensure.That (>= 8.0.0)
- MyNatsClient (>= 0.13.0)
- Newtonsoft.Json (>= 11.0.2)
-
.NETStandard 2.0
- Ensure.That (>= 8.0.0)
- MyNatsClient (>= 0.13.0)
- Newtonsoft.Json (>= 11.0.2)
Used By
NuGet packages (3)
Showing the top 3 NuGet packages that depend on MyNatsClient.Encodings.Json:
Package | Downloads |
---|---|
Serilog.Sinks.Nats
Serilog sink for NATS messaging server
|
|
Mi.Fx.Nats
macula.io Nats abstraction
|
|
Mi.Fx.Contract
Package Description
|
GitHub repositories
This package is not used by any popular GitHub repositories.
Version History
Version | Downloads | Last updated |
---|---|---|
0.18.2 | 534 | 3/18/2021 |
0.18.1 | 172 | 3/11/2021 |
0.18.0 | 58 | 3/10/2021 |
0.17.1 | 55 | 3/9/2021 |
0.17.0 | 1,479 | 10/16/2020 |
0.16.1 | 739 | 9/19/2020 |
0.16.0 | 199 | 9/19/2020 |
0.15.0 | 231 | 9/19/2020 |
0.14.3 | 855 | 8/25/2020 |
0.14.2 | 193 | 8/16/2020 |
0.14.1 | 8,970 | 1/6/2020 |
0.13.0 | 30,321 | 6/29/2018 |
0.12.0 | 444 | 6/29/2018 |
0.12.0-pre1 | 600 | 5/4/2018 |
0.11.0 | 8,495 | 1/26/2017 |
0.10.0 | 598 | 1/10/2017 |