Skip to content

Instantly share code, notes, and snippets.

@ultrafunkamsterdam
Created April 19, 2020 08:19
Show Gist options
  • Save ultrafunkamsterdam/587ec519e323c644f303cea3289559a2 to your computer and use it in GitHub Desktop.
Save ultrafunkamsterdam/587ec519e323c644f303cea3289559a2 to your computer and use it in GitHub Desktop.
Blockchain Live Transactions
function BlockchainRealtime(callback){
/**
* Opens a stream which subscribes to receive new unconfirmed transactions
* on the blockchain
* @param {function} callback which accepts a object of the form {input: array: output: array}
* @returns {function} return value of this function is a function, which will stop the stream if called.
*/
const sock = new WebSocket('wss://ws.blockchain.info/inv');
sock.onmessage = function(msg){
let data = JSON.parse(msg.data);
let input = data.x.inputs[0].prev_out;
let output = data.x.out[0];
callback({"input": input, "output": output})
}
sock.onopen = function(ev){
sock.send(JSON.stringify({"op":"unconfirmed_sub"}))
}
return function(){
sock.send(JSON.stringify({"op": "unconfirmed_unsub"}));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment