Struct async_nats::client::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
impl Client
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.
Examples
let client = async_nats::connect("demo.nats.io").await?;
assert!(client.is_server_compatible(2, 8, 4));
sourcepub async fn publish_with_headers(
&self,
subject: String,
headers: HeaderMap,
payload: Bytes
) -> Result<(), PublishError>
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?;
sourcepub async fn publish_with_reply(
&self,
subject: String,
reply: String,
payload: Bytes
) -> Result<(), PublishError>
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?;
sourcepub async fn publish_with_reply_and_headers(
&self,
subject: String,
reply: String,
headers: HeaderMap,
payload: Bytes
) -> Result<(), PublishError>
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?;
sourcepub async fn request(
&self,
subject: String,
payload: Bytes
) -> Result<Message, RequestError>
pub async fn request( &self, subject: String, 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".into(), "data".into()).await?;
sourcepub async fn request_with_headers(
&self,
subject: String,
headers: HeaderMap,
payload: Bytes
) -> Result<Message, RequestError>
pub async fn request_with_headers( &self, subject: String, 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".into(), headers, "data".into())
.await?;
sourcepub async fn send_request(
&self,
subject: String,
request: Request
) -> Result<Message, RequestError>
pub async fn send_request( &self, subject: String, 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(
&self,
subject: String
) -> Result<Subscriber, SubscribeError>
pub async fn subscribe( &self, subject: String ) -> Result<Subscriber, SubscribeError>
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);
}
sourcepub async fn queue_subscribe(
&self,
subject: String,
queue_group: String
) -> Result<Subscriber, SubscribeError>
pub async fn queue_subscribe( &self, subject: String, 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.>".into(), "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 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());
Trait Implementations§
source§impl ServiceExt for Client
Available on crate feature service
only.
impl ServiceExt for Client
service
only.