#nats #client #api-client #stan #async #api-bindings

natsclient

NATS client designed for both an ergonomic API and performance

5 releases

0.0.7 Mar 18, 2020
0.0.6 Dec 16, 2019
0.0.5 Jul 18, 2019
0.0.4 May 22, 2019
0.0.3 Apr 28, 2019

#23 in #nats

Apache-2.0

36KB
788 lines

travis  license

NATS Client

A simple, developer-friendly NATS client designed with an ergonomic API designed to allow you to use this client anywhere, whether you're using tokio or single-threaded apps or traditional multi-threaded.

Usage

The following sample illustrates basic publish and subscribe features:

let jwt = "...";
let seed = "...";
    
let opts = ClientOptions::builder()
    .cluster_uris(vec!["nats://localhost:4222".into()])
    .authentication(AuthenticationStyle::UserCredentials(
        jwt.to_string(),
        seed.to_string(),
    ))
    .build()?;

let client = Client::from_options(opts)?;
client.connect()?;

client.subscribe("ticker", move |msg| {
    let symbol: SymbolReply = serde_json::from_slice(&msg.payload).unwrap();
    info!("Received stock ticker: {:?}", symbol);
    Ok(())
})?;

To publish a message:

c.publish(&r, payload_bytes, None)?;

And to utilize the request/response pattern:

 let reply = client.request(
    "symbolquery",
    r#"{"symbol": "NATS"}"#.as_bytes(),
    Duration::from_millis(100),
)?;

let symbol: SymbolReply = serde_json::from_slice(&reply.payload).unwrap();
info!("Stock symbol response: {:?}", symbol);

Features

The following is a list of features currently supported and planned by this client:

  • - Request/Reply
  • - Subscribe
  • - Publish
  • - All authentication models, including NATS 2.0 JWT and seed keys
  • - Adherance to protocol v1, accepts new server information whenever it's sent from NATS
  • - Automatic reconnect upon connection failure
  • - TLS support
  • - NATS Streaming (STAN)

Dependencies

~13MB
~231K SLoC