Connecting to the Default Server
Some libraries also provide a special way to connect to a default url, which is generally nats://localhost:4222:
Go
Java
JavaScript
Python
Ruby
C
1
nc, err := nats.Connect(nats.DefaultURL)
2
if err != nil {
3
log.Fatal(err)
4
}
5
defer nc.Close()
6
7
// Do something with the connection
Copied!
1
Connection nc = Nats.connect();
2
3
// Do something with the connection
4
5
nc.close();
Copied!
1
const nc = await connect();
2
// Do something with the connection
3
doSomething();
4
// When done close it
5
await nc.close();
Copied!
1
nc = NATS()
2
await nc.connect()
3
4
# Do something with the connection
5
6
await nc.close()
Copied!
1
require 'nats/client'
2
3
NATS.start do |nc|
4
# Do something with the connection
5
6
# Close the connection
7
nc.close
8
end
Copied!
1
natsConnection *conn = NULL;
2
natsStatus s;
3
4
s = natsConnection_ConnectTo(&conn, NATS_DEFAULT_URL);
5
if (s != NATS_OK)
6
// handle error
7
8
// Destroy connection, no-op if conn is NULL.
9
natsConnection_Destroy(conn);
Copied!
Export as PDF
Copy link
Edit on GitHub