notes from a passionate developer





This is a personal blog. The opinions expressed here represent my own and not those of my employer, nor current or previous. All content is published "as is", without warranty of any kind and I don't take any responsibility and can't be liable for any claims, damages or other liabilities that might be caused by the content.

New release of my NATS client focusing on simplifying usage

Just reached v0.7.0 of MyNatsClient and focus has been on simplifying the usage of it. This means that the client now keeps track of subscriptions and auto-subscribes when the client gets connected or re-connected. It now also allows you to setup a subscription both against the NATS server and the internal observable stream of messages in one call. And the final bigger change is that it now supports simplifying methods for performing requests. Lets have a look at some short code samples.

Simplest pub-sub example

Before this release, you had to subscribe both to the in-process client.MsgOpStream as well as sending a sub command to the NATS server via client.Sub(...) or client.SubAsync(...). You still can. But you don't have to anymore. You can now do that in one call instead:

var connectionInfo = new ConnectionInfo("")
    AutoReconnectOnFailure = true

using(var client = new NatsClient("clientId1", connectionInfo))
    await client.SubWithHandlerAsync("clock.ticked", msg => {
        Dump($"Clock ticked. Tick is {msg.GetPayloadAsString()}");

    await client.PubAsync("clock.ticked", getNextTick());

Use an observer to get exceptions etc

You can also make use of client.SubWithObserver(...) instead. This lets you hook in a callback for Action<Exception>.

using(var client = new NatsClient("clientId1", connectionInfo))
    var observer = new DelegatingObserver<MsgOp>(
        msg => {
            //On message
        ex => {
            //On ex

    await client.SubWithObserverAsync("clock.ticked", observer);

    await client.PubAsync("clock.ticked", getNextTick());


using(var client = new NatsClient("clientId1", connectionInfo))
    //The response producer
    await client.SubWithHandlerAsync("getTemp", msg => {
        client.Pub(msg.ReplyTo, getTemp(msg.GetPayloadAsString()));

    //The requesting party
    var response = await client.RequestAsync(

    Dump($"Temp in Stockholm is {response.GetPayloadAsString()}");

Subscribing & Unsubscribing

The Client will keep track of subscriptions. And you can set them up before connecting. Once it gets connected, it will register the subscriptions against the NATS server. If you make use of ConnectionInfo.AutoReconnectOnFailure it will also re-subscribe in the event of exceptions.

When subscribing to a subject using the client, you will be returned an ISubscription. The methods for subscribing are:

To Unsubscribe, you can do any of the following:

NOTE it's perfectly fine to do both e.g. subscription.Dispose as well as consumer.Dispose or e.g. consumer.Unsubscribe and then subscription.Dispose.

That's it for now. One step close to a stable release.

Merry Christmas.


View Comments