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
Java
JavaScript
Python
Ruby
C
1
nc, err := nats.Connect("demo.nats.io")
2
if err != nil {
3
log.Fatal(err)
4
}
5
defer nc.Close()
6
7
mp := nc.MaxPayload()
8
log.Printf("Maximum payload is %v bytes", mp)
9
10
// Do something with the max payload
Copied!
1
Connection nc = Nats.connect("nats://demo.nats.io:4222");
2
3
long max = nc.getMaxPayload();
4
// Do something with the max payload
5
6
nc.close();
Copied!
1
t.log(`max payload for the server is ${nc.info.max_payload} bytes`);
Copied!
1
nc = NATS()
2
3
await nc.connect(servers=["nats://demo.nats.io:4222"])
4
5
print("Maximum payload is %d bytes" % nc.max_payload)
6
7
# Do something with the max payload.
Copied!
1
require 'nats/client'
2
3
NATS.start(max_outstanding_pings: 5) do |nc|
4
nc.on_reconnect do
5
puts "Got reconnected to #{nc.connected_server}"
6
end
7
8
nc.on_disconnect do |reason|
9
puts "Got disconnected! #{reason}"
10
end
11
12
# Do something with the max_payload
13
puts "Maximum Payload is #{nc.server_info[:max_payload]} bytes"
14
end
Copied!
1
natsConnection *conn = NULL;
2
natsStatus s = NATS_OK;
3
4
s = natsConnection_ConnectTo(&conn, NATS_DEFAULT_URL);
5
if (s == NATS_OK)
6
{
7
int64_t mp = natsConnection_GetMaxPayload(conn);
8
printf("Max payload: %d\n", (int) mp);
9
}
10
11
(...)
12
13
// Destroy objects that were created
14
natsConnection_Destroy(conn);
Copied!

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
Java
JavaScript
Python
Ruby
C
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
11
// Do something with the connection
Copied!
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
7
// Do something with the connection
8
9
nc.close();
Copied!
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
});
Copied!
1
nc = NATS()
2
3
await nc.connect(servers=["nats://demo.nats.io:4222"], pedantic=True)
4
5
# Do something with the connection.
Copied!
1
require 'nats/client'
2
3
NATS.start(pedantic: true) do |nc|
4
nc.on_reconnect do
5
puts "Got reconnected to #{nc.connected_server}"
6
end
7
8
nc.on_disconnect do |reason|
9
puts "Got disconnected! #{reason}"
10
end
11
12
nc.close
13
end
Copied!
1
natsConnection *conn = NULL;
2
natsOptions *opts = NULL;
3
natsStatus s = NATS_OK;
4
5
s = natsOptions_Create(&opts);
6
if (s == NATS_OK)
7
s = natsOptions_SetPedantic(opts, true);
8
if (s == NATS_OK)
9
s = natsConnection_Connect(&conn, opts);
10
11
(...)
12
13
// Destroy objects that were created
14
natsConnection_Destroy(conn);
15
natsOptions_Destroy(opts);
Copied!

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
Java
JavaScript
Python
Ruby
C
1
// This does not apply to the NATS Go Client
Copied!
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
7
// Do something with the connection
8
9
nc.close();
Copied!
1
// the max control line is determined automatically by the client
Copied!
1
# Asyncio NATS client does not allow custom control lines.
Copied!
1
# There is no need to customize this in the Ruby NATS client.
Copied!
1
// control line is not configurable on C NATS client.
Copied!

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
Java
JavaScript
Python
Ruby
C
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
11
// Do something with the connection
Copied!
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
7
// Do something with the connection
8
9
nc.close();
Copied!
1
const nc = await connect({
2
verbose: true,
3
servers: ["demo.nats.io:4222"],
4
});
Copied!
1
nc = NATS()
2
3
await nc.connect(servers=["nats://demo.nats.io:4222"], verbose=True)
4
5
# Do something with the connection.
Copied!
1
require 'nats/client'
2
3
NATS.start(verbose: true) do |nc|
4
nc.on_reconnect do
5
puts "Got reconnected to #{nc.connected_server}"
6
end
7
8
nc.on_disconnect do |reason|
9
puts "Got disconnected! #{reason}"
10
end
11
12
nc.close
13
end
Copied!
1
natsConnection *conn = NULL;
2
natsOptions *opts = NULL;
3
natsStatus s = NATS_OK;
4
5
s = natsOptions_Create(&opts);
6
if (s == NATS_OK)
7
s = natsOptions_SetVerbose(opts, true);
8
if (s == NATS_OK)
9
s = natsConnection_Connect(&conn, opts);
10
11
(...)
12
13
// Destroy objects that were created
14
natsConnection_Destroy(conn);
15
natsOptions_Destroy(opts);
Copied!
Last modified 10d ago