ZMQ Notification System
Bitcoin Core’s ZeroMQ (ZMQ) notification system provides a real-time, lightweight interface for external applications to receive notifications about blockchain events such as new blocks and transactions.What is ZeroMQ?
ZeroMQ is a high-performance asynchronous messaging library that provides message-oriented semantics over various transport protocols including:- TCP connections
- Inter-process communication (IPC)
- Shared memory
How Bitcoin Core Uses ZMQ
Bitcoin Core acts as a “border router” that:- Implements the Bitcoin wire protocol
- Maintains the local blockchain database
- Makes consensus decisions
- Broadcasts transactions into the network
- Provides a queryable RPC interface
Key Characteristics
Read-Only Interface: The ZMQ socket in Bitcoin Core is write-only from the daemon’s perspective. PUB sockets don’t have a read function, so no state is introduced into bitcoind.
- Self-connecting and self-healing: Connections between endpoints automatically restore after outages
- No buffering required: Subscribers receive complete messages (transactions/blocks) all-at-once
- Message-oriented: Full transactions and blocks are delivered as single messages
- Sequence numbers: Each message includes a sequence number to detect lost notifications
Prerequisites
Build Requirements
ZMQ support requires:- libzmq >= 4.0.0 - The ZeroMQ C library (libzmq releases)
- Typically packaged as
libzmq3-devon Debian/Ubuntu systems - The C++ wrapper for ZeroMQ is not needed
Enabling ZMQ Support
By default, ZMQ is not automatically compiled. Enable it during the build configuration:Client Library Requirements
For Python clients, install PyZMQ:Configuration
ZMQ notifications are configured via command-line options orbitcoin.conf. Each notification type has its own endpoint configuration.
Basic Configuration Example
Configuration File Example
Inbitcoin.conf:
The same address can be used for multiple notification types. Each notification can also be specified multiple times with different addresses.
High Water Mark Configuration
The outbound message high water mark (SNDHWM) controls how many messages can be queued before blocking or dropping:-zmqpubhashtxhwm=n-zmqpubhashblockhwm=n-zmqpubrawblockhwm=n-zmqpubrawtxhwm=n-zmqpubsequencehwm=n
1000.
Socket Types and Addresses
The socket type is always PUB (publish). Valid ZeroMQ socket addresses include:| Protocol | Example | Description |
|---|---|---|
| TCP | tcp://127.0.0.1:28332 | TCP connection (IPv4) |
| TCP IPv6 | tcp://[::1]:28333 | TCP connection (IPv6) |
| IPC | unix:/tmp/bitcoind.tx.raw | Unix domain socket |
The
ZMQ_IPV6 option is automatically enabled for IPv6 addresses and ZMQ_TCP_KEEPALIVE is enabled for TCP transports.Message Structure
All ZMQ messages consist of three parts:- Topic - String identifying the notification type
- Body - Message payload (transaction/block data)
- Sequence Number - 4-byte little-endian unsigned integer
Available Notification Types
Bitcoin Core provides five notification types:| Notification | Topic | Description |
|---|---|---|
-zmqpubhashtx | hashtx | Transaction hash notifications |
-zmqpubhashblock | hashblock | Block hash notifications |
-zmqpubrawtx | rawtx | Raw transaction data |
-zmqpubrawblock | rawblock | Raw block data |
-zmqpubsequence | sequence | Mempool and block sequence events |
Important Considerations
Data Validation
Block Reorganizations
For*block topics, when the blockchain tip changes:
- Only the new tip is notified
- Reorganizations may occur without individual block notifications
- Subscribers must retrieve the chain from the last known block to the new tip
- The
invalidateblockRPC does not trigger notifications - Use the
sequencetopic for complete block connection/disconnection events
Message Loss Detection
ZMQ notifications can be lost during transmission. The sequence number allows detection:TCP Keepalive
TheZMQ_TCP_KEEPALIVE option is enabled, which activates the underlying SO_KEEPALIVE socket option. Configure OS-level keepalive settings before connection establishment:
Next Steps
ZMQ Notifications
Learn about each notification type and see code examples
RPC Reference
Explore Bitcoin Core’s JSON-RPC interface