This documentation is for version v1.3.0 of NNG, but the latest released version is v1.8.0. see the documentation for v1.8.0 for the most up-to-date information.


#include <nanomsg/nn.h>

int nn_setsockopt(int sock, int level, int option, const void *val, size_t sz);


The nn_setsockopt() function sets a socket option on socket sock, affecting the behavior of the socket. The option set is determined by the level and option. The value of the option is set by val, and sz, which are pointers to the actual value and the size of the value, respectively.

This function is provided for API compatibility with legacy libnanomsg. Consider using the relevant modern API instead.

The level determines whether the option is a generic socket option, or is transport-specific. The values possible for level are as follows:


Generic socket option


Transport specific option for IPC.


Transport specific option for TCP.


Transport specific option for WebSocket.

The following generic socket options are possible (all are of type int and thus size 4, unless otherwise indicated.)


Send buffer size in bytes.

In nng buffers are sized as a count of messages rather than bytes, and so an attempt to estimate a conversion based upon a predetermined message size of 1kB is made. The value supplied is rounded up to the nearest value divisible by 1024, and then divided by 1024 to convert to a message count. Applications that have unusual message sizes may wish to adjust the value used here accordingly.

Receive buffer size in bytes.

The same caveats for NN_SNDBUF apply here as well.

Send time-out in milliseconds. Send operations will fail with ETIMEDOUT if no message can be received after this many milliseconds have transpired since the operation was started. A value of -1 means that no timeout is applied.


Receive time-out in milliseconds. Receive operations will fail with ETIMEDOUT if no message can be received after this many milliseconds have transpired since the operation was started. A value of -1 means that no timeout is applied.


Maximum receive size in bytes. The socket will discard messages larger than this on receive. The default, 1MB, is intended to prevent denial-of-service attacks. The value -1 removes any limit.


Reconnect interval in milliseconds. After an outgoing connection is closed or fails, the socket will automatically attempt to reconnect after this many milliseconds. This is the starting value for the time, and is used in the first reconnection attempt after a successful connection is made. The default is 100.


Maximum reconnect interval in milliseconds. Subsequent reconnection attempts after a failed attempt are made at exponentially increasing intervals (back-off), but the interval is capped by this value. If this value is smaller than NN_RECONNECT_IVL, then no exponential back-off is performed, and each reconnect interval will be determined solely by NN_RECONNECT_IVL. The default is zero.


This option is ignored, and exists only for compatibility.

This option was unreliable in early releases of libnanomsg, and is unsupported in nng and recent libnanomsg releases. Applications needing assurance of message delivery should either include an explicit notification (automatic with the NN_REQ protocol) or allow sufficient time for the socket to drain before closing the socket or exiting.

This option is not implemented at this time.


This option is not implemented at this time.


This option is not implemented at this time.


This option is a string, and represents the socket name. It can be changed to help with identifying different sockets with their different application-specific purposes.


Maximum “hops” through proxies and devices a message may go through. This value, if positive, provides some protection against forwarding loops in device chains.

Not all protocols offer this protection, so care should still be used in configuring device forwarding.

The following option is available for NN_REQ sockets using the NN_REQ level:


Request retry interval in milliseconds. If an NN_REQ socket does not receive a reply to a request within this period of time, the socket will automatically resend the request. The default value is 60000 (one minute).

The following options are available for NN_SUB sockets using the NN_SUB level:


Subscription topic, for NN_SUB sockets. This sets a subscription topic. When a message from a publisher arrives, it is compared against all subscriptions. If the first sz bytes of the message are not identical to val, then the message is silently discarded.

To receive all messages, subscribe to an empty topic (sz equal to zero).

Removes a subscription topic that was earlier established.

The following option is available for NN_SURVEYOR sockets using the NN_SURVEYOR level:


Survey deadline in milliseconds for NN_SURVEYOR sockets. After sending a survey message, the socket will only accept responses from respondents for this long. Any responses arriving after this expires are silently discarded.

In addition, the following transport specific options are offered:


This NN_IPC option is not supported at this time.


This NN_IPC option is not supported at this time.


This NN_IPC option is not supported at this time.


This NN_TCP option is not supported at this time.


This NN_WS option is not supported, as nng only supports binary messages in this implementation.


This function returns zero on success, and -1 on failure.



The socket sock is not an open socket.


Insufficient memory is available.


The level and/or option is invalid.


The option, or the value passed, is invalid.


The library is shutting down.


The option cannot be changed.