Spectrum API - JS Client library with samples

in #spectrum2 years ago (edited)

spectrum.jpeg

EOS Tribe published JS Client library for Telos and Worbli Spectrum API endpoints:

Telos JS libraries:

Worbli JS libraries:

JS method signatures for both JS libraries:

  • get_blocks(onMessageHandler, onCloseHandler, onErrorHandler)
  • get_transaction(account, onMessageHandler, onCloseHandler, onErrorHandler)
  • get_actions(account, actions, onMessageHandler, onCloseHandler, onErrorHandler)
  • get_table_rows(account, table, scope, onMessageHandler, onCloseHandler, onErrorHandler)

Below is the sample use JS code for get_blocks method:

<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width">
  <title>Spectrum Demo</title>
  <script type=text/javascript src="https://spectrum-client.s3.amazonaws.com/telos.spectrum.min.js"></script>
  <script>
    var onMessageHandler = function (data) {
      console.log("Message: "+data);
    };
    var onCloseHandler = function(data) {
      console.log("Connection closed: "+data);
    };
    var onErrorHandler = function(error) {
      console.log("Error: "+error);
    };
    var websocket = get_blocks(onMessageHandler, onCloseHandler, onErrorHandler);
  </script>
</head>
<body>
  <p>View console for messages</p>
</body>
</html>

Sample JS code for get_transaction method:

<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width">
  <title>Spectrum Demo</title>
  <script type=text/javascript src="https://spectrum-client.s3.amazonaws.com/telos.spectrum.min.js"></script>
  <script>
    var onMessageHandler = function (data) {
      console.log("Message: "+data);
    };
    var onCloseHandler = function(data) {
      console.log("Connection closed: "+data);
    };
    var onErrorHandler = function(error) {
      console.log("Error: "+error);
    };
    var account = "eosio";
    var websocket = get_transaction(account, onMessageHandler, onCloseHandler, onErrorHandler);
  </script>
</head>
<body>
  <p>View console for messages</p>
</body>
</html>

Sample JS code for get_actions method:

<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width">
  <title>Spectrum Demo</title>
  <script type=text/javascript src="https://spectrum-client.s3.amazonaws.com/telos.spectrum.min.js"></script>
  <script>
    var onMessageHandler = function (data) {
      console.log("Message: "+data);
    };
    var onCloseHandler = function(data) {
      console.log("Connection closed: "+data);
    };
    var onErrorHandler = function(error) {
      console.log("Error: "+error);
    };
    var account = "eosio";
    var actions = ["transfer","buyram"];
    var websocket = get_actions(account, actions, onMessageHandler, onCloseHandler, onErrorHandler);
  </script>
</head>
<body>
  <p>View console for messages</p>
</body>
</html>

Feel free to give it a try and provide your feedback at out Telegram channel: https://t.me/EOSTribe

Original post describing Spectrum API architecture: https://steemit.com/eosio/@eostribe/introducing-spectrum-streaming-api-for-eosio

EOS Tribe is committed to continue work on creating innovative solutions for EOSIO.