Nanomsg & NNG Wrappers for PureBasic
Scalability protocols in PureBasic — publish/subscribe, request/reply, pipeline, survey, bus, and pair. Two side-by-side packages: classic nanomsg and modern NNG.
nanomsg is no longer maintained. For new work, use the sibling package PureBasicNNG (NNG / nanomsg-next-gen). The APIs are not compatible.
Same repo — pick the stack that matches your needs
Classic libnanomsg binding. Ideal for existing nanomsg code and wire-compatible peers.
NanomsgSocket · NanomsgRuntimenn_socket(AF_SP, protocol) styleLibrary/x64/nanomsg.dllModern NNG (nanomsg-next-gen). Prefer this for new projects.
PubOpen, SubOpen, …#NNG_OKLibrary/x64/nng.dll| Nanomsg | NNG (PureBasicNNG) |
|---|---|
nn_socket(AF_SP, NN_PUB) | PubOpen() |
| Bind / Connect | Listen / Dial |
Rc < 0 failure | Rc <> #NNG_OK |
| nn_errno | return code / LastError() |
| SetsockoptString(…, NN_SUB_SUBSCRIBE) | Subscribe(sock, topic) |
NanomsgSocket, NanomsgRuntime — structured PureBasic modules with clear error paths.
SetsockoptString for topics, SetsockoptInt for timeouts — matching the C API cleanly.
NnPoll / NnPollFd, plus Allocmsg / Freemsg with #NN_MSG.
NnSendmsg / NnRecvmsg with NnIovec / NnMsghdr (see SendmsgIovec example).
NnDllOpen resolves all nn_* exports once into gNnFuncs — later calls reuse the cache.
Bundled nanomsg.dll under Library/x64. Examples require x64 PureBasic.
Samples under PureBasicNanoMsg/Example
Topic filter via NN_SUB_SUBSCRIBE
Request–reply RPC style
Pipeline with NnPoll
Surveyor / Respondent · deadline · ETIMEDOUT / EFSM
Bidirectional mesh with NnPoll
PairInproc smoke test · SendmsgIovec
Prefix the published message so it matches the subscriber filter
IncludeFile "../../Core/Enums.pbi"
IncludeFile "../../Core/NanomsgWrapper.pbi"
UseModule NanomsgWrapper
Define Socket.i = NanomsgSocket::Socket(#AF_SP, #NN_PUB)
Define Rc.i = NanomsgSocket::Bind(Socket, "tcp://*:1689")
While 1
Define topic.s = "quotes"
; Prefix must match NN_SUB_SUBSCRIBE on the subscriber
Define msg.s = topic + "#Bid:" + Random(9000, 1000)
Rc = NanomsgSocket::SendString(Socket, msg, Len(msg), 0)
Delay(500)
Wend
Define Socket.i = NanomsgSocket::Socket(#AF_SP, #NN_SUB)
NanomsgSocket::Connect(Socket, "tcp://localhost:1689")
NanomsgSocket::SetsockoptString(Socket, #NN_SUB, #NN_SUB_SUBSCRIBE, "quotes")
Define *buf = AllocateMemory(256)
Define n.i = NanomsgSocket::Recv(Socket, *buf, MemorySize(*buf), 0)
If n >= 0
PrintN(PeekS(*buf, n, #PB_Ascii)) ; use returned length
EndIf
FreeMemory(*buf)
Environment · runtime · build
PureBasicNanoMsg/Library/x64/nanomsg.dll
PureBasicNNG/Library/x64/nng.dll
Examples Chdir into the library folder before DllOpen.
Requires PureBasic Compiler. Tested under Windows 11. Include Core Enums + Wrapper modules for your chosen stack.
Clone the repo, pick nanomsg or NNG, and start with PUB/SUB or REQ/REP. For new projects, prefer PureBasicNNG.