Skip to main content

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:
ZMQ support requires libzmq version 4.0.0 or higher. Check your package manager for the exact version.

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:
hashtx:
rawblock:
hashblock:
sequence:
All hashes are in reversed byte order (same format as RPC and block explorers), not internal byte order.

Sequence Notifications

The sequence 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
Sequence numbers are topic-specific. A gap in sequence numbers indicates lost messages for that topic.

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 in contrib/zmq/:
See contrib/zmq/zmq_sub.py in the Bitcoin Core repository for a complete working example.

Subscribe to Specific Topics

You must set ZMQ_SUBSCRIBE option or no messages will be received. Subscribing to b"" receives all topics.

Advanced Features

TCP Keepalive

ZMQ_TCP_KEEPALIVE is automatically enabled:
This prevents silent connection drops by network middleboxes.

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:
  1. When added to mempool
  2. When included in a block
  3. In subsequent blocks if reorganizations occur
Track transaction state separately if you need to handle each only once.

Block Notifications

Block notifications occur when:
  • New block extends the active chain tip
  • Block reorganization changes the tip
During reorganizations, only the new tip is notified. Subscribers must track the chain themselves to detect and handle reorgs.
Notifications are NOT sent when:
  • invalidateblock RPC is called
  • Historical blocks are connected during sync (with assumeutxo)

Sequence Topic Behavior

The sequence topic publishes all block connections and disconnections, unlike hashblock. This enables:
  • Complete reorg detection
  • Mempool state tracking
  • Ordered event processing

Security Considerations

Important Security Notes:
  1. No authentication: Anyone who can connect can subscribe
  2. No authorization: No access control on connections
  3. Read-only: Subscribers cannot send commands
  4. No validation: Subscribers must validate all received data
  5. Trusted network only: Expose ZMQ ports only to trusted clients

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

Solution: Rebuild with ZMQ support:

No Messages Received

Check subscription:
Check connection:

Port Already in Use

Solution: Check if port is in use:
Use a different port or stop the conflicting service.

Connection Refused

Check Bitcoin Core is running:
Verify ZMQ is configured:

Performance Tuning

High Water Mark Sizing

Set HWM based on expected message rate and acceptable memory usage:
  • Blocks: ~100-1000 (low rate)
  • Transactions: 10000-100000 (high rate)
  • Sequence: 10000+ (tracks everything)

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