Bitcoin Core ZMQ Notifications: PUB/SUB Topics and Message Formats
Bitcoin Core can publish asynchronous notifications through ZeroMQ (ZMQ). Applications can subscribe to these messages instead of repeatedly querying the node for every possible change. This interface is a notification channel, not a replacement for consensus validation or authenticated RPC.
Publisher and subscriber architecture
Bitcoin Core acts as a publisher. An external process creates a SUB socket, connects to a configured endpoint and subscribes to one or more topics. The publisher does not wait for an application to acknowledge each event. A subscriber that starts late or disconnects can miss messages.
A typical local development configuration uses zmqpubhashblock=tcp://127.0.0.1:28332. This is an illustrative setting for a separate test node, not a requirement to modify an existing production deployment. The available options depend on the build and version of Bitcoin Core.
Notification topics
The hashblock and rawblock topics report block notifications using a hash or serialized block respectively. The hashtx and rawtx topics report transaction notifications using a hash or serialized transaction. Transaction notifications can occur when transactions enter the mempool and when blocks connect, so repeated transaction identifiers are normal.
The sequence topic provides more explicit information about block connection, block disconnection, mempool addition and mempool removal. Choose topics according to the application state that needs to be maintained rather than subscribing to large raw payloads unnecessarily.
Multipart message format
A notification consists of three frames: the topic, the binary body and a four-byte little-endian notification counter. The body format depends on the topic. A hash is binary data rather than a ready-made JSON string. Follow the documented byte order when converting hashes for RPC calls.
The notification counter can help detect gaps within a notification stream. It is not a block height, a global transaction number or a durable replay cursor. Account for counter wraparound and publisher restarts.
Security and recovery
Do not expose a ZMQ publisher directly to an untrusted network. The notification interface does not inherit Bitcoin Core RPC authentication. Restrict access using local bindings or appropriate private networking.
On startup, obtain an authoritative state snapshot through RPC. Use notifications to trigger updates and periodically reconcile against the node. If a message gap is detected, invalidate affected cached state and rebuild it from RPC. This approach preserves correctness even when notifications are lost.
Primary references
Bitcoin Core ZMQ documentation: https://github.com/bitcoin/bitcoin/blob/master/doc/zmq.md
