create account

Ok it's time for you to learn WebSocket! Anyone can do this at home, nothing special required! by coinables

View this thread on: hive.blogpeakd.comecency.com
· @coinables ·
Ok it's time for you to learn WebSocket! Anyone can do this at home, nothing special required!
<html>
<h2>Display Real-Time Bitcoin Transactions with JQuery and Websocket</h2>
<br><center><iframe width="560" height="315" src="https://www.youtube.com/embed/jEdfNG-eT8E" frameborder="0" allowfullscreen></iframe></center><br>
Websockets provide the client a real-time connection to a server. Websockets use their own ws: protocol so they don't use the HTTP protocol. Websockets can connect the user and server so that they receive information as it becomes available, instead of constantly sending requests back and forth to see if new data is available. Blockchain.info has a websocket API so you can get real-time information on new transactions and new blocks as they happen. The sad thing is the documentation available on their websocket page is almost non-existant and there is no example code. They provide you with the wss link to connect to and then the names of new transactions and blocks you can "subscribe" to after you connect to the websocket, but that is it. I'm going to show you how to use javascript to connect to a websocket, subscribe to messages and begin displaying live data on your website.<br><br>First of all let's put in our script tags &lt;script&gt;&lt;&#47;script&gt; and then we'll establish a connection to our websocket using new WebSocket&#40;&#41;.<br> 

<pre class="prettyprint">

var btcs = new WebSocket('wss://ws.blockchain.info/inv');

</pre><br><br>So we started by creating the variable var btcs and then we had that be equal to the new websocket connection. Now that we have a connection to the websocket we need to send it a message and tell blockchain.info which information we are interested in receiving. We are going to use the onopen and send API commands to do this. Let's call the previous variable var btcs and create a function that subscribes us to &#123;"op":"unconfirmed_sub"&#125; which is new transactions to the bitcoin network.<br>

<pre class="prettyprint">

btcs.onopen = function(){

	btcs.send(JSON.stringify({"op":"unconfirmed_sub"}));

	};

</pre>

<br><br>JSON.stringify will send a JSON request to the websocket server telling it that we want to receive updates on new transactions to the network. At this point the server will start sending "messages" to the client everytime there is a new transaction. We need to tell javascript what to do everytime a message is received using the onmessage command. Create a new function using onmessage and I'm going to name the function onmsg. Within that function we will parse the JSON using the data command.<br>

<pre class="prettyprint">

btcs.onmessage = function(onmsg){

	var response = JSON.parse(onmsg.data);

	console.log(response);

	}

</pre><br><br>At this point you could stick this script into an HTML document and when you run it, it will start shooting out all the new transactions as objects in JSON format into your browsers debug/console. If you look at it you'll notice they all start with utx and then all the transaction data is under x . The data within the x array includes input and out or sender and receiver. If we want info form the input area we would call the response variable and then add in .x.input . However there can be multiple inputs and outs so we have to clarify which one. If we add in stright brackets and put a zero &#91;0&#93;that will get us information on the first item in the array. Here's an example that will get the BTC amount that was received by the first output.<br>

<pre class="prettyprint">

btcs.onmessage = function(onmsg){

	var response = JSON.parse(onmsg.data);

	var amount = response.x.out[0].value;

	var calAmount = amount / 100000000;

	console.log(calAmount);

	}

</pre>

<br><br>You'll notice I also added in a calculation to divide the result by 100 million to get the value in bitcoins since the data is provided in satohis. Since it's still just going to the debug/console that's not very helpful to the user, let's add in some jquery to have this info output on the page. You can either install jquery to your server or just use a link to grab the min.js from jquery.com http://code.jquery.com/jquery-1.11.0.min.js.<br><br>

Within the onmessage function I'm going to start with $&#40;'#messages'&#41; which tells jquery to look for the element in the HTML that has the ID of "messages". Since I don't have one yet I'll add some HTML and create a div with the ID "messages". Now I'll tell jquery to prepend $&#40;add to the top$&#41; each new amount within a p tag. <br>
<h3>The completed code will look like this</h3><br>

<pre class="prettyprint">

&lt;html&gt;

&lt;script src="http://code.jquery.com/jquery-1.11.0.min.js"&gt;&lt;&#47;script&gt;



&lt;script&gt;

