create account

Dev snippet: How to subscribe to blocks by theoretical

View this thread on: hive.blogpeakd.comecency.com
· @theoretical ·
$38.90
Dev snippet: How to subscribe to blocks
I'm going to try something new with this post:  Sharing small specific howtos to help other developers integrate code with Steem.

Today we'll look at processing blocks as they come in.  We can actually get event notifications for new blocks using the `set_block_applied_callback` function, which is defined in `database_api.hpp` as follows [1]:

    void set_block_applied_callback( std::function<void(const variant& block_header)> cb );

This is confusing -- it takes a single parameter which is a callback function.  While you can create an `std::function` in C++, how's it possible to pass this function over the network or use this API from non-C++ languages?

The answer is that instead of accepting actual functions, the steemd server's API's accept a number representing a function.  This number is then passed by server-pushed callback notifications, and the client is responsible for routing the notification to the proper client-side callback function.

Because notifications require server push, we have to use a websocket connection.  Here's a simple example with the `wscat` utility which lets us interact with websockets in the terminal:

    $ wscat -c ws://127.0.0.1:8090/
    > {"jsonrpc": "2.0", "method": "call", "params": ["database_api","set_block_applied_callback",[1234]], "id": 1}
    < {"id":1,"result":null}
    < {"method":"notice","params":[1234,[{"previous":"0033cb1b...","timestamp":"2016-07-21T17:40:36","witness":"xeldal",...}]]}
    < {"method":"notice","params":[1234,[{"previous":"0033cb1c...","timestamp":"2016-07-21T17:40:39","witness":"blocktrades",...}]]}
    < {"method":"notice","params":[1234,[{"previous":"0033cb1d...","timestamp":"2016-07-21T17:40:42","witness":"bhuz",...}]]}

This example should be very enlightening.  We called `set_block_applied_callback` to establish a subscription, and the server started sending us notifications.  The number the client passes as the callback can be any value, it is simply echoed by the server with each notification.  If we subscribe to multiple callbacks on the same websocket connection, the client needs some way to route each notification to its corresponding subscription -- so it should simply assign diffferent callback ID's for each subscription.

Now we want to call `get_block` to get the full block data, for that we need a block number ("block height" in Bitcoin terminology).  In Steem, the first 4 bytes (8 hex digits) of the block ID represents the block number, so we can simply take the first 8 characters of the `previous` member, convert to integer and add 1 to get the block number which can be passed to the `get_block` API call:

    > {"jsonrpc": "2.0", "method": "call", "params": ["database_api","get_block",[3394332]], "id": 1}
    < {"id":1,"result":{"previous":"0033cb1b171069d351390acdc91b962a2b5bb6db","timestamp":"2016-07-21T17:40:36","witness":"xeldal",...}}

[1] Even if you're not a C++ coder, looking at the `*_api.hpp` files in the Steem source tree is very helpful because it will tell you what API methods are available and what parameters they take.
👍  , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , ,
properties (23)
authortheoretical
permlinkdev-snippet-how-to-subscribe-to-blocks
categorysteem
json_metadata{"tags":["steem"]}
created2016-07-21 18:16:06
last_update2016-07-21 18:16:06
depth0
children5
last_payout2016-08-23 12:26:36
cashout_time1969-12-31 23:59:59
total_payout_value33.178 HBD
curator_payout_value5.724 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length3,076
author_reputation30,164,760,525,645
root_title"Dev snippet: How to subscribe to blocks"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id253,945
net_rshares9,451,449,822,342
author_curate_reward""
vote details (37)
@nicoleta ·
very interesting ,i hope more people will use it. Thank you
👍  , ,
properties (23)
authornicoleta
permlinkre-theoretical-dev-snippet-how-to-subscribe-to-blocks-20160721t182101081z
categorysteem
json_metadata{"tags":["steem"]}
created2016-07-21 18:21:00
last_update2016-07-21 18:21:00
depth1
children0
last_payout2016-08-23 12:26:36
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_length59
author_reputation137,518,217,509
root_title"Dev snippet: How to subscribe to blocks"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id254,097
net_rshares2,542,037,823
author_curate_reward""
vote details (3)
@stellabelle ·
Hey, this is not in regards to this post, but i wanted to mention that i wrote about you in my latest post. Also, please fact check it because I am not sure that I described your role as mathematician of voting rewards algorithm..........let me know if i need to correct anything. https://steemit.com/vacation/@stellabelle/christmas-in-july-our-vacation-was-funded-entirely-by-steem-dollars
properties (22)
authorstellabelle
permlinkre-theoretical-dev-snippet-how-to-subscribe-to-blocks-20160722t144039103z
categorysteem
json_metadata{"tags":["steem"],"links":["https://steemit.com/vacation/@stellabelle/christmas-in-july-our-vacation-was-funded-entirely-by-steem-dollars"]}
created2016-07-22 14:40:39
last_update2016-07-22 14:40:39
depth1
children0
last_payout2016-08-23 12:26:36
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_length390
author_reputation516,061,669,130,124
root_title"Dev snippet: How to subscribe to blocks"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id274,806
net_rshares0
@tatyanasvitsa ·
Lol great post)))
properties (22)
authortatyanasvitsa
permlinkre-theoretical-dev-snippet-how-to-subscribe-to-blocks-20160722t182332732z
categorysteem
json_metadata{"tags":["steem"]}
created2016-07-21 18:18:12
last_update2016-07-21 18:18:12
depth1
children0
last_payout2016-08-23 12:26:36
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_length17
author_reputation37,999,020,213
root_title"Dev snippet: How to subscribe to blocks"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id254,000
net_rshares0
@weenis ·
![Raised Fist](http://i.imgur.com/voMPMhz.gif) 
 Rock On @theoretical!
properties (22)
authorweenis
permlinkdev-snippet-how-to-subscribe-to-blocks
categorysteem
json_metadata""
created2016-07-21 18:16:42
last_update2016-07-21 18:16:42
depth1
children1
last_payout2016-08-23 12:26:36
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_length70
author_reputation-4,781,861,673,917
root_title"Dev snippet: How to subscribe to blocks"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id253,962
net_rshares0
@coar ·
hello bot
properties (22)
authorcoar
permlinkre-weenis-dev-snippet-how-to-subscribe-to-blocks-20160721t183511389z
categorysteem
json_metadata{"tags":["steem"]}
created2016-07-21 18:35:12
last_update2016-07-21 18:35:12
depth2
children0
last_payout2016-08-23 12:26:36
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_length9
author_reputation4,231,602,317,552
root_title"Dev snippet: How to subscribe to blocks"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id254,475
net_rshares0