1 # Block and Transaction Broadcasting with ZeroMQ
3 [ZeroMQ](http://zeromq.org/) is a lightweight wrapper around TCP
4 connections, inter-process communication, and shared-memory,
5 providing various message-oriented semantics such as publish/subscribe,
6 request/reply, and push/pull.
8 The Bitcoin Core daemon can be configured to act as a trusted "border
9 router", implementing the bitcoin wire protocol and relay, making
10 consensus decisions, maintaining the local blockchain database,
11 broadcasting locally generated transactions into the network, and
12 providing a queryable RPC interface to interact on a polled basis for
13 requesting blockchain related data. However, there exists only a
14 limited service to notify external software of events like the arrival
15 of new blocks or transactions.
17 The ZeroMQ facility implements a notification interface through a set
18 of specific notifiers. Currently there are notifiers that publish
19 blocks and transactions. This read-only facility requires only the
20 connection of a corresponding ZeroMQ subscriber port in receiving
21 software; it is not authenticated nor is there any two-way protocol
22 involvement. Therefore, subscribers should validate the received data
23 since it may be out of date, incomplete or even invalid.
25 ZeroMQ sockets are self-connecting and self-healing; that is,
26 connections made between two endpoints will be automatically restored
27 after an outage, and either end may be freely started or stopped in
30 Because ZeroMQ is message oriented, subscribers receive transactions
31 and blocks all-at-once and do not need to implement any sort of
32 buffering or reassembly.
36 The ZeroMQ feature in Bitcoin Core requires ZeroMQ API version 4.x or
37 newer. Typically, it is packaged by distributions as something like
38 *libzmq3-dev*. The C++ wrapper for ZeroMQ is *not* needed.
40 In order to run the example Python client scripts in contrib/ one must
41 also install *python3-zmq*, though this is not necessary for daemon
46 By default, the ZeroMQ feature is automatically compiled in if the
47 necessary prerequisites are found. To disable, use --disable-zmq
48 during the *configure* step of building bitcoind:
50 $ ./configure --disable-zmq (other options)
52 To actually enable operation, one must set the appropriate options on
53 the command line or in the configuration file.
57 Currently, the following notifications are supported:
60 -zmqpubhashblock=address
61 -zmqpubrawblock=address
64 The socket type is PUB and the address must be a valid ZeroMQ socket
65 address. The same address can be used in more than one notification.
69 $ bitcoind -zmqpubhashtx=tcp://127.0.0.1:28332 \
70 -zmqpubrawtx=ipc:///tmp/bitcoind.tx.raw
72 Each PUB notification has a topic and body, where the header
73 corresponds to the notification type. For instance, for the
74 notification `-zmqpubhashtx` the topic is `hashtx` (no null
75 terminator) and the body is the transaction hash (32
78 These options can also be provided in bitcoin.conf.
80 ZeroMQ endpoint specifiers for TCP (and others) are documented in the
81 [ZeroMQ API](http://api.zeromq.org/4-0:_start).
83 Client side, then, the ZeroMQ subscriber socket must have the
84 ZMQ_SUBSCRIBE option set to one or either of these prefixes (for
85 instance, just `hash`); without doing so will result in no messages
86 arriving. Please see `contrib/zmq/zmq_sub.py` for a working example.
90 From the perspective of bitcoind, the ZeroMQ socket is write-only; PUB
91 sockets don't even have a read function. Thus, there is no state
92 introduced into bitcoind directly. Furthermore, no information is
93 broadcast that wasn't already received from the public P2P network.
95 No authentication or authorization is done on connecting clients; it
96 is assumed that the ZeroMQ port is exposed only to trusted entities,
97 using other means such as firewalling.
99 Note that when the block chain tip changes, a reorganisation may occur
100 and just the tip will be notified. It is up to the subscriber to
101 retrieve the chain from the last known block to the new tip.
103 There are several possibilities that ZMQ notification can get lost
104 during transmission depending on the communication type your are
105 using. Bitcoind appends an up-counting sequence number to each
106 notification which allows listeners to detect lost notifications.