Miscellaneous functionalities

This section contains miscellaneous functionalities and options for connect.

Get the Maximum Payload Size

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

Go

  1. nc, err := nats.Connect("demo.nats.io")
  2. if err != nil {
  3. log.Fatal(err)
  4. }
  5. defer nc.Close()
  6. mp := nc.MaxPayload()
  7. log.Printf("Maximum payload is %v bytes", mp)
  8. // Do something with the max payload

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();

JavaScript

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

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.

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

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);

{% endtabs %}

Turn On Pedantic Mode

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 wildcard 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:

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

Java

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

JavaScript

  1. // the pedantic option is useful for developing nats clients.
  2. // the javascript clients also provide `debug` which will
  3. // print to the console all the protocol interactions
  4. // with the server
  5. const nc = await connect({
  6. pedantic: true,
  7. servers: ["demo.nats.io:4222"],
  8. debug: true,
  9. });

Python

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

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

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);

{% endtabs %}

Set the Maximum Control Line Size

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.

It is not recommended to set this to a value that is higher than the one of other clients or the nats-server.

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

Go

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

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();

JavaScript

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

Python

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

Ruby

  1. # There is no need to customize this in the Ruby NATS client.

C

  1. // control line is not configurable on C NATS client.

{% endtabs %}

Turn On/Off Verbose Mode

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:

Go

  1. opts := nats.GetDefaultOptions()
  2. opts.Url = "demo.nats.io"
  3. // Turn on Verbose
  4. opts.Verbose = 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

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();

JavaScript

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

Python

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

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

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_SetVerbose(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);

{% endtabs %}