Author

Topic: Blockchain.info Websocket API (Read 1815 times)

newbie
Activity: 5
Merit: 0
August 05, 2014, 03:00:20 PM
#7
Code:




WebSocket Test



WebSocket Test



Well this is a 5 min on-top-of-the-knee script that will print every transaction received.
Check the API for all the available properties and try to change the code as you intend.
Good luck!  Wink
Thank you so much! I really appreciate it!
sr. member
Activity: 345
Merit: 500
August 03, 2014, 05:50:58 PM
#6
Code:




WebSocket Test



WebSocket Test



Well this is a 5 min on-top-of-the-knee script that will print every transaction received.
Check the API for all the available properties and try to change the code as you intend.
Good luck!  Wink
newbie
Activity: 5
Merit: 0
August 03, 2014, 02:46:17 PM
#5
I've had a couple of problems in the past when implementing that script and couldn't figure out what was causing it.
Instead I've found a similar approach, which seems to work well, you can try it out.

Code:
function initWebSocket()
{
// init blockchain websocket (activity, blocks)
var blockchain = new WebSocket('ws://ws.blockchain.info/inv');

blockchain.onerror = function (error){ console.log('connection.onerror',error); };

blockchain.onopen = function ()
{
blockchain.send( JSON.stringify( {"op":"unconfirmed_sub"} ) ); // subscribe to uncofirmed activity
blockchain.send( JSON.stringify( {"op":"blocks_sub"} ) ); // subscribe to new blocks
};
 
blockchain.onmessage = function (message)
{
var response = JSON.parse(message.data);

var date = new Date(0);
date.setUTCSeconds( response.x.time );

if( response.op == "utx")
{
var amount = 0;

for(var i=0;i amount += response.x.out[i].value;

// amount is in satoshi
// 1 BTC = 100,000,000 Satoshi (https://en.bitcoin.it/wiki/activity)
response.amount = amount / 100000000;
response.type = TYPE_TRANSACTION;
response.index = index++;
}
else if( response.op == "block" )
{
response.type = TYPE_BLOCK;
response.amount = Math.round( response.x.height / 10000 );
}

if( DEBUG )
console.log( response.op, response );

response.date = date;

};

You can find the source here:
https://gist.github.com/npedrini/6030317

Thank you for your response! I'm really a newbie, so I tried copying & pasting the code on your github into an html file, but it stuck loading. I also tried putting your code into mine, but that didn't work either. How should I implement your code?

Thanks again for your help, I really appreciate it.
sr. member
Activity: 345
Merit: 500
August 02, 2014, 09:39:23 AM
#4
I've had a couple of problems in the past when implementing that script and couldn't figure out what was causing it.
Instead I've found a similar approach, which seems to work well, you can try it out.

Code:
function initWebSocket()
{
// init blockchain websocket (activity, blocks)
var blockchain = new WebSocket('ws://ws.blockchain.info/inv');

blockchain.onerror = function (error){ console.log('connection.onerror',error); };

blockchain.onopen = function ()
{
blockchain.send( JSON.stringify( {"op":"unconfirmed_sub"} ) ); // subscribe to uncofirmed activity
blockchain.send( JSON.stringify( {"op":"blocks_sub"} ) ); // subscribe to new blocks
};
 
blockchain.onmessage = function (message)
{
var response = JSON.parse(message.data);

var date = new Date(0);
date.setUTCSeconds( response.x.time );

if( response.op == "utx")
{
var amount = 0;

for(var i=0;i amount += response.x.out[i].value;

// amount is in satoshi
// 1 BTC = 100,000,000 Satoshi (https://en.bitcoin.it/wiki/activity)
response.amount = amount / 100000000;
response.type = TYPE_TRANSACTION;
response.index = index++;
}
else if( response.op == "block" )
{
response.type = TYPE_BLOCK;
response.amount = Math.round( response.x.height / 10000 );
}

if( DEBUG )
console.log( response.op, response );

response.date = date;

};

You can find the source here:
https://gist.github.com/npedrini/6030317
newbie
Activity: 5
Merit: 0
August 02, 2014, 09:23:18 AM
#3
Their cloud flare might be interfering causing the circuit to break. Why can't you just reload the page on an error?
Because I want to have a subscription to all new blocks (similar to how they display on their homepage). I'm positive that it isn't cloudflare that stops me, it's blockchain.info's servers that think I'm not there anymore (because they haven't heard anything for a while).
legendary
Activity: 2912
Merit: 1060
August 02, 2014, 04:11:16 AM
#2
Their cloud flare might be interfering causing the circuit to break. Why can't you just reload the page on an error?
newbie
Activity: 5
Merit: 0
July 31, 2014, 08:40:57 AM
#1
Hi,

I'm trying to get the blockchain.info websocket api to connect for subscribing to blocks. I basically took the code off http://www.websocket.org/echo.html and plugged in blockchain.info parameters. It works, but I'm finding that after a while it will automatically disconnect. How do I keep the session alive?
Here's my code:
Code:




WebSocket Test



WebSocket Test





Jump to: