Inside Bitcoin
Opening the selected file…
Opening the selected file…
The code behind the network, with room to understand it.
Release published 2026-10-28 · Snapshot fetched 2026-11-01. Latest stable release at fetch time; development branches and Git history are excluded. Only the selected code window and visible folders load here.
1# Block and Transaction Broadcasting with ZeroMQ3[ZeroMQ](https://zeromq.org/) is a lightweight wrapper around TCP4connections, inter-process communication, and shared-memory,5providing various message-oriented semantics such as publish/subscribe,6request/reply, and push/pull.8The Bitcoin Core daemon can be configured to act as a trusted "border9router", implementing the bitcoin wire protocol and relay, making10consensus decisions, maintaining the local blockchain database,11broadcasting locally generated transactions into the network, and12providing a queryable RPC interface to interact on a polled basis for13requesting blockchain related data. However, there exists only a14limited service to notify external software of events like the arrival15of new blocks or transactions.17The ZeroMQ facility implements a notification interface through a set18of specific notifiers. Currently there are notifiers that publish19blocks and transactions. This read-only facility requires only the20connection of a corresponding ZeroMQ subscriber port in receiving21software; it is not authenticated nor is there any two-way protocol22involvement. Therefore, subscribers should validate the received data23since it may be out of date, incomplete or even invalid.25ZeroMQ sockets are self-connecting and self-healing; that is,26connections made between two endpoints will be automatically restored27after an outage, and either end may be freely started or stopped in28any order.30Because ZeroMQ is message oriented, subscribers receive transactions31and blocks all-at-once and do not need to implement any sort of32buffering or reassembly.34## Prerequisites36The ZeroMQ feature in Bitcoin Core requires the ZeroMQ API >= 4.0.037[libzmq](https://github.com/zeromq/libzmq/releases).38For version information, see [dependencies.md](dependencies.md).39Typically, it is packaged by distributions as something like40*libzmq3-dev*. The C++ wrapper for ZeroMQ is *not* needed.42In order to run the example Python client scripts in the `contrib/zmq/`43directory, one must also install [PyZMQ](https://github.com/zeromq/pyzmq)44(generally with `pip install pyzmq`), though this is not necessary for daemon45operation.47## Enabling49By default, the ZeroMQ feature is not automatically compiled.50To enable, use `-DWITH_ZMQ=ON` when configuring the build system:52$ cmake -B build -DWITH_ZMQ=ON54To actually enable operation, one must set the appropriate options on55the command line or in the configuration file.57## Usage59Currently, the following notifications are supported:61-zmqpubhashtx=address62-zmqpubhashblock=address63-zmqpubrawblock=address64-zmqpubrawtx=address65-zmqpubsequence=address67The socket type is PUB and the address must be a valid ZeroMQ socket68address. The same address can be used in more than one notification.69The same notification can be specified more than once.71The option to set the PUB socket's outbound message high water mark72(SNDHWM) may be set individually for each notification:74-zmqpubhashtxhwm=n75-zmqpubhashblockhwm=n76-zmqpubrawblockhwm=n77-zmqpubrawtxhwm=n78-zmqpubsequencehwm=n80The high water mark value must be an integer greater than or equal to 0.82For instance:84$ bitcoind -zmqpubhashtx=tcp://127.0.0.1:28332 \85-zmqpubhashtx=tcp://192.168.1.2:28332 \86-zmqpubhashblock="tcp://[::1]:28333" \87-zmqpubrawtx=unix:/tmp/bitcoind.tx.raw \88-zmqpubhashtxhwm=1000090`bitcoin node` or `bitcoin gui` can also be substituted for `bitcoind`.92Notification types correspond to message topics (details in next section). For instance,93for the notification `-zmqpubhashtx` the topic is `hashtx`. These options can also be94provided in bitcoin.conf.96### Message format98All ZMQ messages share the same structure with three parts: _topic_ string,99message _body_, and _message sequence number_:101| topic | body | message sequence number |102|-----------+------------------------------------------------------+--------------------------|103| rawtx | <serialized transaction> | <4-byte LE uint> |104| hashtx | <reversed 32-byte transaction hash> | <4-byte LE uint> |105| rawblock | <serialized block> | <4-byte LE uint> |106| hashblock | <reversed 32-byte block hash> | <4-byte LE uint> |107| sequence | <reversed 32-byte block hash>C | <4-byte LE uint> |108| sequence | <reversed 32-byte block hash>D | <4-byte LE uint> |109| sequence | <reversed 32-byte transaction hash>R<8-byte LE uint> | <4-byte LE uint> |110| sequence | <reversed 32-byte transaction hash>A<8-byte LE uint> | <4-byte LE uint> |112where:114- message sequence number represents message count to detect lost messages, distinct for each topic115- all transaction and block hashes are in _reversed byte order_ (i. e. with bytes116produced by hashing function reversed), the same format as the RPC interface and block117explorers use to display transaction and block hashes119#### rawtx121Notifies about all transactions, both when they are added to mempool or when a new block122arrives. This means a transaction could be published multiple times: first when it enters123mempool and then again in each block that includes it. The body part of the message is the124serialized transaction.126#### hashtx128Notifies about all transactions, both when they are added to mempool or when a new block129arrives. This means a transaction could be published multiple times: first when it enters130mempool and then again in each block that includes it. The body part of the message is the13132-byte transaction hash in reversed byte order.133#### rawblock135Notifies when the chain tip is updated. When assumeutxo is in use, this notification will136not be issued for historical blocks connected to the background validation chainstate. The137body part of the message is the serialized block.139#### hashblock141Notifies when the chain tip is updated. When assumeutxo is in use, this notification will142not be issued for historical blocks connected to the background validation chainstate. The143body part of the message is the 32-byte block hash in reversed byte order.145#### sequence147The 8-byte LE uints correspond to _mempool sequence number_ and the types of bodies are:149- `C` : block with this hash connected150- `D` : block with this hash disconnected151- `R` : transaction with this hash removed from mempool for non-block inclusion reason152- `A` : transaction with this hash added to mempool154### Implementing ZMQ client156ZeroMQ endpoint specifiers for TCP (and others) are documented in the157[ZeroMQ API](https://libzmq.readthedocs.io/en/zeromq4-x/).159Client side, then, the ZeroMQ subscriber socket must have the160ZMQ_SUBSCRIBE option set to one or either of these prefixes (for161instance, just `hash`); without doing so will result in no messages162arriving. Please see [`contrib/zmq/zmq_sub.py`](/contrib/zmq/zmq_sub.py) for a working example.164The ZMQ_PUB socket's ZMQ_TCP_KEEPALIVE option is enabled. This means that165the underlying SO_KEEPALIVE option is enabled when using a TCP transport.166The effective TCP keepalive values are managed through the underlying167operating system configuration and must be configured prior to connection establishment.169For example, when running on GNU/Linux, one might use the following170to lower the keepalive setting to 10 minutes:172sudo sysctl -w net.ipv4.tcp_keepalive_time=600174Setting the keepalive values appropriately for your operating environment may175improve connectivity in situations where long-lived connections are silently176dropped by network middle boxes.178Also, the socket's ZMQ_IPV6 option is enabled to accept connections from IPv6179hosts as well. If needed, this option has to be set on the client side too.181## Remarks183From the perspective of bitcoind, the ZeroMQ socket is write-only; PUB184sockets don't even have a read function. Thus, there is no state185introduced into bitcoind directly. Furthermore, no information is186broadcast that wasn't already received from the public P2P network.188No authentication or authorization is done on connecting clients; it189is assumed that the ZeroMQ port is exposed only to trusted entities,190using other means such as firewalling.192Note that for `*block` topics, when the block chain tip changes,193a reorganisation may occur and just the tip will be notified.194It is up to the subscriber to retrieve the chain from the last known195block to the new tip. Also note that no notification will occur if the tip196was in the active chain, as would be the case after calling the `invalidateblock` RPC.197In contrast, the `sequence` topic publishes all block connections and198disconnections.200There are several possibilities that ZMQ notification can get lost201during transmission depending on the communication type you are202using. Bitcoind appends an up-counting sequence number to each203notification which allows listeners to detect lost notifications.205The `sequence` topic refers specifically to the mempool sequence206number, which is also published along with all mempool events. This207is a different sequence value than in ZMQ itself in order to allow a total208ordering of mempool events to be constructed.
Independent explanations by MyBitcoinExplorer. Not affiliated with or endorsed by Bitcoin Core. Guides are educational and do not replace the source or its tests.
Bitcoin Core source © its contributors, distributed under the MIT license. Original notices and bundled third-party licenses are retained in the source tree.