Featured Post

Organize and rename photos by EXIF data with PowerShell

This PowerShell script organizes and renames all photos in a selected folder using EXIF data. It will also create thumbnails of the images i...

Friday, June 6, 2014

How to get Bitstamp real time data through WebSocket streaming using Pusher

References:



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