Ratsio
Ratsio is a Rust client library for NATS messaging system and NATS Event Streaming.
Inspired by nitox and rust-nats but my project needed NATS streaming, so I couldn't use any of those 2. If this project is useful to you, feel free to contribute or suggest features. at the moment it's just the features I need.
Add the following to your Cargo.toml.
= "^0.2"
ratsio
Rust -stable, -beta and -nightly are supported.
Features:
- Nats messaging queue. Publish, Subcribe and Request.
- Nats cluster support, auto reconnect and dynamic cluster hosts update.
- Async from the ground up, using tokio and futures.
- TLS mode
- NATS 1.x Authentication
- NATS 2.0 JWT-based client authentication
- NATS Streaming Server
Usage
Subscribing and Publishing to a NATS subject: see tests/nats_client_test.rs
let mut runtime = new.unwrap;
let options = builder
.cluster_uris
.build
.unwrap;
let fut = from_options
.and_then
.and_then;
let = channel;
runtime.spawn;
let connection_result = rx.wait.expect;
let _ = runtime.shutdown_now.wait;
info!;
assert!;
let msg = connection_result.unwrap;
assert_eq!;
Subscribing and Publishing to a NATS streaming subject: see tests/stan_client_test.rs
let mut runtime = new.unwrap;
let nats_options = builder
.cluster_uris
.build
.unwrap;
let stan_options = builder
.nats_options
.cluster_id
.client_id.build
.unwrap;
let = unbounded;
let = channel;
let subject: String = "test.subject".into;
let subject1 = subject.clone;
let subject2 = subject.clone;
let program = from_options
.and_then
.and_then
.map_err
.and_then.map_err;
runtime.spawn;
let stan_client = stan_client_rx.wait.expect;
match result_rx.wait.next.expect ;
let = channel;
runtime.spawn;
let _ = close_rx.wait.expect;
let _ = runtime.shutdown_now.wait;
Important Changes
Version 0.2
Users no longer need to use ratsio::ops::Connect struct when configuring a connection. Options are now availabble on NatsClientOptions, username, password, tls_required, auth_token, etc
For example
let nats_options = builder
.username
.password
.cluster_uris
.build
.unwrap;
Internal nuid fork from nuid upgraded to use rand version ^0.6
Version 0.2.1
More ergonomics when creating options. A bit easier on the eye.
It is now possible to pass either of String, &str, Vec or Vec<&str> to cluster_uris(...) on NatsClientOptions::builder(), use
use ratsio::prelude::VecUri;
or just use ratsio::prelude::*;
let nats_options = builder
.username
.password
.cluster_uris
.build
.unwrap;
or
let nats_options = builder
.username
.password
.cluster_uris // For 1 url
.build
.unwrap;
Version 0.2.2
Bug fixes:
- Send connect op when reconnecting, thanks to David McNeil
Version 0.3.0
Merged the from_options
and connect
methods because from_options
was actually making a TCP connection, and so its
name was misleading. Further, the new client now properly awaits the INFO
pre-amble from the server before supplying the
CONNECT
message.
Added support for NATS 2.0 client authentication via JWTs and nkeys. You can now pass a
UserJWT
option along with a callback used to sign a nonce
(a small random string the server uses to verify that the client
does possess the private key), as shown in the following example:
let raw_jwt = String from;
let opt_jwt = new;
let mut runtime = new.unwrap;
let options = builder
.cluster_uris
.user_jwt
.build
.unwrap;
let client = connect;
And here's what a sample sign_nonce
function looks like:
At the moment, you have to define your own callback to ensure that your code can manage the lifetime of the seed key in a
way that is hopefully short-lived. If the NATS client library managed your seed key lifetime, it would have to enforce a
'static
guarantee, which isn't the most secure approach.
Contact
For bug reports, patches, feature requests or other messages, please send a mail to michael@zulzi.com
License
This project is licensed under the MIT License.