var btcs = new WebSocket('wss://ws.blockchain.info/inv');



btcs.onopen = function()

	{

	btcs.send( JSON.stringify( {"op":"unconfirmed_sub"} ) );

	};



btcs.onmessage = function(onmsg)

	{

	var response = JSON.parse(onmsg.data);

	var amount = response.x.out[0].value;

	var calAmount = amount &#47; 100000000;

	$('#messages').prepend("&lt;p&gt;" + calAmount + "&lt;&#47;p&gt;");

	}



&lt;&#47;script&gt;

&lt;body&gt;

&lt;div id="messages"&gt;&lt;&#47;div&gt;

&lt;&#47;body&gt;

&lt;&#47;html&gt;

</pre>

Now you have a live stream of all new bitcoin transactions using a websocket. You can use CSS to style it or whatever you like. You add in some code to the onmessage function that has the browser play a note everytime a new transaction goes through and make something like bitlisten.com. It's up to your imagination!

<br><br>


</html>
👍  , ,
properties (23)
authorcoinables
permlinkok-it-s-time-for-you-to-learn-websocket-anyone-can-do-this-at-home-nothing-special-required
categorytutorial
json_metadata{"tags":["tutorial","bitcoin","websocket"],"links":["http://code.jquery.com/jquery-1.11.0.min.js.","http://code.jquery.com/jquery-1.11.0.min.js"]}
created2016-08-17 15:25:51
last_update2016-08-17 15:25:51
depth0
children3
last_payout2016-09-17 15:27:33
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length5,510
author_reputation27,226,399,844
root_title"Ok it's time for you to learn WebSocket! Anyone can do this at home, nothing special required!"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id854,229
net_rshares14,754,909,710
author_curate_reward""
vote details (3)
@cheetah ·
Hi! I am a content-detection robot. This post is to help manual curators; I have NOT flagged you.
Here is similar content:
http://www.btcthreads.com/websocket.html
👍  , ,
properties (23)
authorcheetah
permlinkre-ok-it-s-time-for-you-to-learn-websocket-anyone-can-do-this-at-home-nothing-special-required-20160817t152648
categorytutorial
json_metadata""
created2016-08-17 15:26:57
last_update2016-08-17 15:26:57
depth1
children1
last_payout2016-09-17 15:27:33
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length163
author_reputation942,693,160,055,713
root_title"Ok it's time for you to learn WebSocket! Anyone can do this at home, nothing special required!"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id854,243
net_rshares10,466,949,456
author_curate_reward""
vote details (3)
@coinables ·
Yes that is my website, and I am reposting my OC.
properties (22)
authorcoinables
permlinkre-cheetah-re-ok-it-s-time-for-you-to-learn-websocket-anyone-can-do-this-at-home-nothing-special-required-20160817t152648-20160817t153152815z
categorytutorial
json_metadata{"tags":["tutorial"]}
created2016-08-17 15:32:33
last_update2016-08-17 15:32:33
depth2
children0
last_payout2016-09-17 15:27:33
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length49
author_reputation27,226,399,844
root_title"Ok it's time for you to learn WebSocket! Anyone can do this at home, nothing special required!"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id854,340
net_rshares0
@steemitboard ·
Congratulations @coinables! You have received a personal award!

[![](https://steemitimages.com/70x70/http://steemitboard.com/@coinables/birthday1.png)](http://steemitboard.com/@coinables) Happy Birthday - 1 Year on Steemit
Click on the badge to view your own Board of Honor on SteemitBoard.

For more information about this award, click [here](https://steemit.com/steemitboard/@steemitboard/steemitboard-update-8-happy-birthday)
> By upvoting this notification, you can help all Steemit users. Learn how [here](https://steemit.com/steemitboard/@steemitboard/http-i-cubeupload-com-7ciqeo-png)!
properties (22)
authorsteemitboard
permlinksteemitboard-notify-coinables-20170719t020935000z
categorytutorial
json_metadata{"image":["https://steemitboard.com/img/notifications.png"]}
created2017-07-19 02:09:33
last_update2017-07-19 02:09:33
depth1
children0
last_payout2017-07-26 02:09:33
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length593
author_reputation38,975,615,169,260
root_title"Ok it's time for you to learn WebSocket! Anyone can do this at home, nothing special required!"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id8,931,129
net_rshares0