Struct async_nats::Client

source ·
pub struct Client { /* private fields */ }
Expand description

Client is a Cloneable handle to NATS connection. Client should not be created directly. Instead, one of two methods can be used: crate::connect and crate::ConnectOptions::connect

Implementations§

Returns last received info from the server.

Examples
let client = async_nats::connect("demo.nats.io").await?;
println!("info: {:?}", client.server_info());

Returns true if the server version is compatible with the version components.

Examples
let client = async_nats::connect("demo.nats.io").await?;
assert!(client.is_server_compatible(2, 8, 4));

Publish a Message to a given subject.

Examples
let client = async_nats::connect("demo.nats.io").await?;
client.publish("events.data".into(), "payload".into()).await?;

Publish a Message with headers to a given subject.

Examples
use std::str::FromStr;
let client = async_nats::connect("demo.nats.io").await?;
let mut headers = async_nats::HeaderMap::new();
headers.insert("X-Header", async_nats::HeaderValue::from_str("Value").unwrap());
client.publish_with_headers("events.data".into(), headers, "payload".into()).await?;

Publish a Message to a given subject, with specified response subject to which the subscriber can respond. This method does not await for the response.

Examples
let client = async_nats::connect("demo.nats.io").await?;
client.publish_with_reply("events.data".into(), "reply_subject".into(), "payload".into()).await?;

Publish a Message to a given subject with headers and specified response subject to which the subscriber can respond. This method does not await for the response.

Examples
use std::str::FromStr;
let client = async_nats::connect("demo.nats.io").await?;
let mut headers = async_nats::HeaderMap::new();
client.publish_with_reply_and_headers("events.data".into(), "reply_subject".into(), headers, "payload".into()).await?;

Sends the request with headers.

Examples

let client = async_nats::connect("demo.nats.io").await?;
let response = client.request("service".into(), "data".into()).await?;

Sends the request with headers.

Examples

let client = async_nats::connect("demo.nats.io").await?;
let mut headers = async_nats::HeaderMap::new();
headers.insert("Key", "Value");
let response = client.request_with_headers("service".into(), headers, "data".into()).await?;

Sends the request created by the Request.

Examples
let client = async_nats::connect("demo.nats.io").await?;
let request = async_nats::Request::new().payload("data".into());
let response = client.send_request("service".into(), request).await?;

Create a new globally unique inbox which can be used for replies.

Examples
let reply = nc.new_inbox();
let rsub = nc.subscribe(reply).await?;

Subscribes to a subject to receive messages.

Examples
use futures::StreamExt;
let client = async_nats::connect("demo.nats.io").await?;
let mut subscription = client.subscribe("events.>".into()).await?;
while let Some(message) = subscription.next().await {
    println!("received message: {:?}", message);
}

Subscribes to a subject with a queue group to receive messages.

Examples
use futures::StreamExt;
let client = async_nats::connect("demo.nats.io").await?;
let mut subscription = client.queue_subscribe("events.>".into(), "queue".into()).await?;
while let Some(message) = subscription.next().await {
    println!("received message: {:?}", message);
}

Flushes the internal buffer ensuring that all messages are sent.

Examples
let client = async_nats::connect("demo.nats.io").await?;
client.flush().await?;

Returns the current state of the connection.

Examples
let client = async_nats::connect("demo.nats.io").await?;
println!("connection state: {}", client.connection_state());

Trait Implementations§

Returns a copy of the value. Read more
Performs copy-assignment from source. Read more
Formats the value using the given formatter. Read more

Auto Trait Implementations§

Blanket Implementations§

Gets the TypeId of self. Read more
Immutably borrows from an owned value. Read more
Mutably borrows from an owned value. Read more

Returns the argument unchanged.

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Instruments this type with the current Span, returning an Instrumented wrapper. Read more

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Should always be Self
The resulting type after obtaining ownership.
Creates owned data from borrowed data, usually by cloning. Read more
Uses borrowed data to replace owned data, usually by cloning. Read more
The type returned in the event of a conversion error.
Performs the conversion.
The type returned in the event of a conversion error.
Performs the conversion.
Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more