Connecting to a Specific Server
// If connecting to the default port, the URL can be simplified
// to just the hostname/IP.
// That is, the connect below is equivalent to:
// nats.Connect("nats://demo.nats.io:4222")
nc, err := nats.Connect("demo.nats.io")
if err != nil {
log.Fatal(err)
}
defer nc.Close()
// Do something with the connection nc = Nats.connect("nats://demo.nats.io:4222");// Connection is AutoCloseable
try (Connection nc = Nats.connect("nats://demo.nats.io:4222")) {
// Do something with the connection
}const nc = await connect({ servers: "demo.nats.io" });
// Do something with the connection
doSomething();
await nc.close();nc = NATS()
await nc.connect(servers=["nats://demo.nats.io:4222"])
# Do something with the connection
await nc.close()Last updated
Was this helpful?