JavaScript access to the blockchain

onChainChange

This page describes the way your application can get notified of new blocks being added to the Bitcoin Cash chain.

Details

The Flowee.network.onChainChanged property takes a function and has as an implicit effect that we subscribe to chain-changed events. The server will send us updates of chain-changed events as they happen.

const FloweeServices = require('floweejs');
var Flowee = new FloweeServices();
Flowee.network.onChainChanged = function(change) {
    console.log(change);
}
// connect to the default Flowee server.
Flowee.connect();

This will give you an output like below when a new block arrived:

{
  blockId: '79070931789198c69242297db04f9678b7d7b9002634ede5f4e62d36b41d27cb',
  blockHash: Uint8Array(32) [ /* bytes */ ],
  blockHeight: 10
}

Some times the chain gets split, typically no more than 1 block. This is the result of 2 blocks being mined by different miners at the same time. In some of these cases you will get a so called “reorg” where the chain reorganizes itself after another block comes in.

In that case you will get the following output:

{
removed: [
    {
      blockId: '79070931789198c69242297db04f9678b7d7b9002634ede5f4e62d36b41d27cb',
      blockHash: [Uint8Array],
      blockHeight: 10
    },
    {
      blockId: '06e81dfcc3c3eb46004b41cb3ce67a21cf995847df07b5e44b6edb1703ff0408',
      blockHash: [Uint8Array],
      blockHeight: 9
    }
  ]
}