nightly_async_nats

Struct 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§

Source§

impl Client

Source

pub fn server_info(&self) -> ServerInfo

Returns last received info from the server.

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

pub fn is_server_compatible(&self, major: i64, minor: i64, patch: i64) -> bool

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));
Source

pub async fn publish( &self, subject: String, payload: Bytes, ) -> Result<(), PublishError>

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?;
Source

pub async fn publish_with_headers( &self, subject: String, headers: HeaderMap, payload: Bytes, ) -> Result<(), PublishError>

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?;
Source

pub async fn publish_with_reply( &self, subject: String, reply: String, payload: Bytes, ) -> Result<(), PublishError>

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?;
Source

pub async fn publish_with_reply_and_headers( &self, subject: String, reply: String, headers: HeaderMap, payload: Bytes, ) -> Result<(), PublishError>

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?;
Source

pub async fn request( &self, subject: String, payload: Bytes, ) -> Result<Message, Error>

Sends the request with headers.

§Examples

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

pub async fn request_with_headers( &self, subject: String, headers: HeaderMap, payload: Bytes, ) -> Result<Message, Error>

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?;
Source

pub async fn send_request( &self, subject: String, request: Request, ) -> Result<Message, Error>

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?;
Source

pub fn new_inbox(&self) -> String

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

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

pub async fn subscribe(&self, subject: String) -> Result<Subscriber, Error>

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

pub async fn queue_subscribe( &self, subject: String, queue_group: String, ) -> Result<Subscriber, Error>

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

pub async fn flush(&self) -> Result<(), Error>

Flushes the internal buffer ensuring that all messages are sent.

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

pub fn connection_state(&self) -> State

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§

Source§

impl Clone for Client

Source§

fn clone(&self) -> Client

Returns a copy of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for Client

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

impl Freeze for Client

§

impl !RefUnwindSafe for Client

§

impl Send for Client

§

impl Sync for Client

§

impl Unpin for Client

§

impl !UnwindSafe for Client

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dst: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dst. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

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

fn in_current_span(self) -> Instrumented<Self>

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

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

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

Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V

Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

impl<T> ErasedDestructor for T
where T: 'static,

Source§

impl<T> MaybeSendSync for T