11 unstable releases

0.6.0 Nov 8, 2021
0.5.0 Sep 10, 2020
0.4.4 Aug 14, 2020
0.4.3 Apr 13, 2020
0.3.1 Oct 7, 2019

#771 in Asynchronous

Download history 98/week @ 2024-07-22 89/week @ 2024-07-29 88/week @ 2024-08-05 35/week @ 2024-08-12 56/week @ 2024-08-19 82/week @ 2024-08-26 48/week @ 2024-09-02 64/week @ 2024-09-09 53/week @ 2024-09-16 130/week @ 2024-09-23 96/week @ 2024-09-30 280/week @ 2024-10-07 114/week @ 2024-10-14 48/week @ 2024-10-21 106/week @ 2024-10-28 99/week @ 2024-11-04

375 downloads per month

MIT/Apache

150KB
3K SLoC

Rants

build status crates.io docs License License

An async NATS client library for the Rust programming language.

The client aims to be an ergonomic, yet thin, wrapper over the NATS client protocol. The easiest way to learn to use the client is by reading the NATS client protocol documentation. The main entry point into the library's API is the Client struct.

TLS support can be powered by the native-tls crate enabled with the native-tls feature, or TLS support can be powered by the rustls crate enabled with the rustls-tls feature.

Example

use futures::stream::StreamExt;
use rants::Client;

#[tokio::main]
async fn main() {
   // A NATS server must be running on `127.0.0.1:4222`
   let address = "127.0.0.1:4222".parse().unwrap();
   let client = Client::new(vec![address]);

   // Configure the client to receive messages even if it sent the message
   client.connect_mut().await.echo(true);

   // Connect to the server
   client.connect().await;

   // Create a new subject called "test"
   let subject = "test".parse().unwrap();

   // Subscribe to the "test" subject
   let (_, mut subscription) = client.subscribe(&subject, 1024).await.unwrap();

   // Publish a message to the "test" subject
   client
       .publish(&subject, b"This is a message!")
       .await
       .unwrap();

   // Read a message from the subscription
   let message = subscription.next().await.unwrap();
   let message = String::from_utf8(message.into_payload()).unwrap();
   println!("Received '{}'", message);

   // Disconnect from the server
   client.disconnect().await;
}

Development

The integration test suite requires the NATS_PATH environment variable point to the NATS server executable:

> cargo test

The env_logger crate is used in integration tests. To enable it and run a single test run:

> RUST_LOG=rants=trace cargo test ping_pong

Alternatives

Dependencies

~6–19MB
~290K SLoC