PureBasic · nanomsg · NNG

PureBasicNanoMsg

Nanomsg & NNG Wrappers for PureBasic

nanomsg NNG (next-gen) PureBasic 6.0+ x64 Module API MIT License

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.

Two Packages, Side by Side

Same repo — pick the stack that matches your needs

PureBasicNanoMsg

Classic libnanomsg binding. Ideal for existing nanomsg code and wire-compatible peers.

  • NanomsgSocket · NanomsgRuntime
  • nn_socket(AF_SP, protocol) style
  • Bind / Connect · nn_errno
  • Bundled: Library/x64/nanomsg.dll
Legacy · Sustaining
Recommended

PureBasicNNG

Modern NNG (nanomsg-next-gen). Prefer this for new projects.

  • Protocol helpers: PubOpen, SubOpen, …
  • Listen / Dial · success = #NNG_OK
  • Errors returned directly (no nn_errno)
  • Bundled: Library/x64/nng.dll
Browse PureBasicNNG →

Nanomsg → NNG quick map

Nanomsg NNG (PureBasicNNG)
nn_socket(AF_SP, NN_PUB)PubOpen()
Bind / ConnectListen / Dial
Rc < 0 failureRc <> #NNG_OK
nn_errnoreturn code / LastError()
SetsockoptString(…, NN_SUB_SUBSCRIBE)Subscribe(sock, topic)

nanomsg Binding Features

Module API

NanomsgSocket, NanomsgRuntime — structured PureBasic modules with clear error paths.

Typed Sockopts

SetsockoptString for topics, SetsockoptInt for timeouts — matching the C API cleanly.

Poll & Zero-Copy

NnPoll / NnPollFd, plus Allocmsg / Freemsg with #NN_MSG.

Scatter / Gather

NnSendmsg / NnRecvmsg with NnIovec / NnMsghdr (see SendmsgIovec example).

Cached Exports

NnDllOpen resolves all nn_* exports once into gNnFuncs — later calls reuse the cache.

x64 Runtime

Bundled nanomsg.dll under Library/x64. Examples require x64 PureBasic.

Example Patterns

Samples under PureBasicNanoMsg/Example

PUB / SUB

Topic filter via NN_SUB_SUBSCRIBE

REQ / REP

Request–reply RPC style

PUSH / PULL

Pipeline with NnPoll

Survey

Surveyor / Respondent · deadline · ETIMEDOUT / EFSM

Bus

Bidirectional mesh with NnPoll

PAIR · inproc

PairInproc smoke test · SendmsgIovec

PUB / SUB Example (nanomsg)

Prefix the published message so it matches the subscriber filter

Publisher (excerpt) PureBasic · x64
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
Subscriber (excerpt) nn_recv has no null terminator
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)

Get Started

Environment · runtime · build

Environment

  • Windows 7+
  • PureBasic 6.0+ (recommend)
  • Modules: PureBasic 5.20+
  • x64 only (bundled DLLs)

Runtimes

PureBasicNanoMsg/Library/x64/nanomsg.dll
PureBasicNNG/Library/x64/nng.dll

Examples Chdir into the library folder before DllOpen.

Build

Requires PureBasic Compiler. Tested under Windows 11. Include Core Enums + Wrapper modules for your chosen stack.

Scalable Messaging in PureBasic

Clone the repo, pick nanomsg or NNG, and start with PUB/SUB or REQ/REP. For new projects, prefer PureBasicNNG.