Created
April 19, 2020 08:19
-
-
Save ultrafunkamsterdam/587ec519e323c644f303cea3289559a2 to your computer and use it in GitHub Desktop.
Blockchain Live Transactions
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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