Ping/Pong Protocol

NATS client applications use a PING/PONG protocol to check that there is a working connection to the NATS service. Periodically the client will send PING messages to the server, which responds with a PONG. This period is configured by specifying a ping interval on the client connection settings.

Ping-Pong Protocol - 图1

The connection will be closed as stale when the client reaches a number of pings which recieved no pong in response, which is configured by specifying the maximum pings outstanding on the client connection settings.

The ping interval and the maximum pings outstanding work together to specify how quickly the client connection will be notified of a problem. This will also help when there is a remote network partition where the operating system does not detect a socket error. Upon connection close, the client will attempt to reconnect. When it knows about other servers, these will be tried next.

In the presence of traffic, such as messages or client side pings, the server will not initiate the PING/PONG interaction.

On connections with significant traffic, the client will often figure out there is a problem between PINGS, and as a result the default ping interval is typically on the order of minutes. To close an unresponsive connection after 100s, set the ping interval to 20s and the maximum pings outstanding to 5:

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

  1. // Set Ping Interval to 20 seconds and Max Pings Outstanding to 5
  2. nc, err := nats.Connect("demo.nats.io", nats.Name("API Ping Example"), nats.PingInterval(20*time.Second), nats.MaxPingsOutstanding(5))
  3. if err != nil {
  4. log.Fatal(err)
  5. }
  6. defer nc.Close()
  7. // Do something with the connection

{% endtab %}

{% tab title=”Java” %}

  1. Options options = new Options.Builder()
  2. .server("nats://demo.nats.io")
  3. .pingInterval(Duration.ofSeconds(20)) // Set Ping Interval
  4. .maxPingsOut(5) // Set max pings in flight
  5. .build();
  6. // Connection is AutoCloseable
  7. try (Connection nc = Nats.connect(options)) {
  8. // Do something with the connection
  9. }

{% endtab %}

{% tab title=”JavaScript” %}

  1. // Set Ping Interval to 20 seconds and Max Pings Outstanding to 5
  2. const nc = await connect({
  3. pingInterval: 20 * 1000,
  4. maxPingOut: 5,
  5. servers: ["demo.nats.io:4222"],
  6. });

{% endtab %}

{% tab title=”Python” %}

  1. nc = NATS()
  2. await nc.connect(
  3. servers=["nats://demo.nats.io:4222"],
  4. # Set Ping Interval to 20 seconds and Max Pings Outstanding to 5
  5. ping_interval=20,
  6. max_outstanding_pings=5,
  7. )
  8. # Do something with the connection.

{% endtab %}

{% tab title=”C# V1” %}

  1. Options opts = ConnectionFactory.GetDefaultOptions();
  2. opts.Url = "nats://demo.nats.io";
  3. opts.PingInterval = 20000; // Set Ping Interval in milliseconds
  4. opts.MaxPingsOut = 5; // Set max pings in flight
  5. // IConnection is IDisposable
  6. using (IConnection nc = new ConnectionFactory().CreateConnection(opts))
  7. {
  8. Console.WriteLine(nc.ServerInfo);
  9. // Do something with the connection
  10. }

{% tab title=”Ruby” %}

  1. require 'nats/client'
  2. # Set Ping Interval to 20 seconds and Max Pings Outstanding to 5
  3. NATS.start(ping_interval: 20, max_outstanding_pings: 5) do |nc|
  4. nc.on_reconnect do
  5. puts "Got reconnected to #{nc.connected_server}"
  6. end
  7. nc.on_disconnect do |reason|
  8. puts "Got disconnected! #{reason}"
  9. end
  10. # Do something with the connection
  11. end

{% endtab %}

{% tab title=”C” %}

  1. natsConnection *conn = NULL;
  2. natsOptions *opts = NULL;
  3. natsStatus s = NATS_OK;
  4. s = natsOptions_Create(&opts);
  5. if (s == NATS_OK)
  6. // Set Ping interval to 20 seconds (20,000 milliseconds)
  7. s = natsOptions_SetPingInterval(opts, 20000);
  8. if (s == NATS_OK)
  9. // Set the limit to 5
  10. s = natsOptions_SetMaxPingsOut(opts, 5);
  11. if (s == NATS_OK)
  12. s = natsConnection_Connect(&conn, opts);
  13. (...)
  14. // Destroy objects that were created
  15. natsConnection_Destroy(conn);
  16. natsOptions_Destroy(opts);

{% endtab %} {% endtabs %}