A Node.js subscriber and strict parser for qbit Core's ZMQ notifications (new blocks, transactions and mempool changes), which tells you when you missed some.
npm install @qbit-oss/zmq zeromq@^6.8.0
# or
pnpm add @qbit-oss/zmq zeromq@^6.8.0
# or
yarn add @qbit-oss/zmq zeromq@^6.8.0
zeromq, the native ZeroMQ addon, is
an optional peer dependency: install it beside the package. Only the
subscriber's default socket needs it, loaded at the first iteration; the
parser and the types work without it, and without it a subscriber fails with
ZmqSetupError code: 'module_load'. npm and pnpm do not install an optional
peer. It ships prebuilt addons for Linux, macOS and Windows; elsewhere its
install script builds libzmq from source, and a package manager that blocks
install scripts (pnpm 10 and later) needs it approved only there. See
Packaging and dependencies.
Requirements:
^22.13.0 || ^24.0.0 || >=26.0.0. CI tests 22.13.0 and the
newest 22.x, 24.x and 26.x on Linux x64, and loads the addon on Linux arm64
and macOS arm64; other platforms, and Node.js 27 and later, are untested.-zmqpub… options. Tested against
v1.0.1.@types/node 22.0.0 or later, which
you install yourself, at the major of your Node.js line.The package ships CommonJS and ESM builds with type declarations. See Compatibility for what CI tests.
Log each new block from a testnet4 node on the same machine, in an ES module
(for top-level await):
import { QbitZmqSubscriber, ZmqSetupError } from '@qbit-oss/zmq'
// qbitd -testnet4 -zmqpubhashblock=tcp://127.0.0.1:28332
const zmq = new QbitZmqSubscriber({ url: 'tcp://127.0.0.1:28332', topics: ['hashblock'] })
try {
for await (const event of zmq) {
if (event.kind === 'message' && event.message.topic === 'hashblock') console.log('block', event.message.hash)
if (event.kind === 'gap') console.log('missed notifications: read the chain over RPC')
}
} catch (error) {
// A setup problem that retrying cannot fix, such as zeromq not installed.
if (ZmqSetupError.is(error)) console.error(error.code, error.message)
throw error
}
The loop runs until zmq.close(). A dropped connection, a qbitd restart or a
malformed message is an event, not an exception.
hashblock, hashtx,
rawblock, rawtx and sequence, is parsed and checked; a message that
does not fit is a malformed event.
Topicsgap
event says when messages were lost, and how many when that is known.
Gapshealth() reports unknown, receiving, stale or closed,
measured on the monotonic clock, so silence is never taken for health.
Healthreconnect event; heartbeats find a hung
qbitd within 30 s.
Connection handlingZmqSetupError; everything else is an event with a code or reason.
Errorsurl that libzmq would take
and then never connect to.
Addresses@qbit-oss/rpc, before
acting on it.rawblock and rawtx bodies are not decoded, and CURVE authentication is
not supported (qbitd does not offer it).The full list is in Limits.
@qbit-oss/rpc@qbit-oss/rpc, to read what
changed