Overview
ZeroMQ (ZMQ) is a lightweight messaging library that enables Bitcoin Core to publish real-time notifications about blocks and transactions. This provides a push-based alternative to polling RPC methods for monitoring blockchain events.ZMQ is a read-only notification interface. It requires no authentication and implements no two-way protocol. Subscribers should validate received data.
What is ZeroMQ?
ZeroMQ provides:- Message-oriented semantics: Publish/subscribe, request/reply, push/pull
- Self-connecting sockets: Automatic connection recovery
- Self-healing: Resilient to endpoint failures
- No buffering needed: All-at-once message delivery
- Multiple transports: TCP, IPC, inproc, Unix sockets
Key Features
- Real-time notifications: Instant push when events occur
- Multiple subscribers: Many clients can listen simultaneously
- Lightweight: Minimal overhead on Bitcoin Core
- Language agnostic: Works with any ZMQ client library
- Resilient: Either end can start/stop independently
Prerequisites
1
Install ZeroMQ Library
Debian/Ubuntu:macOS:Note: You only need
libzmq, not the C++ wrapper.2
Build Bitcoin Core with ZMQ Support
3
Install Python ZMQ (Optional)
For example client scripts:
Notification Types
Bitcoin Core supports five ZMQ notification topics:Block Notifications
Transaction Notifications
Sequence Notifications
Configuration Options
Basic Configuration
Multiple Endpoints
You can publish the same notification to multiple addresses:High Water Mark (HWM)
Control the message queue size for each notification:The high water mark is the maximum number of messages to queue before dropping. Higher values use more memory but prevent message loss during bursts.
Transport Types
TCP Sockets
Unix Domain Sockets
IPC Sockets
Message Format
All ZMQ messages have three parts:Message Structure
Topic-Specific Body Formats
rawtx:Sequence Notifications
Thesequence topic provides ordered mempool and block events:
Event Types
Sequence Number
Each topic maintains its own sequence counter:- Increments with each message
- Allows detection of lost messages
- Independent per topic
- Wraps at 2^32
Example Configurations
Monitor New Transactions
bitcoin.conf
Monitor New Blocks
bitcoin.conf
Full Monitoring Setup
bitcoin.conf
Unix Sockets for Local Apps
bitcoin.conf
Client Implementation
Python Example
Bitcoin Core includes example Python clients incontrib/zmq/:
Subscribe to Specific Topics
Advanced Features
TCP Keepalive
ZMQ_TCP_KEEPALIVE is automatically enabled:IPv6 Support
ZMQ_IPV6 is enabled by default:Lost Message Detection
Detect lost messages using sequence numbers:Behavior and Limitations
Transaction Notifications
Transactions are notified multiple times:
- When added to mempool
- When included in a block
- In subsequent blocks if reorganizations occur
Block Notifications
Block notifications occur when:- New block extends the active chain tip
- Block reorganization changes the tip
invalidateblockRPC is called- Historical blocks are connected during sync (with assumeutxo)
Sequence Topic Behavior
Thesequence topic publishes all block connections and disconnections, unlike hashblock.
This enables:
- Complete reorg detection
- Mempool state tracking
- Ordered event processing
Security Considerations
Best Practices
Security Best Practices:
- Use Unix sockets for local applications
- Bind TCP sockets to localhost (127.0.0.1) only
- Use firewall rules to restrict access
- Validate all received data before use
- Monitor for gaps in sequence numbers
- Don’t expose ZMQ ports to the Internet
Firewall Configuration
Troubleshooting
ZMQ Not Compiled In
No Messages Received
Check subscription:Port Already in Use
Connection Refused
Check Bitcoin Core is running:Performance Tuning
High Water Mark Sizing
Transport Selection
Use Cases
Real-Time Block Explorer
bitcoin.conf
Payment Processing
bitcoin.conf
Lightning Network Node
bitcoin.conf
Analytics Platform
bitcoin.conf
See Also
- P2P Network - Network configuration
- RPC Interface - JSON-RPC API
- ZeroMQ Documentation
- ZeroMQ API Reference
contrib/zmq/zmq_sub.py- Example Python client