Hive Developer logo

Hive Developer Portal

PY: Stream Blockchain Transactions

How to stream transactions on the live 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.

Steps

  1. App setup Configure imports and initialization of libraries
  2. Stream blocks Stream blocks
  3. 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

To Run the tutorial

  1. review dev requirements
  2. git clone https://gitlab.syncad.com/hive/devportal.git
  3. cd devportal/tutorials/python/13_stream_blockchain_transactions
  4. pip install -r requirements.txt
  5. python index.py
  6. After a few moments, you should see a prompt for input in terminal screen.