pub struct Client { /* private fields */ }
Expand description
The entry point to the NATS client protocol
Implementations§
Source§impl Client
impl Client
Sourcepub fn with_connect(addresses: Vec<Address>, connect: Connect) -> Self
pub fn with_connect(addresses: Vec<Address>, connect: Connect) -> Self
Sourcepub async fn state(&self) -> ClientState
pub async fn state(&self) -> ClientState
Get the current state of the Client
Sourcepub async fn state_stream(&self) -> WatchReceiver<ClientState>
pub async fn state_stream(&self) -> WatchReceiver<ClientState>
Get a watch stream of Client
state transitions
Sourcepub async fn connect_mut(&self) -> ClientRefMut<'_, Connect>
pub async fn connect_mut(&self) -> ClientRefMut<'_, Connect>
Get a mutable reference to this Client
’s Connect
Sourcepub async fn addresses_mut(&self) -> ClientRefMut<'_, [Address]>
pub async fn addresses_mut(&self) -> ClientRefMut<'_, [Address]>
Get a mutable reference to the list of addresses used to try and establish a connection to a server.
Sourcepub async fn tcp_connect_timeout(&self) -> Duration
pub async fn tcp_connect_timeout(&self) -> Duration
Get the configured TCP connect timeout. [default = 10s
]
This is the timeout of a single connect attempt. It is not the timeout of the
connect
future which has no internal timeout.
Sourcepub async fn set_tcp_connect_timeout(
&self,
tcp_connect_timeout: Duration,
) -> &Self
pub async fn set_tcp_connect_timeout( &self, tcp_connect_timeout: Duration, ) -> &Self
Set the TCP connect timeout.
Sourcepub async fn delay_generator_mut(&self) -> ClientRefMut<'_, DelayGenerator>
pub async fn delay_generator_mut(&self) -> ClientRefMut<'_, DelayGenerator>
Get the DelayGenerator
The default generator is generated with generate_delay_generator
with the following parameters:
connect_series_attempts_before_cool_down
=3
connect_delay
=0s
connect_series_delay
=5s
cool_down
=60s
Sourcepub async fn subscription(
&self,
sid: Sid,
) -> Option<ClientRef<'_, Subscription>>
pub async fn subscription( &self, sid: Sid, ) -> Option<ClientRef<'_, Subscription>>
Return a reference to a Subscription
if the client is aware
of the specified subscription ID
Sourcepub async fn send_connect(&self) -> Result<()>
pub async fn send_connect(&self) -> Result<()>
Sourcepub async fn connect(&self)
pub async fn connect(&self)
Connect to a NATS server
This will randomly shuffle a list consisting of all explicitly specified
addresses and
addresses received in an INFO
message’s connect_urls
.
A randomized list of addresses is used to avoid a
thundering herd.
The client will continuously try to connect to each address in this list. The timeout of
each connect attempt is specified by the
tcp_connect_timeout
. The delay between
each connect attempt is specified by the
delay_generator_mut
.
When this future resolves, we are guaranteed to have entered the
Connected
state.
Unless, disconnect
was called.
Should the client become disconnected for any reason, other than calling
disconnect
, the client will continuously try
to reconnect. Upon a successful reconnect, the client will automatically subscribe to all
subscriptions.
Sourcepub async fn disconnect(&self)
pub async fn disconnect(&self)
Disconnect from the NATS server
When this future resolves, we are guaranteed to have entered the
Disconnected
state.
Note: Client
does not disconnect
when it is Drop
ped. In order to
avoid leaking futures, you must explicitly call disconnect
.
Sourcepub async fn publish(&self, subject: &Subject, payload: &[u8]) -> Result<()>
pub async fn publish(&self, subject: &Subject, payload: &[u8]) -> Result<()>
Convenience wrapper around publish_with_optional_reply
Sourcepub async fn publish_with_reply(
&self,
subject: &Subject,
reply_to: &Subject,
payload: &[u8],
) -> Result<()>
pub async fn publish_with_reply( &self, subject: &Subject, reply_to: &Subject, payload: &[u8], ) -> Result<()>
Convenience wrapper around publish_with_optional_reply
Sourcepub async fn publish_with_optional_reply(
&self,
subject: &Subject,
reply_to: Option<&Subject>,
payload: &[u8],
) -> Result<()>
pub async fn publish_with_optional_reply( &self, subject: &Subject, reply_to: Option<&Subject>, payload: &[u8], ) -> Result<()>
Sourcepub async fn request(&self, subject: &Subject, payload: &[u8]) -> Result<Msg>
pub async fn request(&self, subject: &Subject, payload: &[u8]) -> Result<Msg>
Implements the request-reply pattern
Sourcepub async fn request_with_timeout(
&self,
subject: &Subject,
payload: &[u8],
duration: Duration,
) -> Result<Msg>
pub async fn request_with_timeout( &self, subject: &Subject, payload: &[u8], duration: Duration, ) -> Result<Msg>
Implements the request-reply pattern with a timeout
Sourcepub async fn subscribe(
&self,
subject: &Subject,
buffer: usize,
) -> Result<(Sid, MpscReceiver<Msg>)>
pub async fn subscribe( &self, subject: &Subject, buffer: usize, ) -> Result<(Sid, MpscReceiver<Msg>)>
Convenience wrapper around subscribe_with_optional_queue_group
Sourcepub async fn subscribe_with_queue_group(
&self,
subject: &Subject,
queue_group: &str,
buffer: usize,
) -> Result<(Sid, MpscReceiver<Msg>)>
pub async fn subscribe_with_queue_group( &self, subject: &Subject, queue_group: &str, buffer: usize, ) -> Result<(Sid, MpscReceiver<Msg>)>
Convenience wrapper around subscribe_with_optional_queue_group
Sourcepub async fn subscribe_with_optional_queue_group(
&self,
subject: &Subject,
queue_group: Option<&str>,
buffer: usize,
) -> Result<(Sid, MpscReceiver<Msg>)>
pub async fn subscribe_with_optional_queue_group( &self, subject: &Subject, queue_group: Option<&str>, buffer: usize, ) -> Result<(Sid, MpscReceiver<Msg>)>
Sourcepub async fn unsubscribe(&self, sid: Sid) -> Result<()>
pub async fn unsubscribe(&self, sid: Sid) -> Result<()>
Convenience wrapper around unsubscribe_optional_max_msgs
Sourcepub async fn unsubscribe_with_max_msgs(
&self,
sid: Sid,
max_msgs: u64,
) -> Result<()>
pub async fn unsubscribe_with_max_msgs( &self, sid: Sid, max_msgs: u64, ) -> Result<()>
Convenience wrapper around unsubscribe_optional_max_msgs
Sourcepub async fn unsubscribe_optional_max_msgs(
&self,
sid: Sid,
max_msgs: Option<u64>,
) -> Result<()>
pub async fn unsubscribe_optional_max_msgs( &self, sid: Sid, max_msgs: Option<u64>, ) -> Result<()>
Sourcepub async fn unsubscribe_all(&self) -> Result<()>
pub async fn unsubscribe_all(&self) -> Result<()>
Unsubscribe from all subscriptions
Sourcepub async fn info_stream(&self) -> WatchReceiver<Info>
pub async fn info_stream(&self) -> WatchReceiver<Info>
Get a watch stream of INFO
messages received from the server
Sourcepub async fn ping_stream(&self) -> WatchReceiver<()>
pub async fn ping_stream(&self) -> WatchReceiver<()>
Get a watch stream of PING
messages received from the server
Sourcepub async fn pong_stream(&self) -> WatchReceiver<()>
pub async fn pong_stream(&self) -> WatchReceiver<()>
Get a watch stream of PONG
messages received from the server
Sourcepub async fn ok_stream(&self) -> WatchReceiver<()>
pub async fn ok_stream(&self) -> WatchReceiver<()>
Get a watch stream of +OK
messages received from the server
Sourcepub async fn err_stream(&self) -> WatchReceiver<ProtocolError>
pub async fn err_stream(&self) -> WatchReceiver<ProtocolError>
Get a watch stream of -ERR
messages received from the server
Sourcepub async fn ping(&self) -> Result<()>
pub async fn ping(&self) -> Result<()>
Send a PING
to the server.
This method, coupled with a pong_stream
, can be a useful way to check that the client is
still connected to the server.