Nanomsg & NNG Wrappers for FreeBASIC
Scalability protocols in FreeBASIC — publish/subscribe, request/reply, pipeline, survey, pair, and bus. Two side-by-side packages: classic nanomsg and modern NNG.
nanomsg is no longer maintained. For new work, prefer the sibling package FreeBasicNNG (NNG / nanomsg-next-gen). The two APIs are intentionally incompatible.
Same repo — pick the stack that matches your needs
Classic libnanomsg binding. Ideal for existing nanomsg code and wire-compatible peers.
LibNanomsgSocket · LibNanomsgRuntimeLong (C int)Library/x64/nanomsg.dllModern NNG (nanomsg-next-gen). Prefer this for new projects.
LibNanomsgSocket, LibNanomsgRuntime, LibNanomsgMessage — OOP-style façade over nn_* APIs.
SetsockoptString for topics, SetsockoptInt / GetsockoptInt for timeouts and deadlines.
NnAllocmsg / Reallocmsg / Freemsg with size sentinel NN_MSG on send/recv.
NnPoll / NnPollFd with NN_POLLIN / NN_POLLOUT; timeout in ms (-1 = forever).
NnGetStatistic / GetStatistic with NN_STAT_* constants.
NnTerm / LibNanomsgRuntime.Term for multi-thread shutdown.
TODO: BUS example still on the roadmap
Prefix the published message so it matches the subscriber filter
#Include "../../Core/Enums.bi"
#Include "../../Core/NanomsgWrapper.bi"
Dim NnSocketRec As LibNanomsgSocket
Dim NnRuntime As LibNanomsgRuntime
If LibNanomsgWrapper.DllOpen(lpszLibNnDll) Then
Dim Socket As Long = NnSocketRec.Socket(AF_SP, NN_PUB)
Dim Rc As Long = NnSocketRec.Bind(Socket, "tcp://*:1689")
While 1
Dim topic As String = "quotes"
' Prefix must match NN_SUB_SUBSCRIBE on the subscriber
Dim msg As String = topic & "#Bid: …,Ask:…"
Rc = NnSocketRec.Send(Socket, StrPtr(msg), Len(msg), 0)
Sleep(500)
Wend
End If
Dim Socket As Long = NnSocketRec.Socket(AF_SP, NN_SUB)
NnSocketRec.Connect(Socket, "tcp://localhost:1689")
NnSocketRec.SetsockoptString(Socket, NN_SUB, NN_SUB_SUBSCRIBE, StrPtr("quotes"))
Dim buf As Any Ptr = CAllocate(256)
Dim n As Long = NnSocketRec.Recv(Socket, buf, 256, 0)
If n >= 0 Then Print(BytesToString(buf, n)) ' copy by returned length
Deallocate(buf)
Environment · runtime · build
…/Library/x64/nanomsg.dll
Examples Chdir into the library folder before DllOpen.
fbc PubServer.bas -target win64
fbc SubClient.bas -target win64
Clone the repo, pick nanomsg or NNG, and start with PUB/SUB. For new projects, prefer FreeBasicNNG.