A Rx.NET client for the NATS messaging system.
The client is all asynchronous and reactive.
async Task Publish(string subject, byte[] payload)
{
using (var connection = await NatsClient.Connect())
{
await connection.Publish(subject, payload);
}
}
async Task Subscribe(string subject, Action<IncomingMessage> handleMessage)
{
using (var connection = await NatsClient.Connect())
{
var subscription = connection.GetSubscription(subject);
await subscription.FirstAsync().Do(handleMessage);
}
}
async Task Request(string subject, Action<IncomingMessage> handleAnswer)
{
using (var connection = await NatsClient.Connect())
{
var answer = await connection.Request(subject);
handleAnswer(answer);
}
}
Reply to request
async Task Reply(string subject)
{
using (var connection = await NatsClient.Connect())
{
var subscription = connection.GetSubscription(subject);
var request = await subscription.FirstAsync();
await connection.Publish(request.ReplyTo, "answer");
}
}