Hive Developer Portal
Stream Blockchain Transactions
How to stream transactions and operations from Hive blockchain.
Full, runnable src of Stream Blockchain Transactions can be downloaded as part of: tutorials/python (or download just this tutorial: devportal-master-tutorials-python-13_stream_blockchain_transactions.zip).
In this tutorial we show you how to stream transactions on the Hive blockchain using the blockchain
class found within the beem library.
Intro
Tutorial is demonstrating the typical process of streaming operations on Hive. We will show some information from certain ops, based on certain conditions.
We are using the blockchain.stream()
function provided by beem which returns each operation after it has been accepted by witnesses. By default it follows irreversible blocks which was accepted by all witnesses.
Also see:
Steps
- App setup Configure imports and initialization of libraries
- Stream blocks Stream blocks
- Sample result Sample results
1. App setup
In this tutorial we use 1 package:
beem library and interaction with Blockchain
from beem.blockchain import Blockchain
from beem import Hive
h = Hive()
blockchain = Blockchain(blockchain_instance=h)
Above we create an instance of Blockchain which will give us the ability to stream the live transactions from the blockchain.
2. Stream blocks
Next we create an instance of stream
and then loop through the steam as transactions are available and print them to the screen.
stream = blockchain.stream()
for op in stream:
if op["type"] == 'comment':
if len(op["parent_author"]) == 0:
print(op["author"] + " authored a post: " + op["title"])
else:
print(op["author"] + " replied to " + op["parent_author"])
For this tutorial, we are only interested in the comment
operation. Then, we check if the author wrote a top-level post or a reply.
Also see: Broadcast Ops
3. Sample result
shortsegments replied to edje
riverflows replied to breezin
ejmh.vibes replied to kiritoccs
carlosadolfochac authored a post: NATURA.
hiveupme replied to prydefoltz
shortsegments replied to filotasriza3
walterprofe authored a post: Límites 01 Introducción
poshbot replied to prydefoltz
Final code:
from beem.blockchain import Blockchain
from beem import Hive
h = Hive()
blockchain = Blockchain(blockchain_instance=h)
stream = blockchain.stream()
for op in stream:
if op["type"] == 'comment':
if len(op["parent_author"]) == 0:
print(op["author"] + " authored a post: " + op["title"])
else:
print(op["author"] + " replied to " + op["parent_author"])
To Run the tutorial
- review dev requirements
git clone https://gitlab.syncad.com/hive/devportal.git
cd devportal/tutorials/python/13_stream_blockchain_transactions
pip install -r requirements.txt
python index.py
- After a few moments, you should see a prompt for input in terminal screen.