Powered By GitBook
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
TypeScript
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
let nc = NATS.connect();
2
nc.on('connect', (c) => {
3
// Do something with the connection
4
doSomething();
5
// When done close it
6
nc.close();
7
});
8
nc.on('error', (err) => {
9
failed(err);
10
});
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
// will throw an exception if connection fails
2
let nc = await connect();
3
// Do something with the connection
4
5
// When done close it
6
nc.close();
7
8
9
// alternatively, you can use the Promise pattern
10
let nc1: Client;
11
connect()
12
.then((c) => {
13
nc1 = c;
14
// Do something with the connection
15
nc1.close();
16
});
17
// add a .catch/.finally
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!
Last modified 1yr ago
Export as PDF
Copy link