All Projects → jedisct1 → Rust Nats

jedisct1 / Rust Nats

Licence: other
A simple NATS client library for Rust

Programming Languages

rust
11053 projects

Labels

Projects that are alternatives of or similar to Rust Nats

Gokit
Go Examples: From basics to distributed systems
Stars: ✭ 325 (+225%)
Mutual labels:  nats
Hemera
🔬 Writing reliable & fault-tolerant microservices in Node.js https://hemerajs.github.io/hemera/
Stars: ✭ 773 (+673%)
Mutual labels:  nats
Ultimate Backend
Multi tenant SaaS starter kit with cqrs graphql microservice architecture, apollo federation, event source and authentication
Stars: ✭ 978 (+878%)
Mutual labels:  nats
Natsboard
Dashboard for monitoring NATS (an open source messaging system)
Stars: ✭ 339 (+239%)
Mutual labels:  nats
Nats.py
Python3 client for NATS.io
Stars: ✭ 384 (+284%)
Mutual labels:  nats
Nats.rb
Ruby client for NATS, the cloud native messaging system.
Stars: ✭ 850 (+750%)
Mutual labels:  nats
Benthos
Fancy stream processing made operationally mundane
Stars: ✭ 3,705 (+3605%)
Mutual labels:  nats
Nats.ex
Elixir client for NATS, the cloud native messaging system. https://nats.io
Stars: ✭ 97 (-3%)
Mutual labels:  nats
Nats Operator
NATS Operator
Stars: ✭ 471 (+371%)
Mutual labels:  nats
Docker Compose Nats Cluster
A three-node NATS cluster running on top of Docker Compose for testing purposes.
Stars: ✭ 31 (-69%)
Mutual labels:  nats
Vlan Nats
Virtual LAN over NATS
Stars: ✭ 350 (+250%)
Mutual labels:  nats
Nats.net
The official C# Client for NATS
Stars: ✭ 378 (+278%)
Mutual labels:  nats
Pipe
Multi provider event grid written in go
Stars: ✭ 9 (-91%)
Mutual labels:  nats
Nats.java
Java client for NATS
Stars: ✭ 325 (+225%)
Mutual labels:  nats
Arduino Nats
An Arduino / ESP8266 / Particle Photon compatible C++ library for communicating with a NATS (http://nats.io) server
Stars: ✭ 44 (-56%)
Mutual labels:  nats
Nats.go
Golang client for NATS, the cloud native messaging system.
Stars: ✭ 3,690 (+3590%)
Mutual labels:  nats
Teacup nats
Teacup based NATS client for Erlang
Stars: ✭ 16 (-84%)
Mutual labels:  nats
Nats Queue Worker
Queue-worker for OpenFaaS with NATS Streaming
Stars: ✭ 98 (-2%)
Mutual labels:  nats
Gg
监听mysql的binlog,发送消息到nats或rabbitmq
Stars: ✭ 67 (-33%)
Mutual labels:  nats
Nats K8s
Various k8s/nats artifacts in experimental work.
Stars: ✭ 12 (-88%)
Mutual labels:  nats

Rust-NATS

Rust-NATS is a Rust client library for the NATS message queue.

The crate is called nats and can be added as a dependency using Cargo:

[dependencies]
nats = "*"

Rust -stable, -beta and -nightly are supported.

The library was designed to be robust. It doesn't use any usafe code, it never calls panic!() and failed commands are automatically retried on different cluster nodes.

It provides a simple low-level interface that makes it easy to send/receive messages over Rust channels if needed.

Connecting

Single-node client connection:

extern crate nats;
use nats::*;

let client = Client::new("nats://user:[email protected]").unwrap();

The username and password are optional.

Connecting to a cluster:

let cluster = vec!("nats://user:[email protected]", "nats://127.0.0.2");
let mut client = nats::Client::new(cluster).unwrap();

By default, commands are sent in fire-and-forget mode. In order to wait for an acknowledgment after each command, the synchronous ("verbose") mode can be turned on:

client.set_synchronous(true);

The client name can also be customized:

client.set_name("app");

Publishing messages

client.publish("subject.test", "test".as_bytes()).unwrap();

In order to use NATS for RPC, the Client.make_request() function creates an ephemeral subject ("inbox"), subscribes to it, schedules the removal of the subscription after the first received message, publishes the initial request, and returns the inbox subject name:

let inbox = client.make_request("subject.rpc", "test".as_bytes()).unwrap();

Subscribing to subjects

Client.subscribe() adds a subscription to a subject, with an optional group:

let s1 = client.subscribe("subject", None).unwrap();
let s2 = client.subscribe("subject.*", Some("app")).unwrap();

With group membership, a given message will be only delivered to one client in the group.

Client.unsubscribe() removes a subscription:

client.unsubscribe(s1).unwrap();

Or to remove it after n messages have been received:

client.unsubscribe_after(s1, n).unwrap();

Receiving events

Client.wait() waits for a new event, and transparently responds to server PING requests.

let event = client.wait();

This returns an Event structure:

pub struct Event {
    pub subject: String,
    pub channel: Channel,
    pub msg: Vec<u8>,
    pub inbox: Option<String>
}

Alternatively, events can be received using an iterator:

for event in client.events() {
    ...
}

TLS

Build and set TLSConfig before connect:

use std::fs::File;
use std::io::Read;
use self::nats::openssl;  // use re-exported openssl crate

// Load root certificate
let mut file = File::open("./configs/certs/ca.pem").unwrap();
let mut contents = vec![];
file.read_to_end(&mut contents).unwrap();
let cert = openssl::x509::X509::from_pem(&contents).unwrap();

// Load client certificate
let mut file = File::open("./configs/certs/client.crt").unwrap();
let mut contents = vec![];
file.read_to_end(&mut contents).unwrap();
let client_cert = openssl::x509::X509::from_pem(&contents).unwrap();

// Load client key
let mut file = File::open("./configs/certs/client.key").unwrap();
let mut contents = vec![];
file.read_to_end(&mut contents).unwrap();
let client_key = openssl::pkey::PKey::private_key_from_pem(&contents).unwrap();

let mut builder = nats::TlsConfigBuilder::new().unwrap();
// Set root certificate
builder.add_root_certificate(cert).unwrap();
// Set client certificate
builder.add_client_certificate(&client_cert, &client_key).unwrap();
let tls_config = builder.build();

let mut client = nats::Client::new("nats://localhost:4222").unwrap();
client.set_tls_config(tls_config);
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].