PureBasic · libzmq · Messaging

PureBasicZMQ

ZeroMQ Wrapper for PureBasic

libzmq 4.3.6 API PureBasic 6.0+ x86 / x64 Module + Low-level MIT License

Bring high-performance messaging to PureBasic. Bind, connect, publish, request, push, and more — with module wrappers, multipart frames, Curve security, and complete pattern examples.

Key Features

Full Messaging Patterns

PUB/SUB, REQ/REP, PUSH/PULL, DEALER/ROUTER, PAIR, STREAM — with low-level and Module examples.

Multipart Frames

SendFrames, SendStrings, RecvAllFrames, HasRcvMore — clean multi-part message helpers.

Module API

ZmqContext, ZmqSocket, ZmqRuntime — structured PureBasic modules for cleaner code.

Security

CURVE, PLAIN, Z85 encoding — secure transports when your libzmq build supports them.

Poll · Proxy · Monitor

Socket polling, proxy helpers, socket monitor events, and capability probes via zmq_has.

x86 & x64

Place libzmq.dll under Library/x64 or Library/x86 — examples resolve path from the processor.

Messaging Patterns

Low-level and Module examples for every classic pattern

Pattern Low-level Module Port
PUB / SUB PubServer · SubClient Module/PubServer · SubClient 1689
REQ / REP ReqClient · RepServer Module/ReqClient · RepServer 1700
PUSH / PULL PushServer · PullClient Module/PushServer · PullClient 1701
DEALER / ROUTER DealerClient · RouterServer Module/DealerClient · RouterServer 1702
PAIR PairClient · PairServer Module/PairClient · PairServer 1703
STREAM StreamClient · StreamServer Module/StreamClient · StreamServer 1704
Multipart Frames.pb Module/Frames.pb 1705
Monitor Monitor.pb Module/Monitor.pb 1700 + inproc

Also: Has · Poll · Proxy · Z85 · Curve* · Plain · Msg · Thread · Stopwatch · Weather

PUB / SUB Example

Publisher is send-only — do not call Recv on a ZMQ_PUB socket

Publisher (excerpt) PureBasic
IncludeFile "../../Core/Enums.pbi"
IncludeFile "../../Core/ZeroMQWrapper.pbi"
UseModule ZeroMQWrapper

Define Context.i = ZmqContext::New()
Define Socket.i  = ZmqSocket::Socket(Context, #ZMQ_PUB)
Define Rc.i      = ZmqSocket::Bind(Socket, "tcp://*:1689")

If Rc <> 0
  PrintN("Bind failed: " + ZmqRuntime::Strerror(ZmqRuntime::Errno()))
Else
  Delay(200)  ; slow joiner
  While 1
    Define topic.s = "quotes"
    Define msg.s   = "Bid:" + Random(9000, 1000)
    ZmqSocket::Send(Socket, topic, Len(topic), #ZMQ_SNDMORE)
    ZmqSocket::Send(Socket, msg, Len(msg), 0)
    Delay(100)
  Wend
EndIf
Subscriber (excerpt) Subscribe before Connect
Define Socket.i = ZmqSocket::Socket(Context, #ZMQ_SUB)
Define sub.s    = "quotes"

; Subscribe before connect to reduce missed early messages
ZmqSocket::Setsockopt(Socket, #ZMQ_SUBSCRIBE, sub, Len(sub))
ZmqSocket::Connect(Socket, "tcp://localhost:1689")

Define *buf = AllocateMemory(256)
Define n.i  = ZmqSocket::Recv(Socket, *buf, MemorySize(*buf), 0)
; Use Recv byte count — ZMQ payloads are not null-terminated
PrintN(PeekS(*buf, n, #PB_UTF8))
FreeMemory(*buf)

Return Values & Errors

libzmq conventions apply

Success / Failure

  • Most calls: 0 success, -1 failure
  • Send / Recv: byte count or -1
  • Socket / CtxNew: non-zero handle or null
  • Do not treat every 0 as failure

Diagnostics

Define Rc.i = ZmqSocket::Bind(Socket, addr)

If Rc <> 0
  PrintN("Bind failed: " +
    ZmqRuntime::Strerror(
      ZmqRuntime::Errno()))
EndIf

Get Started

Environment · library path · build

Environment

  • Windows 10+
  • PureBasic 6.0+ (recommend)
  • Modules: PureBasic 5.20+
  • libzmq 4.3.6 API constants

Library Path

Place libzmq.dll under:

PureBasicZMQ/Library/x64/
PureBasicZMQ/Library/x86/

Build

Requires PureBasic Compiler. Tested under Windows 11. Include Core/Enums.pbi and Core/ZeroMQWrapper.pbi.

Message from PureBasic

Clone the repo, drop in libzmq, and start with PUB/SUB or REQ/REP — module or low-level, your choice.