NATS Docs
NATS.io
Twitter
Slack
GitHub
Search…
Welcome
Release Notes
What's New!
NATS Concepts
Overview
What is NATS
Subject-Based Messaging
Core NATS
JetStream
Subject Mapping and Partitioning
NATS Service Infrastructure
Security
Connectivity
Using NATS
NATS Tools
Developing With NATS
Anatomy of a NATS application
Connecting
Connecting to the Default Server
Connecting to a Specific Server
Connecting to a Cluster
Connection Name
Authenticating with a User and Password
Authenticating with a Token
Authenticating with an NKey
Authenticating with a Credentials File
Encrypting Connections with TLS
Setting a Connect Timeout
Ping/Pong Protocol
Turning Off Echo'd Messages
Miscellaneous functionalities
Automatic Reconnections
Monitoring the Connection
Receiving Messages
Sending Messages
JetStream
Tutorials
Running a NATS service
Installing, running and deploying a NATS Server
Environmental considerations
NATS and Docker
NATS and Kubernetes
NATS Server Clients
Configuring NATS Server
Managing and Monitoring your NATS Server Infrastructure
Reference
FAQ
NATS Protocols
Legacy
STAN aka 'NATS Streaming'
nats-account-server
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
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!
Previous
Connecting
Next
Connecting to a Specific Server
Last modified
6mo ago
Export as PDF
Copy link
Edit on GitHub