/SimpleNatsClient

simple c# client for nats.io

Primary LanguageC#MIT LicenseMIT

SimpleNatsClient

A Rx.NET client for the NATS messaging system.

CircleCI NuGet


Sample Usage

The client is all asynchronous and reactive.

Publish

async Task Publish(string subject, byte[] payload)
{
    using (var connection = await NatsClient.Connect())
    {
        await connection.Publish(subject, payload);
    }
}

Subscribe

async Task Subscribe(string subject, Action<IncomingMessage> handleMessage)
{
    using (var connection = await NatsClient.Connect())
    {
        var subscription = connection.GetSubscription(subject);
        await subscription.FirstAsync().Do(handleMessage);

    }
}

Request

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");
    }
}