11个不稳定版本

0.6.0 2021年11月8日
0.5.0 2020年9月10日
0.4.4 2020年8月14日
0.4.3 2020年4月13日
0.3.1 2019年10月7日

#943 in 异步

Download history 79/week @ 2024-04-21 84/week @ 2024-04-28 28/week @ 2024-05-05 84/week @ 2024-05-12 116/week @ 2024-05-19 176/week @ 2024-05-26 130/week @ 2024-06-02 146/week @ 2024-06-09 163/week @ 2024-06-16 125/week @ 2024-06-23 61/week @ 2024-06-30 215/week @ 2024-07-07 123/week @ 2024-07-14 91/week @ 2024-07-21 95/week @ 2024-07-28 85/week @ 2024-08-04

404每月下载次数

MIT/Apache

150KB
3K SLoC

Rants

build status crates.io docs License License

一个为Rust编程语言设计的异步NATS客户端库。

客户端旨在成为NATS客户端协议的一个易于使用且轻薄的包装器。学习使用客户端的最简单方法是阅读NATS客户端协议文档。库API的主要入口点是Client结构体。

TLS支持可以通过启用native-tls功能的native-tls crate或通过启用rustls-tls功能的rustls crate来实现。

示例

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

开发

集成测试套件需要NATS_PATH环境变量指向NATS服务器可执行文件

> cargo test

在集成测试中使用env_logger crate。要启用它并运行单个测试,请

> RUST_LOG=rants=trace cargo test ping_pong

替代方案

依赖项

~6–20MB
~281K SLoC