Added stream functions to Steem JS documentationsteemCreated with Sketch.

in #utopian-io7 years ago

U5du4GjP1oJBeM1wwJnXtGAZmS4PZb1_1680x8400.png

For the longest time I have been polling the getAccountHistory() function every few seconds to watch for specific operations for an account until I recently discovered the "stream" functions included in the Steem JS library.

I never knew about the stream functions before because they are annoyingly not included in the documentation! So I decided I should fix that so others have an easier time than I did. Additionally, most of the current documentation merely shows what the methods are, but doesn't provide any other information about what the parameters are, what the return values are, or anything else really.

So I have added the following documentation which is much more thorough and what I wish I had when first trying out the Steem JS library:

Stream Block Number

Streams block numbers by calling the callback function each time a new block is created.

var release = steem.api.streamBlockNumber(mode, function(err, result) {
    console.log(err, result);
});
// Call the returned 'release' function to stop streaming
release();

Parameters:

  • mode: 'head' (default) or 'irreversible'
  • callback: function to be called with the number of each new block that is created

Stream Block

Streams blocks by calling the callback function each time a new block is created.

steem.api.streamBlock(mode, function(err, result) {
    console.log(err, result);
});
// Call the returned 'release' function to stop streaming
release();

Parameters:

  • mode: 'head' (default) or 'irreversible'
  • callback: function to be called with the number of each new block that is created

Example block data passed as the "result" property to the callback function:

block_id: "01091c2ccea73ce2013017239cb8e9c6adac7a2a"
extensions: []
previous: "01091c2b769f7554a5932b29b81c3460fc6f335b"
signing_key: "STM6GcZkfF2rNWwNUmhDCmfLZTpdfMgQDAcAvvGL4MZ7iNVapAVkT"
timestamp: "2017-11-20T01:27:36"
transaction_ids: ["11d0ba9308dd3c25124753eb72ff95c984faf299", ...]
transaction_merkle_root: "4adfa63d8532427add4c081272af09443b8bc70d"
transactions: [{...}, ...]
witness: "pfunk"
witness_signature: "2018f9d65bf44ec3f79d5763ff4bda633146f4f3f4b8998939f648514da00c809a32f25f633b91a5610b49fd735df78cfb841576ce95133d2ee1c7a4e208dc376d"

Stream Transactions

Streams transactions by calling the callback function each time a new block is created with each transaction in the block.

steem.api.streamTransactions(mode, function(err, result) {
    console.log(err, result);
});
// Call the returned 'release' function to stop streaming
release();

Parameters:

  • mode: 'head' (default) or 'irreversible'
  • callback: function to be called with the number of each new block that is created

Example transaction data passed as the "result" property to the callback function:

expiration: "2017-11-20T01:33:06"
extensions: []
operations: [...]
ref_block_num: 7299
ref_block_prefix: 1091086591
signatures: ["205a04e6d6184bc18188938d86565505510cefac33b0838064578bf3395752edaa91c2f3e4485c8e3dd734c797c1ca2ee97"]

Stream Operations

Streams operations by calling the callback function each time a new block is created with each operation in each transaction in the block.

steem.api.streamOperations(mode, function(err, result) {
    console.log(err, result);
});
// Call the returned 'release' function to stop streaming
release();

Parameters:

  • mode: 'head' (default) or 'irreversible'
  • callback: function to be called with the number of each new block that is created

Example operation data passed as the "result" property to the callback function:

["vote", {...}]

You can see the pull request here: https://github.com/steemit/steem-js/pull/283



Posted on Utopian.io - Rewarding Open Source Contributors

Sort:  

Hi, thanks for this.... documentation is really badly needed... One question, steem.api.streamOperations always stops after a few seconds, so really it seems like a new streamOperation call is needed every 20 seconds??

Hey, that shouldn't be the case...there have been issues with the api.steemit.com node recently. Have you tried using https://rpc.buildteam.io instead and see if it works better?

Thanks for the quick response. I set:
https://rpc.buildteam.io but I get a 101 Switch Protocols, seems to be the legacy WSS and not the new JSON-RPC... it is just stuck...

It stops after a few seconds after a json-rpc call returns a code -32603 Internal Error... I am using SteemJS 0.6.7.
Is this an incompatibility of the library or is it only me?

I needed it so badly, I have gone through the documents like 3-5 times just to check how to get the new operation on the blocks. But eventually @netuoso helped me find that.

BDW if you would like to know all the operations of Steem JS then you can also check http://steem.esteem.ws/#/.

Thanks for that link, that is super useful!!!

Hi seems link is broken you provided here.
.can you pls give other references for steem js?

Great job. As you said, the steem.js documentation just list the methods we can use. It's great to see someone improving that documentation. Explaining what the method does, what parameters it takes and what the result of calling that method will be will be great. I think I've seen streamOperations method explained somewhere else but all the other are new to me. I think steem.js has a few examples on github. It will be great to see some additions in that folder too.

Didn't know these functions existed, nice to know!

Thanks, this is great! We can add our own contributions to the documentation, can we not?

Thank you for the contribution. It has been approved.

You can contact us on Discord.
[utopian-moderator]

Hey @yabapmatt I am @utopian-io. I have just upvoted you at 20% Power!

Achievements

  • Seems like you contribute quite often. AMAZING!

Community-Driven Witness!

I am the first and only Steem Community-Driven Witness. Participate on Discord. Lets GROW TOGETHER!

mooncryption-utopian-witness-gif

Up-vote this comment to grow my power and help Open Source contributions like this one. Want to chat? Join me on Discord https://discord.gg/Pc8HG9x

Best Docs ever < thx !!

Coin Marketplace

STEEM 0.16
TRX 0.16
JST 0.029
BTC 68542.93
ETH 2454.71
USDT 1.00
SBD 2.54