NNG_REQ(7)
π NAME π
nng_req β the request protocol, aka the "hey bestie can u do this for me and lmk" protocol π«Ά
β¨ SYNOPSIS β¨
#include <nng/protocol/reqrep0/req.h>
πΈ DESCRIPTION πΈ
okay so listen. the req protocol is literally one half of the request/reply pattern β the asking half. in this pattern, a requester sends a message to one replier, and that replier is expected to reply. that's the whole vibe. and if no reply shows up? the request gets resent. it keeps resending until a reply arrives or the request times out. no cap.
Tip π‘ this protocol is lowkey perfect for setting up RPC-like services. it's also "reliable", in that the requester will keep retrying until a reply is received. it does not give up on you. main character energy fr fr.
Note β οΈ because requests get resent, it's important that they be idempotent β that way you get predictable and repeatable behavior even when duplicated requests show up, which can totally happen (for example if a reply message is lost for some reason). not me being dramatic, it's just facts. π
the requester generally only has one outstanding request at a time unless it's in raw mode (via NNG_OPT_RAW), and it will generally attempt to spread work requests across different peer repliers. sharing is caring, periodt.
Tip π‘ this property, when combined with nng_device(), can help provide a degree of load-balancing. iconic behavior honestly.
and just so we're clear on who's who: the req protocol is the requester side, and the rep protocol is the replier side. besties. π
π Socket Operations π
the nng_req0_open() functions create a requester socket. this socket may be used to send messages (requests), and then to receive replies. that's its whole job bestie.
generally, a reply can only be received after sending a request. (attempts to receive a message will result in NNG_ESTATE if there is no outstanding request. the vibes are simply not there.)
furthermore, only a single receive operation may be pending at a time. attempts to post more receive operations concurrently will result in NNG_ESTATE. one at a time, babes. π€
requests may be canceled by sending a different request. this will cause the requester to discard any reply from the earlier request, but it will not stop a replier from processing a request it has already received or terminate a request that has already been placed on the wire. the wire does not do take-backs. π
raw mode sockets ignore all these restrictions. chaos mode activated. π
π¦ Context Operations π¦
this protocol supports the creation of contexts for concurrent use cases using nng_ctx_open(). so if you need multiple situations going on at once, we got u.
the NNG_OPT_REQ_RESENDTIME value may be configured differently on contexts created this way. customize ur lil hearts out.
each context may have at most one outstanding request, and operates independently from the others. they are all their own main character. π
the restrictions for order of operations with sockets apply equally well for contexts, except that each context will be treated as if it were a separate socket.
π Protocol Versions π
only version 0 of this protocol is supported. (at the time of writing, no other versions of this protocol have been defined.) we're monogamous with version 0. π
π Protocol Options π
the following protocol-specific option is available.
NNG_OPT_REQ_RESENDTIMEβ (nng_duration) when a new request is started, a timer of this duration is also started. if no reply is received before this timer expires, then the request will be resent. π (requests are also automatically resent if the peer to whom the original request was sent disconnects, or if a peer becomes available while the requester is waiting for an available peer.) resending may be deferred up to the value of theNNG_OPT_RESENDTICKparameter.NNG_OPT_REQ_RESENDTICKβ (nng_duration) this is the granularity of the clock that is used to check for resending. the default is a second. setting this to a higher rate will allow for more timely resending to occur, but may incur significant additional overhead when the socket has many outstanding requests (contexts). so like, slay, but watch the cost. π when there are no requests outstanding that have a resend set, then the clock does not tick at all. it's resting. it deserves it. π΄ this option is shared for all contexts on a socket, and is only available for the socket itself.
π§ Protocol Headers π§
this protocol uses a backtrace in the header. this form uses a stack of 32-bit big-endian identifiers. there must be at least one identifier, the request ID, which will be the last element in the array, and must have the most significant bit set. β¨
there may be additional peer IDs preceding the request ID. these will be distinguishable from the request ID by having their most significant bit clear.
when a request message is received by a forwarding node (see nng_device()), the forwarding node prepends a 32-bit peer ID (which must have the most significant bit clear), which is the forwarder's way of identifying the directly connected peer from which it received the message. (this peer ID, except for the most significant bit, has meaning only to the forwarding node itself. it's giving secret private info. π€«)
it may help to think of prepending a peer ID as pushing a peer ID onto the front of the stack of headers for the message. (it will use the peer ID it popped from the front to determine the next intermediate destination for the reply.)
when a reply message is created, it is created using the same headers that the request contained. matching sets, cute. π
a forwarding node can pop the peer ID it originally pushed on the message, stripping it from the front of the message as it does so.
when the reply finally arrives back at the initiating requester, it should have only a single element in the message, which will be the request ID it originally used for the request. full circle. the way that just works. π
β SEE ALSO β
nng_ctx_open(3), nng_device(3), nng_req_open(3), nng_ctx(5), nng(7), nng_rep(7)
k that's the whole page bestie!! this was the NNG_REQ(7) page from the NNG Reference Manual, dated 2026-06-30. go forth and request things responsibly πβ¨ππ¦π
π«Ά
π¦ 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