80dB / AsyncNats

An async Nats.io client written using C# 8.0 language options
MIT License
6 stars 2 forks source link

Archived

Due to the fact that 80dB (the company) went bankrupt, this repository will no longer be maintained and the component will no longer receive updates.

AsyncNats

A Nats.IO client specifically written with new C# features in mind. Internally it uses the new System.IO.Pipelines and System.Threading.Channels libraries that were released last year. It also uses the new IAsyncEnumerable as a way to listen to messages published to subjects.

The end result is very fast Nats.io client that, in our opinion, fits the C# 8.0 language features better than the currently existing libraries.

Known issues

There are currently no known issues. But the library has not been rigorously tested in production environments yet.

Known limitations

Usage

You can publish messages using any of the following methods:

PublishObjectAsync // This method serializes the object with the supplied/default serializer
PublishAsync // This method publishes a raw byte array or a string as UTF8

You can subscribe to subjects with the following methods:

Subscribe // This method returns messages send to the specified subject but does not perform any deserialization, *the payload is only valid until the next message has been enumerated*
Subscribe<T> // This method returns deserialized messages send to the specified subject
SubscribeObject<T> // This method is similar to Subscribe<T> but does not wrap the enumerated objects in a NatsTypedMsg, use this if you do not care about subject/subscriptionId/replyTo
SubscribeText // This method is similar to Subscribe<T> except that it only UTF8 decodes the payload

The returned subscriptions are AsyncEnumerables and can be enumerated using the new await foreach:

await foreach(var message in connection.SubscribeText("HELLO"))
{
    // Process message here
}

There's also the option to perform requests using the following methods:

Request // This method sends and receives a raw byte[], Memory<byte> or string
RequestObject // This method sends and receives a serialized/deserialized object 

The request methods require a process to listen to the subjects. The replyTo-subject is automatically generated using the Environment.TickCount when the connection options where created and an internal counter. In larger setups where multiple processes are starting at the same time this might not be unique enough. You can change this prefix by changing it in the options when creating a NatsConnection.

RPC Usage

You can let AsyncNats handle RPC calls for you (instead of using Request + Subscribe) by using these two methods:

StartContractServer<TContract>
GenerateContractClient<TContract>

The contract has to be an interface and only supports methods (both sync/async). The InterfaceAsyncNatsSample gives a good idea on how to use them.

It's possible to have multiple contract servers running with a different base subject. This feature is still in experimental phase.

Release history

v1.0.5

v1.0.4

v1.0.3

v1.0.2

v1.0.1

v1.0.0

v0.8.5

v0.8.4

v0.8.3

v0.8.2

v0.8.1

v0.8.0

v0.7.1

v0.7.0

v0.6.5

v0.6.4

v0.6.3

v0.6.2

v0.6.1

v0.6

v0.5.2

v0.5.1

v0.5

v0.4

v0.3

v0.2

v0.1