Miscellaneous functionalities

    While the client can’t control the maximum payload size, clients may provide a way for applications to obtain the configured after the connection is made. This will allow the application to chunk or limit data as needed to pass through the server.

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

    {% endtab %}

    {% tab title=”Java” %}

    1. Connection nc = Nats.connect("nats://demo.nats.io:4222");
    2. long max = nc.getMaxPayload();
    3. // Do something with the max payload
    4. nc.close();

    {% endtab %}

    {% tab title=”JavaScript” %}

    1. t.log(`max payload for the server is ${nc.info.max_payload} bytes`);

    {% endtab %}

    {% tab title=”Python” %}

    1. nc = NATS()
    2. await nc.connect(servers=["nats://demo.nats.io:4222"])
    3. print("Maximum payload is %d bytes" % nc.max_payload)
    4. # Do something with the max payload.

    {% endtab %}

    {% tab title=”Ruby” %}

    1. require 'nats/client'
    2. NATS.start(max_outstanding_pings: 5) do |nc|
    3. nc.on_reconnect do
    4. puts "Got reconnected to #{nc.connected_server}"
    5. end
    6. nc.on_disconnect do |reason|
    7. puts "Got disconnected! #{reason}"
    8. end
    9. # Do something with the max_payload
    10. puts "Maximum Payload is #{nc.server_info[:max_payload]} bytes"
    11. end

    {% endtab %}

    {% tab title=”C” %}

    1. natsConnection *conn = NULL;
    2. natsStatus s = NATS_OK;
    3. s = natsConnection_ConnectTo(&conn, NATS_DEFAULT_URL);
    4. if (s == NATS_OK)
    5. {
    6. int64_t mp = natsConnection_GetMaxPayload(conn);
    7. printf("Max payload: %d\n", (int) mp);
    8. }
    9. (...)
    10. // Destroy objects that were created
    11. natsConnection_Destroy(conn);

    {% endtab %} {% endtabs %}

    The NATS server provides a pedantic mode that performs extra checks on the protocol.

    One example of such a check is if a subject used for publishing contains a character. The server will not use it as wildcard and therefore omits this check.

    By default, this setting is off but you can turn it on to test your application:

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

    1. opts := nats.GetDefaultOptions()
    2. opts.Url = "demo.nats.io"
    3. // Turn on Pedantic
    4. opts.Pedantic = true
    5. nc, err := opts.Connect()
    6. if err != nil {
    7. log.Fatal(err)
    8. }
    9. defer nc.Close()
    10. // Do something with the connection

    {% endtab %}

    1. Options options = new Options.Builder().
    2. pedantic(). // Turn on pedantic
    3. build();
    4. Connection nc = Nats.connect(options);
    5. // Do something with the connection
    6. nc.close();

    {% endtab %}

    {% tab title=”JavaScript” %}

    {% endtab %}

    {% tab title=”Python” %}

    1. nc = NATS()
    2. await nc.connect(servers=["nats://demo.nats.io:4222"], pedantic=True)
    3. # Do something with the connection.

    {% endtab %}

    {% tab title=”Ruby” %}

    1. require 'nats/client'
    2. NATS.start(pedantic: true) do |nc|
    3. nc.on_reconnect do
    4. puts "Got reconnected to #{nc.connected_server}"
    5. end
    6. nc.on_disconnect do |reason|
    7. puts "Got disconnected! #{reason}"
    8. end
    9. nc.close
    10. 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. s = natsOptions_SetPedantic(opts, true);
    7. if (s == NATS_OK)
    8. s = natsConnection_Connect(&conn, opts);
    9. (...)
    10. // Destroy objects that were created
    11. natsConnection_Destroy(conn);
    12. natsOptions_Destroy(opts);

    {% endtab %} {% endtabs %}

    The protocol between the client and the server is fairly simple and relies on a control line and sometimes a body. The control line contains the operations being sent, like PING or PONG, followed by a carriage return and line feed, CRLF or “\r\n”. The server has a max_control_line option that can limit the maximum size of a control line. For PING and PONG this doesn’t come into play, but for messages that contain subject names and possibly queue group names, the control line length can be important as it effectively limits the possibly combined length. Some clients will try to limit the control line size internally to prevent an error from the server. These clients may or may not allow you to set the size being used, but if they do, the size should be set to match the server configuration.

    For example, to set the maximum control line size to 2k:

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

    1. // This does not apply to the NATS Go Client

    {% endtab %}

    {% tab title=”Java” %}

    1. Options options = new Options.Builder().
    2. server("nats://demo.nats.io:4222").
    3. maxControlLine(2 * 1024). // Set the max control line to 2k
    4. build();
    5. Connection nc = Nats.connect(options);
    6. // Do something with the connection
    7. nc.close();

    {% endtab %}

    {% tab title=”JavaScript” %}

    1. // the max control line is determined automatically by the client

    {% endtab %}

    1. # Asyncio NATS client does not allow custom control lines.

    {% endtab %}

    {% tab title=”Ruby” %}

    {% endtab %}

    {% tab title=”C” %}

      {% endtab %} {% endtabs %}

      Clients can request verbose mode from NATS server. When requested by a client, the server will reply to every message from that client with either a +OK or an error -ERR. However, the client will not block and wait for a response. Errors will be sent without verbose mode as well and client libraries handle them as documented.

      This functionality is only used for debugging the client library or the nats-server themselves. By default the server sets it to on, but every client turns it off.

      To turn on verbose mode:

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

      1. opts.Url = "demo.nats.io"
      2. // Turn on Verbose
      3. opts.Verbose = true
      4. nc, err := opts.Connect()
      5. if err != nil {
      6. log.Fatal(err)
      7. }
      8. defer nc.Close()
      9. // Do something with the connection

      {% endtab %}

      {% tab title=”Java” %}

      1. Options options = new Options.Builder().
      2. server("nats://demo.nats.io:4222").
      3. verbose(). // Turn on verbose
      4. build();
      5. Connection nc = Nats.connect(options);
      6. // Do something with the connection
      7. nc.close();

      {% endtab %}

      {% tab title=”JavaScript” %}

      1. const nc = await connect({
      2. verbose: true,
      3. servers: ["demo.nats.io:4222"],
      4. });

      {% endtab %}

      {% tab title=”Python” %}

      1. nc = NATS()
      2. await nc.connect(servers=["nats://demo.nats.io:4222"], verbose=True)
      3. # Do something with the connection.

      {% endtab %}

      {% tab title=”Ruby” %}

      1. require 'nats/client'
      2. NATS.start(verbose: true) do |nc|
      3. nc.on_reconnect do
      4. puts "Got reconnected to #{nc.connected_server}"
      5. end
      6. nc.on_disconnect do |reason|
      7. puts "Got disconnected! #{reason}"
      8. end
      9. nc.close
      10. end

      {% endtab %}

      1. natsConnection *conn = NULL;
      2. natsOptions *opts = NULL;
      3. natsStatus s = NATS_OK;
      4. s = natsOptions_Create(&opts);
      5. if (s == NATS_OK)
      6. s = natsOptions_SetVerbose(opts, true);
      7. if (s == NATS_OK)
      8. s = natsConnection_Connect(&conn, opts);
      9. (...)
      10. // Destroy objects that were created
      11. natsOptions_Destroy(opts);

      {% endtab %} {% endtabs %}