FreeBASIC · nanomsg · NNG

FreeBasicNanoMsg

Nanomsg & NNG Wrappers for FreeBASIC

nanomsg NNG (next-gen) FreeBASIC 1.10.1+ x64 Type wrappers MIT License

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.

Two Packages, Side by Side

Same repo — pick the stack that matches your needs

FreeBasicNanoMsg

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

  • LibNanomsgSocket · LibNanomsgRuntime
  • Socket handles are Long (C int)
  • Bind / Connect · nn_errno / Strerror
  • Runtime: Library/x64/nanomsg.dll
Legacy · Sustaining
Recommended

FreeBasicNNG

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

  • NNG protocol constructors & updated API
  • Intentionally incompatible with nanomsg binding
  • Active development path
  • Sibling folder in the same repository
Browse FreeBasicNNG →

nanomsg Binding Features

Type Wrappers

LibNanomsgSocket, LibNanomsgRuntime, LibNanomsgMessage — OOP-style façade over nn_* APIs.

Typed Sockopts

SetsockoptString for topics, SetsockoptInt / GetsockoptInt for timeouts and deadlines.

Zero-Copy

NnAllocmsg / Reallocmsg / Freemsg with size sentinel NN_MSG on send/recv.

Poll

NnPoll / NnPollFd with NN_POLLIN / NN_POLLOUT; timeout in ms (-1 = forever).

Statistics

NnGetStatistic / GetStatistic with NN_STAT_* constants.

Clean Shutdown

NnTerm / LibNanomsgRuntime.Term for multi-thread shutdown.

Protocols & Transports

Protocols

NN_PAIR NN_PUB / NN_SUB NN_REQ / NN_REP NN_PUSH / NN_PULL NN_SURVEYOR / NN_RESPONDENT NN_BUS

Transports

NN_INPROC NN_IPC NN_TCP NN_WS NN_TCP_NODELAY NN_IPC_* · NN_WS_MSG_TYPE*

TODO: BUS example still on the roadmap

PUB / SUB Example (nanomsg)

Prefix the published message so it matches the subscriber filter

Publisher (excerpt) fbc PubServer.bas -target win64
#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
Subscriber (excerpt) nn_recv has no null terminator
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)

Get Started

Environment · runtime · build

Environment

  • Windows 7+
  • FreeBASIC 1.10.1+ (recommend)
  • Tested under Windows 10/11
  • x64 nanomsg.dll path in examples

Runtime

…/Library/x64/nanomsg.dll

Examples Chdir into the library folder before DllOpen.

Build

fbc PubServer.bas -target win64
fbc SubClient.bas -target win64

Scalable Messaging in FreeBASIC

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