[][src]Crate rants

An async NATS client library for the Rust programming language.

Note: Currently, rants requires a nightly version of the rust compiler due to its use of async/await syntax, but it should work on stable soon!

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.

Example

use futures::stream::StreamExt;
use rants::Client;
use tokio::runtime::Runtime;

let main_future = async {
   // 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;
};

let runtime = Runtime::new().expect("to create Runtime");
runtime.spawn(main_future);
runtime.shutdown_on_idle();

Modules

error

nats Error and Result

Structs

Address

An address used to connect to a NATS server

Client

The entry point to the NATS client protocol

ClientRef

A type for returning a reference to data behind Client's internal mutex

ClientRefMut

A type for returning a mutable reference to data behind Client's internal mutex

Connect

The CONNECT message sent by the client

Info

The INFO message sent by the server

MpscReceiver

Receive values from the associated Sender.

Msg

The MSG message sent by the server

Subject

A subject to publish or subscribe to

Subscription

A subscription to receive messages from a particular Subject

WatchReceiver

Receives values from the associated Sender.

Enums

Authorization

The methods of client authorization set in the Connect message

ClientState

Client states

ProtocolError

The -ERR messages sent from the server

Functions

generate_delay_generator

Generate a Client's delay_generator_mut

Type Definitions

DelayGenerator

The type of a Client's delay_generator_mut

Sid

The type used for subscription IDs