[][src]Struct nats::Options

pub struct Options { /* fields omitted */ }

Connect options.

Implementations

impl Options[src]

pub fn new() -> Options[src]

Options for establishing a new NATS Connection.

Example

let options = nats::Options::new();
let nc = options.connect("demo.nats.io")?;

pub fn with_token(token: &str) -> Options[src]

Authenticate with NATS using a token.

Example

let nc = nats::Options::with_token("t0k3n!")
    .connect("demo.nats.io")?;

pub fn with_user_pass(user: &str, password: &str) -> Options[src]

Authenticate with NATS using a username and password.

Example

let nc = nats::Options::with_user_pass("derek", "s3cr3t!")
    .connect("demo.nats.io")?;

pub fn with_credentials(path: impl AsRef<Path>) -> Options[src]

Authenticate with NATS using a .creds file.

Example

let nc = nats::Options::with_credentials("path/to/my.creds")
    .connect("connect.ngs.global")?;

pub fn with_nkey<F>(public_key: &str, sig_cb: F) -> Options where
    F: Fn(&[u8]) -> Vec<u8> + Send + Sync + 'static, 
[src]

Authenticate with NATS using a public key and a signature function.

Example

let public_key = "UAMMBNV2EYR65NYZZ7IAK5SIR5ODNTTERJOBOF4KJLMWI45YOXOSWULM";
let private_key = "SUANQDPB2RUOE4ETUA26CNX7FUKE5ZZKFCQIIW63OX225F2CO7UEXTM7ZY";
let kp = nkeys::KeyPair::from_seed(private_key).unwrap();

let nc = nats::Options::with_nkey(public_key, move |nonce| kp.sign(nonce).unwrap())
    .connect("localhost")?;

pub fn with_name(self, name: &str) -> Options[src]

Add a name option to this configuration.

Example

let nc = nats::Options::new()
    .with_name("My App")
    .connect("demo.nats.io")?;

pub fn no_echo(self) -> Options[src]

Select option to not deliver messages that we have published.

Example

let nc = nats::Options::new()
    .no_echo()
    .connect("demo.nats.io")?;

pub fn max_reconnects(self, max_reconnects: Option<usize>) -> Options[src]

Set the maximum number of reconnect attempts. If no servers remain that are under this threshold, all servers will still be attempted.

Example

let nc = nats::Options::new()
    .max_reconnects(Some(3))
    .connect("demo.nats.io")?;

pub fn reconnect_buffer_size(self, reconnect_buffer_size: usize) -> Options[src]

Set the maximum amount of bytes to buffer when accepting outgoing traffic in disconnected mode.

The default value is 8mb.

Example

let nc = nats::Options::new()
    .reconnect_buffer_size(64 * 1024)
    .connect("demo.nats.io")?;

pub fn connect(self, nats_url: &str) -> Result<Connection>[src]

Establish a Connection with a NATS server.

Multiple servers may be specified by separating them with commas.

Example

let options = nats::Options::new();
let nc = options.connect("demo.nats.io")?;

In the below case, the second server is configured to use TLS but the first one is not. Using the tls_required method can ensure that all servers are connected to with TLS, if that is your intention.

let options = nats::Options::new();
let nc = options.connect("nats://demo.nats.io:4222,tls://demo.nats.io:4443")?;

pub async fn connect_async<'_>(self, nats_url: &'_ str) -> Result<Connection>[src]

Establish a Connection with a NATS server asynchronously.

Multiple servers may be specified by separating them with commas.

Example

let options = nats::Options::new();
let nc = options
    .connect_async("nats://demo.nats.io:4222,tls://demo.nats.io:4443")
    .await?;

pub fn disconnect_callback<F>(self, cb: F) -> Self where
    F: Fn() + Send + Sync + 'static, 
[src]

Set a callback to be executed when connectivity to a server has been lost.

pub fn reconnect_callback<F>(self, cb: F) -> Self where
    F: Fn() + Send + Sync + 'static, 
[src]

Set a callback to be executed when connectivity to a server has been established.

pub fn close_callback<F>(self, cb: F) -> Self where
    F: Fn() + Send + Sync + 'static, 
[src]

Set a callback to be executed when the client has been closed due to exhausting reconnect retries to known servers or by completing a drain request.

pub fn reconnect_delay_callback<F>(self, cb: F) -> Self where
    F: Fn(usize) -> Duration + Send + Sync + 'static, 
[src]

Set a callback to be executed for calculating the backoff duration to wait before a server reconnection attempt.

The function takes the number of reconnects as an argument and returns the Duration that should be waited before making the next connection attempt.

It is recommended that some random jitter is added to your returned Duration.

pub fn tls_required(self, tls_required: bool) -> Options[src]

Setting this requires that TLS be set for all server connections.

If you only want to use TLS for some server connections, you may declare them separately in the connect string by prefixing them with tls://host:port instead of nats://host:port.

Examples


let nc = nats::Options::new()
    .tls_required(true)
    .connect("tls://demo.nats.io:4443")?;

pub fn add_root_certificate(self, path: impl AsRef<Path>) -> Options[src]

Adds a root certificate file.

The file must be PEM encoded. All certificates in the file will be used.

Examples


let nc = nats::Options::new()
    .add_root_certificate("my-certs.pem")
    .connect("tls://demo.nats.io:4443")?;

Trait Implementations

impl Debug for Options[src]

impl Default for Options[src]

Auto Trait Implementations

impl !RefUnwindSafe for Options

impl Send for Options

impl Sync for Options

impl Unpin for Options

impl !UnwindSafe for Options

Blanket Implementations

impl<T> Any for T where
    T: 'static + ?Sized
[src]

impl<T> Borrow<T> for T where
    T: ?Sized
[src]

impl<T> BorrowMut<T> for T where
    T: ?Sized
[src]

impl<T> From<T> for T[src]

impl<T, U> Into<U> for T where
    U: From<T>, 
[src]

impl<T> Same<T> for T

type Output = T

Should always be Self

impl<T, U> TryFrom<U> for T where
    U: Into<T>, 
[src]

type Error = Infallible

The type returned in the event of a conversion error.

impl<T, U> TryInto<U> for T where
    U: TryFrom<T>, 
[src]

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

The type returned in the event of a conversion error.

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