Nanomsg Wrapper for PureBasic Programming Language.
- Windows 7 above (recommend)
- PureBasic 6.0 above (recommend)
- Nanomsg
Building requires PureBasic Compiler and test under Windows 10.
Module features require PureBasic 5.20 and above.
Bundled runtime: PureBasicNanoMsg/Library/x64/nanomsg.dll (x64 only).
NnSetsockopt/NanomsgSocket::Setsockopttake a pointer (*optval).- Use
NnSetsockoptString/SetsockoptStringfor topic strings (NN_SUB_SUBSCRIBE). - Use
NnSetsockoptInt/SetsockoptIntfor integer options (NN_RCVTIMEO, …). NnGetsockoptrequires*optvallen(in/out length), matching the C API.NnSendtakes a buffer pointer; useNnSendString/SendStringfor text.NnPoll+NnPollFdsupport non-blocking readiness checks.- Zero-copy helpers:
NnAllocmsg/NnReallocmsg/NnFreemsgwith#NN_MSG.
Publisher Server
EnableExplicit
IncludeFile "../../Core/Enums.pbi"
IncludeFile "../../Core/NanomsgWrapper.pbi"
UseModule NanomsgWrapper
Global lpszCurrentDir.s = GetCurrentDirectory()
; Nanomsg version (x64)
CompilerIf #PB_Compiler_Processor = #PB_Processor_x64
Global lpszLibNnDir.s = "Library/x64"
Global lpszLibNnDll.s = lpszCurrentDir + lpszLibNnDir + "/nanomsg.dll"
SetCurrentDirectory(lpszCurrentDir + lpszLibNnDir)
CompilerElse
CompilerError "Only x64 nanomsg.dll is bundled."
CompilerEndIf
Global lpszServerAddr.s = "tcp://*:1689"
If DllOpen(lpszLibNnDll)
OpenConsole()
Define Socket.i = NanomsgSocket::Socket(#AF_SP, #NN_PUB)
Define Rc.i = NanomsgSocket::Bind(Socket, lpszServerAddr)
PrintN("Bind an IP address: " + lpszServerAddr)
While 1
Define lpszTopic.s = "quotes"
Define lpszMessage.s = lpszTopic + "#Bid:" + Random(9000, 1000) + ",Ask:" + Random(9000, 1000)
NanomsgSocket::SendString(Socket, lpszMessage, Len(lpszMessage), 0)
PrintN("Published: " + lpszMessage)
Delay(500)
Wend
NanomsgSocket::Close(Socket)
CloseConsole()
DllClose()
EndIf
Subscribe Client
EnableExplicit
IncludeFile "../../Core/Enums.pbi"
IncludeFile "../../Core/NanomsgWrapper.pbi"
UseModule NanomsgWrapper
Global lpszCurrentDir.s = GetCurrentDirectory()
; Nanomsg version (x64)
CompilerIf #PB_Compiler_Processor = #PB_Processor_x64
Global lpszLibNnDir.s = "Library/x64"
Global lpszLibNnDll.s = lpszCurrentDir + lpszLibNnDir + "/nanomsg.dll"
SetCurrentDirectory(lpszCurrentDir + lpszLibNnDir)
CompilerElse
CompilerError "Only x64 nanomsg.dll is bundled."
CompilerEndIf
Global lpszServerAddr.s = "tcp://localhost:1689"
If DllOpen(lpszLibNnDll)
OpenConsole()
Define Socket.i = NanomsgSocket::Socket(#AF_SP, #NN_SUB)
Define Rc.i = NanomsgSocket::Connect(Socket, lpszServerAddr)
Define lpszSubscribe.s = "quotes"
NanomsgSocket::SetsockoptString(Socket, #NN_SUB, #NN_SUB_SUBSCRIBE, lpszSubscribe)
While 1
Define *lpszBuffer = AllocateMemory(256)
If NanomsgSocket::Recv(Socket, *lpszBuffer, MemorySize(*lpszBuffer), 0) >= 0
PrintN(PeekS(*lpszBuffer, -1, #PB_UTF8))
EndIf
FreeMemory(*lpszBuffer)
Delay(10)
Wend
NanomsgSocket::Close(Socket)
Input()
CloseConsole()
DllClose()
EndIf
More samples under PureBasicNanoMsg/Example:
- PUB/SUB, REQ/REP, PUSH/PULL (with
NnPoll) - Survey (
SurveyorServer/RespondentClient, deadline +ETIMEDOUT/EFSM) - PAIR +
inproc://smoke test (PairInproc)
Copyright (c) 2017-2026 Ji-Feng Tsai.
Code released under the MIT license.
- Bus example
nn_sendmsg/nn_recvmsg/nn_device
If this application help you reduce time to coding, you can give me a cup of coffee :)
