Get ZMQ notifications from bitcoind-like coins
npm install --save bitcoind-zmq
And make sure to run bitcoind or whatever btc-like coin (that support zmq) with his zmq options.
bitcoind -zmqpubhashtx=tcp://127.0.0.1:28332 -zmqpubhashblock=tcp://127.0.0.1:28332
const BitcoindZmq = require('bitcoind-zmq')
const opts = { maxRetry: 20 }
const btcd = new BitcoindZmq({
// topic: <zmq node>
hashtx: 'tcp://127.0.0.1:28332',
hashblock: 'tcp://127.0.0.1:28332',
rawtx: 'tcp://127.0.0.1:28334',
rawblock: 'tcp://127.0.0.1:28334'
}, opts)
btcd.connect()
btcd.on('hashblock', (hash) => {
// hash <Buffer ... />
})
btcd.on('hashtx', (hash) => {
// hash <Buffer ... />
})
btcd.on('rawblock', (block) => {
// block <Buffer ... />
})
btcd.on('rawtx', (tx) => {
// tx <Buffer ... />
})
btcd.on('connect:*', (uri, type) => {
console.log(`socket ${type} connected to ${uri}`)
})
btcd.on('retry:hashtx', (type, attempt) => {
console.log(`hashtx, connect retry attempt: ${attempt}`)
})
btcd.on('error:*', (err, type) => {
console.error(`${type} had error:`, err)
})
The BitcoindZMQ({...}, <opts>)
Class accepts in his constructor topic -> zmq node
pairs. The only option available is maxRetry
the maximum n. of attempt to connect to a zmq node.
.add(<type>, <uri>)
: Add atopic -> zmq node
pair (ex..add('hashtx', 'tcp://127.0.0.1:28333')
)..on(<eventName>, <fn>)
: the event name could be a EventEmitter2 pattern with namespaces (ex..on('connect:hashtx', () => console.log('watching hashtx'))
)..connect(<nodeType>)
: nodeType could be a String/Array of previously added topics to connect to. If none is provided it will connect to all the added nodes..disconnect(<nodeType>)
: disconnect from the given nodes or from all nodes if no argument is provided.
Also refer to the example for usage.
Bitcoind available events are these below. You can add new coins' events or get rid of those you don't need when instantiating the Class:
hashblock
hashtx
rawblock
rawtx
Reserved events and namespaces:
error:*
event fired withError
close:*
: event fired withError
if any, andnodeType
.connect:*
: event fired withUri
,nodeType
.retry:*
: event fired withnodeType
,attempt
example: error:*
will catch every node error (error:hashblock
, error:hashtx
, and so on...)
DEBUG=bitcoind-zmq
MIT