NNG_SUB(7)
π NNG_SUB β the subscriber protocol, bestie edition π
okay so you're here for nng_sub, which lives in the NNG Reference Manual, and honestly? she's iconic. let's get into it π«Ά
β¨ SYNOPSIS β¨
#include <nng/nng.h>
#include <nng/protocol/pubsub0/sub.h>
πΈ DESCRIPTION πΈ
so sub is literally one half of the publisher/subscriber pattern, and it's giving dynamic duo. in this pattern, a publisher sends data, and that data gets broadcast to all the subscribers. the subscribing apps only ever see the data they actually subscribed to. slay π
sub is the subscriber side. pub is the publisher side. they're a matched set, no cap.
Note π in this implementation, the publisher delivers every message to every subscriber. the subscribers keep their own subscriptions and filter them locally. so likeβ¦ do not use this pattern trying to reduce bandwidth consumption. it is not that girl for that job π
the topics a subscriber subscribes to are just the first part of the message body. so applications need to construct their messages accordingly, fr fr.
π¦ Socket Operations π¦
the nng_sub0_open() functions create a subscriber socket. this socket can receive messages but it literally cannot send them. if you try to send? you get NNG_ENOTSUP. periodt. π
π Protocol Versions π
only version 0 of this protocol is supported. (and at the time of writing, no other versions of this protocol have even been defined. like, none. zero.)
π Protocol Options π
here are the protocol-specific options available. sit down, hydrate, we're listing all of them π
NNG_OPT_SUB_SUBSCRIBEβ this option registers a topic that the subscriber is interested in. it's write-only, and it takes an array of bytes of arbitrary size. every incoming message gets checked against the list of subscribed topics: if the body begins with the entire set of bytes in the topic, the message is accepted. if no topic matches, the message is justβ¦ discarded. bye π
Note β οΈ this option is deprecated. new applications should use thenng_sub0_socket_subscribeornng_sub0_ctx_subscribefunctions instead.
Note β οΈ this option is a byte array. so if you usenng_setopt_string(), theNULterminator byte gets included in the topic. if that isn't what you want, consider usingnng_setopt()and passingstrlen()of the topic as the topic size. iykyk.
Tip π‘ to receive all messages, you can use an empty topic (zero length). yes, empty. she contains multitudes β¨
NNG_OPT_SUB_UNSUBSCRIBEβ this option, also write-only, removes a topic from the subscription list. note that if the topic wasn't previously subscribed to withNNG_OPT_SUB_SUBSCRIBE, then you get anNNG_ENOENTerror. + NOTE: this option is deprecated. new applications should use thenng_sub0_socket_unsubscribeornng_sub0_ctx_unsubscribefunctions.NNG_OPT_SUB_PREFNEWβ(bool)this read/write option specifies the behavior of the subscriber when the queue is full. when true (and true is the default), the subscriber makes room in the queue by removing the oldest message. when false, the subscriber rejects messages if the message queue doesn't have room. choose your fighter π
π§ Protocol Headers π§
the sub protocol has no protocol-specific headers. that's it, that's the section. clean. minimal. main character energy. β
π SEE ALSO π
nng_sub_open(3)nng_sub_subscribe(3)nng_pub(7)nng(7)
and that's the whole page, bestie!! go forth and subscribe to things responsibly π¦π
β written up in the NNG Reference Manual, dated 2026-06-30. mwah π β¨
π¦ 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