Save the following into a file called index.html and open it in a browser, hit F12 to view the Console and that is it! You will start seeing real time Bitcoin market trading data from Bitstamp.
It uses WebSockets and Pusher to get a constant stream of data instead of the traditional method of pulling from an API every 10 seconds.
Click here to see a working sample.
<!DOCTYPE html>
<head>
<title>Bitstamp realtime trade ticket via WebSocket Pusher</title>
<script src="http://js.pusher.com/2.2/pusher.min.js" type="text/javascript"></script>
<script type="text/javascript">
// Enable pusher logging - don't include this in production
Pusher.log = function(message) {
if (window.console && window.console.log) {
window.console.log(message);
if (message.id !== undefined)
{
// window.console.log('id:' + message.id + ' price: ' + message.price + ' amount: ' + message.amount);
}
}
};
// Bitstamp Live Ticker
var pusher = new Pusher('de504dc5763aeef9ff52');
var channel = pusher.subscribe('live_trades');
channel.bind('trade', function(data) {
window.console.log('id:' + data.id + ' price: ' + data.price + ' amount: ' + data.amount);
});
</script>
</head>
No comments:
Post a Comment