Skip to content

Latest commit

 

History

History
75 lines (59 loc) · 1.22 KB

File metadata and controls

75 lines (59 loc) · 1.22 KB

Connecting to the Default Server

Some libraries also provide a special way to connect to a default url, which is generally nats://localhost:4222:

{% tabs %} {% tab title="Go" %}

nc, err := nats.Connect(nats.DefaultURL)
if err != nil {
    log.Fatal(err)
}
defer nc.Close()

// Do something with the connection

{% endtab %}

{% tab title="Java" %}

Connection nc = Nats.connect();

// Do something with the connection

nc.close();

{% endtab %}

{% tab title="JavaScript" %}

const nc = await connect();
// Do something with the connection
doSomething();
// When done close it
await nc.close();

{% endtab %}

{% tab title="Python" %}

nc = NATS()
await nc.connect()

# Do something with the connection

await nc.close()

{% endtab %}

{% tab title="Ruby" %}

require 'nats/client'

NATS.start do |nc|
   # Do something with the connection

   # Close the connection
   nc.close
end

{% endtab %}

{% tab title="C" %}

natsConnection      *conn = NULL;
natsStatus          s;

s = natsConnection_ConnectTo(&conn, NATS_DEFAULT_URL);
if (s != NATS_OK)
  // handle error

// Destroy connection, no-op if conn is NULL.
natsConnection_Destroy(conn);

{% endtab %} {% endtabs %}