Connecting
In order for a NATS client application to connect to the NATS service, and then subscribe or publish messages to subjects, it needs to be able to be configured with the details of how to connect to the NATS service infrastructure and of how to authenticate with it.

NATS URL

  1. 1.
    A 'NATS URL' which is a string (in a URL format) that specifies the IP address and port where the NATS server(s) can be reached, and what kind of connection to establish:
    • Plain un-encrypted TCP connection (i.e. NATS URLs starting with nats://...)
    • TLS encrypted TCP connection (i.e. NATS URLs starting with tls://...)
    • Websocket connection (i.e. NATS URLs starting with ws://...)

Connecting to clusters

Note that when connecting to a NATS service infrastructure with clusters there is more than one URL and the application should allow for more than one URL to be specified in its NATS connect call (typically you pass a comma separated list of URLs as the URL, e.g. "nats://server1:port1,nats://server2:port2").
When connecting to a cluster it is best to provide the complete set of 'seed' URLs for the cluster.

Authentication details

  1. 1.
    If required: authentication details for the application to identify itself with the NATS server(s). NATS supports multiple authentication schemes:

Runtime configuration

Your application should expose a way to be configured at run time with a NATS URL(s) to use, and if you want to use a secure infrastructure, which credentials (.creds) file to use (or if needed with a way to set the token or Nkey, usernames and passwords can be encoded in the NATS URL).

Connection Options

Besides the connectivity and security details, there are numerous options for a NATS connection ranging from timeouts to reconnect settings to setting asynchronous error and connection event callback handlers in your application.

See Also

WebSocket and NATS
WebSocket and NATS | Hello World
NATS WebSockets and React
NATS WebSockets and React
Last modified 10d ago