nuǝɯ pǝɥsᴉꞁod mǝu
Unleash awesomeness. Private packages, team management tools, and powerful integrations. Get started with npm Orgs »

bitcoind-zmq

1.0.1 • Public • Published

bitcoind-zmq

NPM Version node JavaScript Style Guide

Get ZMQ notifications from bitcoind-like coins

Install

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

Usage example

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)
})

API

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 a topic -> 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 with Error
  • close:*: event fired with Error if any, and nodeType.
  • connect:*: event fired with Uri, nodeType.
  • retry:*: event fired with nodeType, attempt

example: error:* will catch every node error (error:hashblock, error:hashtx, and so on...)

Debug

DEBUG=bitcoind-zmq

License

MIT

install

npm i bitcoind-zmq

Downloadsweekly downloads

2

version

1.0.1

license

MIT

homepage

github.com

repository

Gitgithub

last publish

collaborators

  • avatar
Report a vulnerability