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
impl Client
Sourcepub fn timeout(&self) -> Option<Duration>
pub fn timeout(&self) -> Option<Duration>
Returns the default timeout for requests set when creating the client.
§Examples
let client = async_nats::connect("demo.nats.io").await?;
println!("default request timeout: {:?}", client.timeout());
Sourcepub fn server_info(&self) -> ServerInfo
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());
Sourcepub fn is_server_compatible(&self, major: i64, minor: i64, patch: i64) -> bool
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.
This has to be used with caution, as it is not guaranteed that the server that client is connected to is the same version that the one that is a JetStream meta/stream/consumer leader, especially across leafnodes.
§Examples
let client = async_nats::connect("demo.nats.io").await?;
assert!(client.is_server_compatible(2, 8, 4));
Sourcepub async fn publish<S: ToSubject>(
&self,
subject: S,
payload: Bytes,
) -> Result<(), PublishError>
pub async fn publish<S: ToSubject>( &self, subject: S, payload: Bytes, ) -> Result<(), PublishError>
Sourcepub async fn publish_with_headers<S: ToSubject>(
&self,
subject: S,
headers: HeaderMap,
payload: Bytes,
) -> Result<(), PublishError>
pub async fn publish_with_headers<S: ToSubject>( &self, subject: S, 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", headers, "payload".into())
.await?;
Sourcepub async fn publish_with_reply<S: ToSubject, R: ToSubject>(
&self,
subject: S,
reply: R,
payload: Bytes,
) -> Result<(), PublishError>
pub async fn publish_with_reply<S: ToSubject, R: ToSubject>( &self, subject: S, reply: R, 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", "reply_subject", "payload".into())
.await?;
Sourcepub async fn publish_with_reply_and_headers<S: ToSubject, R: ToSubject>(
&self,
subject: S,
reply: R,
headers: HeaderMap,
payload: Bytes,
) -> Result<(), PublishError>
pub async fn publish_with_reply_and_headers<S: ToSubject, R: ToSubject>( &self, subject: S, reply: R, 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", "reply_subject", headers, "payload".into())
.await?;
Sourcepub async fn request<S: ToSubject>(
&self,
subject: S,
payload: Bytes,
) -> Result<Message, RequestError>
pub async fn request<S: ToSubject>( &self, subject: S, payload: Bytes, ) -> Result<Message, RequestError>
Sends the request with headers.
§Examples
let client = async_nats::connect("demo.nats.io").await?;
let response = client.request("service", "data".into()).await?;
Sourcepub async fn request_with_headers<S: ToSubject>(
&self,
subject: S,
headers: HeaderMap,
payload: Bytes,
) -> Result<Message, RequestError>
pub async fn request_with_headers<S: ToSubject>( &self, subject: S, headers: HeaderMap, payload: Bytes, ) -> Result<Message, RequestError>
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", headers, "data".into())
.await?;
Sourcepub async fn send_request<S: ToSubject>(
&self,
subject: S,
request: Request,
) -> Result<Message, RequestError>
pub async fn send_request<S: ToSubject>( &self, subject: S, request: Request, ) -> Result<Message, RequestError>
Sourcepub fn new_inbox(&self) -> String
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?;
Sourcepub async fn subscribe<S: ToSubject>(
&self,
subject: S,
) -> Result<Subscriber, SubscribeError>
pub async fn subscribe<S: ToSubject>( &self, subject: S, ) -> Result<Subscriber, SubscribeError>
Sourcepub async fn queue_subscribe<S: ToSubject>(
&self,
subject: S,
queue_group: String,
) -> Result<Subscriber, SubscribeError>
pub async fn queue_subscribe<S: ToSubject>( &self, subject: S, queue_group: String, ) -> Result<Subscriber, SubscribeError>
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.>", "queue".into()).await?;
while let Some(message) = subscription.next().await {
println!("received message: {:?}", message);
}
Sourcepub async fn flush(&self) -> Result<(), FlushError>
pub async fn flush(&self) -> Result<(), FlushError>
Flushes the internal buffer ensuring that all messages are sent.
§Examples
let client = async_nats::connect("demo.nats.io").await?;
client.flush().await?;
Sourcepub async fn drain(&self) -> Result<(), DrainError>
pub async fn drain(&self) -> Result<(), DrainError>
Drains all subscriptions, stops any new messages from being published, and flushes any remaining messages, then closes the connection. Once completed, any associated streams associated with the client will be closed, and further client commands will fail
§Examples
use futures::StreamExt;
let client = async_nats::connect("demo.nats.io").await?;
let mut subscription = client.subscribe("events.>").await?;
client.drain().await?;
assert!(subscription.next().await.is_none());
client
.subscribe("events.>")
.await
.expect_err("Expected further commands to fail");
Sourcepub fn connection_state(&self) -> State
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());
Sourcepub async fn force_reconnect(&self) -> Result<(), ReconnectError>
pub async fn force_reconnect(&self) -> Result<(), ReconnectError>
Forces the client to reconnect.
Keep in mind that client will reconnect automatically if the connection is lost and this
method does not have to be used in normal circumstances.
However, if you want to force the client to reconnect, for example to re-trigger
the auth-callback
, or manually rebalance connections, this method can be useful.
This method does not wait for connection to be re-established.
§Examples
let client = async_nats::connect("demo.nats.io").await?;
client.force_reconnect().await?;
Sourcepub fn statistics(&self) -> Arc<Statistics>
pub fn statistics(&self) -> Arc<Statistics>
Returns struct representing statistics of the whole lifecycle of the client. This includes number of bytes sent/received, number of messages sent/received, and number of times the connection was established. As this returns Arc with AtomicU64 fields, it can be safely reused and shared across threads.
§Examples
let client = async_nats::connect("demo.nats.io").await?;
let statistics = client.statistics();
println!("client statistics: {:#?}", statistics);
Trait Implementations§
Source§impl ServiceExt for Client
Available on crate feature service
only.
impl ServiceExt for Client
service
only.Source§impl Sink<PublishMessage> for Client
impl Sink<PublishMessage> for Client
Source§type Error = Error<PublishErrorKind>
type Error = Error<PublishErrorKind>
Source§fn poll_ready(
self: Pin<&mut Self>,
cx: &mut Context<'_>,
) -> Poll<Result<(), Self::Error>>
fn poll_ready( self: Pin<&mut Self>, cx: &mut Context<'_>, ) -> Poll<Result<(), Self::Error>>
Sink
to receive a value. Read moreSource§fn start_send(
self: Pin<&mut Self>,
msg: PublishMessage,
) -> Result<(), Self::Error>
fn start_send( self: Pin<&mut Self>, msg: PublishMessage, ) -> Result<(), Self::Error>
poll_ready
which returned Poll::Ready(Ok(()))
. Read moreAuto 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> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T, Item> SinkExt<Item> for T
impl<T, Item> SinkExt<Item> for T
Source§fn with<U, Fut, F, E>(self, f: F) -> With<Self, Item, U, Fut, F>
fn with<U, Fut, F, E>(self, f: F) -> With<Self, Item, U, Fut, F>
Source§fn with_flat_map<U, St, F>(self, f: F) -> WithFlatMap<Self, Item, U, St, F>
fn with_flat_map<U, St, F>(self, f: F) -> WithFlatMap<Self, Item, U, St, F>
Source§fn sink_map_err<E, F>(self, f: F) -> SinkMapErr<Self, F>
fn sink_map_err<E, F>(self, f: F) -> SinkMapErr<Self, F>
Source§fn sink_err_into<E>(self) -> SinkErrInto<Self, Item, E>
fn sink_err_into<E>(self) -> SinkErrInto<Self, Item, E>
Into
trait. Read moreSource§fn buffer(self, capacity: usize) -> Buffer<Self, Item>where
Self: Sized,
fn buffer(self, capacity: usize) -> Buffer<Self, Item>where
Self: Sized,
alloc
only.Source§fn flush(&mut self) -> Flush<'_, Self, Item>where
Self: Unpin,
fn flush(&mut self) -> Flush<'_, Self, Item>where
Self: Unpin,
Source§fn send(&mut self, item: Item) -> Send<'_, Self, Item>where
Self: Unpin,
fn send(&mut self, item: Item) -> Send<'_, Self, Item>where
Self: Unpin,
Source§fn feed(&mut self, item: Item) -> Feed<'_, Self, Item>where
Self: Unpin,
fn feed(&mut self, item: Item) -> Feed<'_, Self, Item>where
Self: Unpin,
Source§fn send_all<'a, St>(&'a mut self, stream: &'a mut St) -> SendAll<'a, Self, St>
fn send_all<'a, St>(&'a mut self, stream: &'a mut St) -> SendAll<'a, Self, St>
Source§fn right_sink<Si1>(self) -> Either<Si1, Self>
fn right_sink<Si1>(self) -> Either<Si1, Self>
Source§fn poll_ready_unpin(
&mut self,
cx: &mut Context<'_>,
) -> Poll<Result<(), Self::Error>>where
Self: Unpin,
fn poll_ready_unpin(
&mut self,
cx: &mut Context<'_>,
) -> Poll<Result<(), Self::Error>>where
Self: Unpin,
Sink::poll_ready
on Unpin
sink types.Source§fn start_send_unpin(&mut self, item: Item) -> Result<(), Self::Error>where
Self: Unpin,
fn start_send_unpin(&mut self, item: Item) -> Result<(), Self::Error>where
Self: Unpin,
Sink::start_send
on Unpin
sink types.