LIBNNG(3)
π LIBNNG(3) β nanomsg next generation library π
libnng β the manual page, bestie edition. Yes it's nng, the nanomsg next generation library, and yes she is that girl. π
β¨ NAME β¨
libnng β nanomsg next generation library π¦
β¨ SYNOPSIS β¨
cc [flags] files -lnng [libraries]
like. that's it. that's the whole build line. you compile your stuff, you link -lnng, you go live your life. π
β¨ DESCRIPTION β¨
okay so picture this: distributed applications have a LOT of communication problems and they're all kind of the same problems over and over again. NNG is a common messaging framework built specifically to solve those common communication problems. no cap. π«Ά
and the way you talk to it is through a C language API. that's the whole vibe.
πΈ Common Functions
these are the little everyday helpers that live in libnng. the utility squad. the ones you call constantly and never think about:
nng_allocβ allocate memory π§nng_freeβ free memory (yes, the sequel)nng_strdupβ duplicate stringnng_strerrorβ return an error descriptionnng_strfreeβ free stringnng_versionβ report library version π
ngl nng_strerror is lowkey doing the most for you every single time something explodes. thank her.
π Socket Functions
and these ones? these operate on sockets. this is where the main character energy lives:
nng_closeβ close socketnng_dialβ create and start dialernng_getoptβ get socket optionnng_listenβ create and start listenernng_recvβ receive datanng_sendβ send data (the iconique counterpart)nng_setoptβ set socket option (DEPRECATED)nng_socket_getβ get socket optionnng_socket_idβ get numeric socket identifiernng_socket_rawβ is the socket rawnng_socket_proto_idβ get the socket protocol identifiernng_socket_proto_nameβ get the socket protocol namenng_socket_setβ set socket optionnng_sub_subscribeβ manage subscriptions
π¨ bestie listen. nng_setopt is marked DEPRECATED. she still exists, she still works, but she is NOT the moment anymore. do not build your new thing on her. periodt. π
and that's part 1, we are literally just getting started β more pages are coming so don't go anywhere bestie π
π Connection Management π
okay so these functions are used with either listeners, or dialers. Listeners accept incoming connection requests, and dialers make them. That's the whole dynamic, bestie β listeners are the ones saying "come thru," dialers are the ones making the first move. π
nng_dialβ create and start dialernng_dialer_closeβ close dialernng_dialer_createβ create dialernng_dialer_getβ get dialer optionnng_dialer_getoptβ get dialer option (DEPRECATED)nng_dialer_idβ get numeric dialer identifiernng_dialer_setβ set dialer optionnng_dialer_setoptβ set dialer option (DEPRECATED)nng_dialer_startβ start dialernng_listenβ create and start listenernng_listener_closeβ close listenernng_listener_createβ create listenernng_listener_getβ get listener optionnng_listener_getoptβ get listener option (DEPRECATED)nng_listener_idβ get numeric listener identifiernng_listener_setβ set listener optionnng_listener_setoptβ set listener option (DEPRECATED)nng_listener_startβ start listenernng_pipe_closeβ close pipenng_pipe_dialerβ return dialer that created pipenng_pipe_getβ get pipe optionnng_pipe_getoptβ get pipe option (DEPRECATED)nng_pipe_idβ get numeric pipe identifiernng_pipe_listenerβ return listener that created pipenng_pipe_notifyβ register pipe notification callbacknng_pipe_socketβ return owning socket for pipe
π¦ Message Handling Functions π¦
If your app wants the richest part of libnng, you're gonna want the message API β a message structure gets passed between functions. This API provides the most powerful support for zero-copy. π
β¨
Okay so here's the whole roster of message functions β the header is one thing, the body is another, and almost everyone only ever touches the body. The body carries your user payload; the header carries protocol-specific header info. Cute, right?
nng_msg_allocβ allocate a messagenng_msg_appendβ append to message bodynng_msg_bodyβ return message bodynng_msg_capacityβ return capacity allocated for message bodynng_msg_chopβ remove data from end of message bodynng_msg_clearβ clear message bodynng_msg_dupβ duplicate a messagenng_msg_freeβ free a messagenng_msg_get_pipeβ get pipe for messagenng_msg_insertβ prepend to message bodynng_msg_lenβ return the message body lengthnng_msg_reallocβ reallocate a messagenng_msg_reserveβ reserve storage for message bodynng_msg_set_pipeβ set pipe for messagenng_msg_trimβ remove data from start of message bodynng_recvmsgβ receive a messagenng_sendmsgβ send a message
π¦ Message Header Handling π¦
Tip β bestie, most apps will never need these. Message headers only carry protocol-specific content, periodt. But if you're living that raw mode life, you may need to poke at the header of messages. iykyk.
nng_msg_headerβ return message headernng_msg_header_appendβ append to message headernng_msg_header_chopβ remove data from end of message headernng_msg_header_clearβ clear message headernng_msg_header_insertβ prepend to message headernng_msg_header_lenβ return the message header lengthnng_msg_header_trimβ remove data from start of message header
β¨ Asynchronous Operations β¨
So most apps interact with NNG synchronously, meaning functions like nng_send will lowkey hold the calling thread hostage until the operation has completed. Like, block and wait, bestie. π
Note β not me telling you that synchronous operations which send messages may return before the message has actually been received, or even transmitted. Fr fr: these functions return as soon as the message was successfully queued for delivery. That's it. That's the whole story. π
Asynchronous operations? Completely different vibe. The calling thread kicks the operation off, but control returns to the calling thread immediately. Then, when the operation is subsequently completed β successful or not, no judgment, both are valid β a user-supplied function gets executed. Callback era. π
A context structure, an nng_aio, is allocated and associated with each asynchronous operation. And here's the rule: only a single asynchronous operation may be associated with an nng_aio at any time. One. Singular. Don't be messy about it. π
β¨ the asynchronous model girlies β¨
okay so these are all the functions you'll be living in when you're doing stuff asynchronously β like, nothing waits around blocking your thread, everything's giving "i'll text you back later" energy π
nng_aio_abortβ abort asynchronous I/O operationnng_aio_allocβ allocate asynchronous I/O handlenng_aio_beginβ begin asynchronous I/O operationnng_aio_busyβ test if asynchronous I/O is busynng_aio_cancelβ cancel asynchronous I/O operationnng_aio_countβ return number of bytes transferrednng_aio_deferβ defer asynchronous I/O operationnng_aio_finishβ finish asynchronous I/O operationnng_aio_freeβ free asynchronous I/O handlenng_aio_get_inputβ return input parameternng_aio_get_msgβ get message from an asynchronous receivenng_aio_get_outputβ return output resultnng_aio_reapβ reap asynchronous I/O handlenng_aio_resultβ return result of asynchronous operationnng_aio_set_inputβ set input parameternng_aio_set_iovβ set scatter/gather vectornng_aio_set_msgβ set message for an asynchronous sendnng_aio_set_outputβ set output resultnng_aio_set_timeoutβ set asynchronous I/O timeoutnng_aio_stopβ stop asynchronous I/O operationnng_aio_waitβ wait for asynchronous I/O operationnng_recv_aioβ receive message asynchronouslynng_send_aioβ send message asynchronouslynng_sleep_aioβ sleep asynchronously
π Protocols π
and these bad boys? they're how you build yourself a socket that's committed to one specific protocol. pick your fighter, bestie β¨
nng_bus_openβ open a bus socketnng_pair_openβ open a pair socketnng_pub_openβ open a pub socketnng_pull_openβ open a pull socketnng_push_openβ open a push socketnng_rep_openβ open a rep socketnng_req_openβ open a req socketnng_respondent_openβ open a respondent socketnng_sub_openβ open a sub socketnng_surveyor_openβ open a surveyor socket
π¦ that's the whole roster, no cap π¦
π« Transports π«
okay so these functions register a transport for use, right? but bestie. plot twist β this is no longer necessary, and applications should not call these functions anymore. that's not me being dramatic, that's the manpage. periodt. π ββοΈ
so like, they're still documented (we don't gatekeep the lore) but every single one of them is DEPRECATED. read them for the history lesson, not for your new project. π
nng_inproc_registerβ register inproc transport (DEPRECATED)nng_ipc_registerβ register IPC transport (DEPRECATED)nng_tcp_registerβ register TCP transport (DEPRECATED)nng_tls_registerβ register TLS transport (DEPRECATED)nng_ws_registerβ register WebSocket transport (DEPRECATED)nng_wss_registerβ register WebSocket Secure transport (DEPRECATED)nng_zt_registerβ register ZeroTier transport (DEPRECATED)
fr fr whisper: if you're out here calling these in 2024+ code, we need to talk. they're deprecated. the library handles this now.
π§ π Protocol Contexts π π§
now THIS is the good stuff. these functions let you pull the protocol processing out of a socket object and into a whole separate context. why do we care? because it means you can spin up multiple contexts on a single socket for concurrent applications. one socket, many main characters. that's range. π
nng_ctx_closeβ close contextnng_ctx_getβ get context optionnng_ctx_getoptβ get context option (DEPRECATED)nng_ctx_idβ get numeric context identifiernng_ctx_openβ create contextnng_ctx_recvβ receive message using context asynchronouslynng_ctx_recvmsgβ receive a message using contextnng_ctx_sendβ send message using context asynchronouslynng_ctx_sendmsgβ send a message using contextnng_ctx_setβ set context optionnng_ctx_setoptβ set context option (DEPRECATED)
heads up bestie: see hownng_ctx_getoptandnng_ctx_setoptare tagged DEPRECATED? yeah. they're the old way.nng_ctx_getandnng_ctx_setare the ones you actually want. the vibe check said "evolve." π¦
π Devices, Relays π
last lil family in this part, and honestly? iconic behaviour. this one is a whole function family (okay it's one function, but the concept is a family) for building forwarders or relayers β the lil guys that take messages from one socket and route them on over to another socket. message traffic controller energy. π¦β¨
nng_deviceβ message forwarding device
so that's your relayer. route it, forward it, slay it. π«Ά
π Statistics π
okay bestie, so these functions are the whole stats vibe β they give you access to statistics that can be used to observe program behaviors and as an aid in troubleshooting. Itβs giving βI know what my code is doingβ energy, fr fr. πβ¨
nng_stat_boolβ get statistic Boolean valuenng_stat_childβ get child statisticnng_stat_nameβ get statistic descriptionnng_stat_findβ find statistic by namenng_stat_find_dialerβ find dialer statisticsnng_stat_find_listenerβ find listener statisticsnng_stat_find_socketβ find socket statisticsnng_stat_nameβ get statistic namenng_stat_nextβ get next statisticnng_stat_stringβ get statistic string valuenng_stat_timestampβ get statistic timestampnng_stat_typeβ get statistic typenng_stat_unitβ get statistic unitnng_stat_valueβ get statistic numeric valuenng_stats_freeβ free statisticsnng_stats_getβ get statistics
π URL Object π
Common functionality is supplied for parsing and handling universal resource locators (URLS). Yes, URLS with the S, we are not taking questions at this time π
nng_url_cloneβ clone URL structurenng_url_freeβ free URL structurenng_url_parseβ create URL structure from string
π Logging Support π
Common functionality for message logging. Because sometimes you need receipts, bestie. π§Ύπ
nng_logβ log a messagenng_log_set_facilityβ set log facilitynng_log_set_levelβ set log levelnng_log_set_loggerβ set logging handler
β¨ Supplemental API β¨
Okay bestie, so these supplemental functions? They're not like intrinsic to building network applications with NNG β they're not the main character of the networking story π . But they're here anyway, as a convenience, to help you out when you're trying to make portable applications. Think of them as the little besties that carry your bags while you do the big networking stuff π«Ά
Here's the full squad, no gatekeeping:
nng_clockβ get time β°nng_cv_allocβ allocate condition variablenng_cv_freeβ free condition variablenng_cv_untilβ wait for condition or timeout β³nng_cv_waitβ wait for conditionnng_cv_wakeβ wake all waitersnng_cv_wake1β wake one waiternng_id_mapβ identifier based mapping table πΊοΈnng_msleepβ sleep for milliseconds π΄nng_mtx_allocβ allocate mutexnng_mtx_freeβ free mutexnng_mtx_lockβ lock mutex πnng_mtx_unlockβ unlock mutex πnng_opts_parseβ parse command line optionsnng_randomβ get random number π²nng_socket_pairβ create connected pair of BSD socketsnng_thread_createβ create thread π§΅nng_thread_destroyβ reap threadnng_thread_set_nameβ set thread name π·οΈ
π Byte Streams π
And these functions? They're the ones you can use with byte streams π§. Fr fr, they're considered low-level β like, you reach for them when the higher level functions that use Scalability Protocols are just not it for whatever you're doing π. Not inappropriate in a bad way, just inappropriate for the vibe.
So like β byte streams, bestie, are what you get when you want a raw connection: an actual TCP connection, a named pipe, that kind of thing π«Ά Each one is wrapped up in an nng_stream object, and you never just β¨materializeβ¨ one out of thin air. They come from either nng_stream_dialer or nng_stream_listener objects. The dialer slides out and makes the connection happen; the listener sits there all cute until somebody shows up. π
Here's the whole squad of byte stream functions, every single one, no skips:
nng_stream_closeβ close byte stream πnng_stream_dialer_allocβ allocate byte stream dialer β¨nng_stream_dialer_closeβ close byte stream dialernng_stream_dialer_dialβ initiate outgoing byte stream π₯nng_stream_dialer_freeβ free byte stream dialernng_stream_dialer_getβ get option from byte stream dialernng_stream_dialer_setβ set option on byte stream dialernng_stream_freeβ free byte streamnng_stream_getβ get option from byte streamnng_stream_listener_acceptβ accept incoming byte stream π«Άnng_stream_listener_allocβ allocate byte stream listenernng_stream_listener_closeβ close byte stream listenernng_stream_listener_freeβ free byte stream listenernng_stream_listener_getβ get option from byte stream listenernng_stream_listener_listenβ bind byte stream listener to address πnng_stream_listener_setβ set option on byte stream listenernng_stream_recvβ receive from byte stream πnng_stream_sendβ send to byte stream πnng_stream_setβ set option on byte stream
π¦ HTTP Support π¦
Okay so this one depends on how your library was built, and that's the tea: the library may be configured with support for HTTP π β and the rule is that this will be the case if WebSocket support is configured as well. So no WebSocket, no HTTP party. Periodt.
When you do have it, though? You get access to functionality to support the creation of HTTP servers and clients, and, if TLS support is present, HTTP/S ones too. both flavors, iconically π
β¨ Common HTTP Functions β¨
Okay bestie, these are the little guys you'll be texting all day long β the functions that handle HTTP requests, responses, and connections. Every single one, in order, no skips, let's go π«Ά
nng_http_conn_closeβ closes down that HTTP connection, bye bye πnng_http_conn_readβ reads from an HTTP connection πnng_http_conn_read_allβ reads everything from an HTTP connection, no crumbs left πͺnng_http_conn_read_reqβ reads an HTTP request π¨nng_http_conn_read_resβ reads an HTTP response π¬nng_http_conn_writeβ writes to an HTTP connection βοΈnng_http_conn_write_allβ writes all of it to an HTTP connection, commitment issues = none πnng_http_conn_write_reqβ writes an HTTP request π€nng_http_conn_write_resβ writes an HTTP response π₯nng_http_req_add_headerβ adds an HTTP request header βnng_http_req_allocβ allocates an HTTP request structure ποΈnng_http_req_copy_dataβ copies the HTTP request body πnng_http_req_del_headerβ deletes an HTTP request header ποΈnng_http_req_freeβ frees that HTTP request structure, so it stops living rent free πnng_http_req_get_dataβ gets the HTTP request body πnng_http_req_get_headerβ returns an HTTP request header πnng_http_req_get_methodβ returns the HTTP request method (the verb energy fr) π·οΈnng_http_req_get_uriβ returns the HTTP request URI πnng_http_req_get_versionβ returns the HTTP request protocol version π’
nng_http_req_resetβ resets the HTTP request structure, starting fresh, main character rebirth arc π¦nng_http_req_set_dataβ sets the HTTP request body π©Ήnng_http_req_set_headerβ sets an HTTP request header πnng_http_req_set_methodβ sets the HTTP request method βοΈnng_http_req_set_uriβ sets the HTTP request URI π§nng_http_req_set_versionβ sets the HTTP request protocol version π§nng_http_res_add_headerβ adds an HTTP response header βnng_http_res_allocβ allocates an HTTP response structure ποΈnng_http_res_alloc_errorβ allocates an HTTP error response, for when things are giving chaos πnng_http_res_copy_dataβ copies the HTTP response body πnng_http_res_del_headerβ deletes an HTTP response header ποΈnng_http_res_freeβ frees the HTTP response structure πnng_http_res_get_dataβ gets the HTTP response body πnng_http_res_get_headerβ returns an HTTP response header πnng_http_res_get_reasonβ returns the HTTP response reason π¬
β¦and the rest of the response girlies, finishing up the roster π
nng_http_res_get_statusβ hands you back the HTTP response status β¨nng_http_res_get_versionβ hands you back the HTTP response protocol version πΈnng_http_res_resetβ wipes the HTTP response structure back to a clean slate π§Όnng_http_res_set_dataβ sets the HTTP response body πnng_http_res_set_headerβ sets an HTTP response header π¨nng_http_res_set_reasonβ sets the HTTP response reason π¬nng_http_res_set_statusβ sets the HTTP response status π’nng_http_res_set_versionβ sets the HTTP response protocol version π·οΈ
π HTTP Client Functions π
These functions are intended for use with HTTP client applications. Like, if you're the one making the requests, this is your section bestie π«Ά
nng_http_client_allocβ allocates an HTTP client π±nng_http_client_connectβ establishes the HTTP client connection πnng_http_client_freeβ frees the HTTP client π (the good kind of free, don't panic)nng_http_client_get_tlsβ gets the HTTP client TLS configuration πnng_http_client_set_tlsβ sets the HTTP client TLS configuration πnng_http_client_transactβ performs one HTTP transaction β¨nng_http_conn_transactβ performs one HTTP transaction on a connection π
HTTP Server Functions πβ¨
These ones are for when you're building an HTTP server app, like actually serving requests to people. Bestie, this is where the main character energy lives fr fr π
nng_http_handler_allocβ allocate HTTP server handlernng_http_handler_collect_bodyβ set HTTP handler to collect request bodynng_http_handler_freeβ free HTTP server handlernng_http_handler_get_dataβ return extra data for HTTP handlernng_http_handler_set_dataβ set extra data for HTTP handlernng_http_handler_set_hostβ set host for HTTP handlernng_http_handler_set_methodβ set HTTP handler methodnng_http_handler_set_treeβ set HTTP handler to match treesnng_http_hijackβ hijack HTTP server connectionnng_http_server_add_handlerβ add HTTP server handlernng_http_server_del_handlerβ delete HTTP server handlernng_http_server_get_addrβ get HTTP server addressnng_http_server_get_tlsβ get HTTP server TLS configurationnng_http_server_holdβ get and hold HTTP server instancenng_http_server_releaseβ release HTTP server instancenng_http_server_set_error_fileβ set custom HTTP error filenng_http_server_set_error_pageβ set custom HTTP error pagenng_http_server_set_tlsβ set HTTP server TLS configurationnng_http_server_res_errorβ use HTTP server error pagenng_http_server_startβ start HTTP servernng_http_server_stopβ stop HTTP server
TLS Configuration Objects ππ¦
The following functions are used to manipulate transport layer security (TLS) configuration objects. The way that most of these functions are so niche that even actual TLS applications won't touch themβ¦ yeah, that's the vibe π Most of y'all will never call these, and that's totally okay bestie, no cap.
Note
These functions will only be present if the library has been built with TLS support. like, if your build didn't have TLS turned on, these justβ¦ don't exist bestie π don't come for me, that's just how it is.
nng_tls_config_allocβ allocate a TLS configuration β¨nng_tls_config_auth_modeβ set the authentication mode πnng_tls_config_ca_chainβ set the certificate authority chain πnng_tls_config_ca_fileβ load the certificate authority from a file πnng_tls_config_cert_key_fileβ load your own certificate and key from a file ποΈnng_tls_config_pskβ set the pre-shared key and identity π€nng_tls_config_own_certβ set your own certificate and key πnng_tls_config_freeβ free the TLS configuration π§Ήnng_tls_config_server_nameβ set the remote server name π°οΈ
π SEE ALSO π
okay so if you wanna keep vibing through the docs, these are the ones to tap next:
and that's the whole page, babe π«Ά
this was LIBNNG(3), dated 2026-06-30 π tagged and dated, archived, iconic, periodt. go forth and configure your TLS like the main character you are β¨π¦π
π¦ where this page comes from
- ubuntu stonking: libnng-dev 1.12.0-1 Β· MIT OR Apache-2.0
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms