Coder Social home page Coder Social logo

rants's Introduction

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

rants's People

Contributors

davidmcneil avatar film42 avatar sphqxe avatar christophermaier avatar taiki-e avatar spebern avatar semtexzv avatar

Watchers

James Cloos avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.