pub fn connect<I: IntoServerList>(nats_urls: I) -> Result<Connection>
Expand description
Connect to one or more NATS servers at the given URLs.
The IntoServerList
trait allows to pass URLs in various different formats. Furthermore, if
you need more control of the connection’s parameters use Options::connect()
.
Warning: There are asynchronous errors that can happen during operation of NATS client.
To handle them, add handler for Options::error_callback()
.
§Examples
If no scheme is provided the nats://
scheme is assumed. The default port is 4222
.
let nc = nats::connect("demo.nats.io")?;
It is possible to provide several URLs as a comma separated list.
let nc = nats::connect("demo.nats.io,tls://demo.nats.io:4443")?;
Alternatively, an array of strings can be passed.
let nc = nats::connect(&["demo.nats.io", "tls://demo.nats.io:4443"])?;
Instead of using strings, ServerAddress
es can be used directly as well. This is handy for
validating user input.
use nats::ServerAddress;
use std::io;
use structopt::StructOpt;
#[derive(Debug, StructOpt)]
struct Config {
#[structopt(short, long = "server", default_value = "demo.nats.io")]
servers: Vec<ServerAddress>,
}
fn main() -> io::Result<()> {
let config = Config::from_args();
let nc = nats::connect(config.servers)?;
Ok(())
}