pub struct Connection(_);
Expand description

A NATS connection.

Implementations

Create a subscription for the given NATS connection.

Example
let sub = nc.subscribe("foo")?;

Create a queue subscription for the given NATS connection.

Example
let sub = nc.queue_subscribe("foo", "production")?;

Publish a message on the given subject.

Example
nc.publish("foo", "Hello World!")?;

Publish a message on the given subject with a reply subject for responses.

Example
let reply = nc.new_inbox();
let rsub = nc.subscribe(&reply)?;
nc.publish_request("foo", &reply, "Help me!")?;

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

Example
let reply = nc.new_inbox();
let rsub = nc.subscribe(&reply)?;

Publish a message on the given subject as a request and receive the response.

Example
let resp = nc.request("foo", "Help me?")?;

Publish a message on the given subject as a request and receive the response. This call will return after the timeout duration if no response is received.

Example
let resp = nc.request_timeout("foo", "Help me?", std::time::Duration::from_secs(2))?;

Publish a message on the given subject as a request and allow multiple responses.

Example
for msg in nc.request_multi("foo", "Help")?.iter().take(1) {}

Flush a NATS connection by sending a PING protocol and waiting for the responding PONG. Will fail with TimedOut if the server does not respond with in 10 seconds. Will fail with NotConnected if the server is not currently connected. Will fail with BrokenPipe if the connection to the server is lost.

Example
nc.flush()?;

Flush a NATS connection by sending a PING protocol and waiting for the responding PONG. Will fail with TimedOut if the server takes longer than this duration to respond. Will fail with NotConnected if the server is not currently connected. Will fail with BrokenPipe if the connection to the server is lost.

Example
nc.flush()?;

Close a NATS connection. All clones of this Connection will also be closed, as the backing IO threads are shared.

If the client is currently connected to a server, the outbound write buffer will be flushed in the process of shutting down.

Example
nc.close();

Calculates the round trip time between this client and the server, if the server is currently connected. Fails with TimedOut if the server takes more than 10 seconds to respond.

Example
println!("server rtt: {:?}", nc.rtt());

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

Returns the client IP as known by the server. Supported as of server version 2.1.6.

Example
println!("ip: {:?}", nc.client_ip());

Returns the client ID as known by the most recently connected server.

Example
println!("ip: {:?}", nc.client_id());

Send an unsubscription for all subs then flush the connection, allowing any unprocessed messages to be handled by a handler function if one is configured.

After the flush returns, we know that a round-trip to the server has happened after it received our unsubscription, so we shut down the subscriber afterwards.

A similar method exists for the Subscription struct which will drain a single Subscription without shutting down the entire connection afterward.

Example
let received = Arc::new(AtomicBool::new(false));
let received_2 = received.clone();

nc.subscribe("test.drain")?.with_handler(move |m| {
    received_2.store(true, SeqCst);
    Ok(())
});

nc.publish("test.drain", "message")?;
nc.drain()?;


assert!(received.load(SeqCst));

Publish a message which may have a reply subject or headers set.

Example
let sub = nc.subscribe("foo.headers")?;
let headers = [("header1", "value1"),
               ("header2", "value2")].iter().collect();
let reply_to = None;
nc.publish_with_reply_or_headers("foo.headers", reply_to, Some(&headers), "Hello World!")?;
nc.flush()?;
let message = sub.next_timeout(std::time::Duration::from_secs(2)).unwrap();
assert_eq!(message.headers.unwrap().len(), 2);

Returns the maximum payload size the most recently connected server will accept.

Example
let nc = nats::connect("demo.nats.io")?;
println!("max payload: {:?}", nc.max_payload());

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.

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

🔬 This is a nightly-only experimental API. (toowned_clone_into)

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.