In this tutorial, we will demonstrate how to use the pycspr library to consume SSE (Server-Sent Events) from a node. The pycspr library provides a simple and easy to use interface for consuming events from a node, and it's built specifically for the CasperLabs blockchain platform. The library provides several classes, including NodeClient, NodeConnection, and NodeEventType. In this tutorial, we will use these classes to create a connection to a node and consume events from the node.
Step 1: Install the Pycspr Library
The first step to using the pycspr library is to install it. You can do this using the following pip command:
pip install pycspr
Step 2: Import Required Libraries
To use the pycspr library, you will need to import the following libraries:
import asyncio
from pycspr.node_client import NodeClient
from pycspr.node_connection import NodeConnection
from pycspr.node_event_type import NodeEventType
Step 3: Create a Connection to the Node
To consume events from a node, you first need to create a connection to the node. You can do this using the NodeClient class and the NodeConnection class. The NodeClient class is the entry point for interacting with a node and provides a convenient way to create a connection to the node. To create a connection, you need to pass the host, port_rest, port_rpc, and port_sse to the NodeConnection class, which is then passed to the NodeClient class.
async def main():
node_host = "localhost"
node_port_rest = 40401
node_port_rpc = 40404
node_port_sse = 40406
node_connection = NodeConnection(
host=node_host,
port_rest=node_port_rest,
port_rpc=node_port_rpc,
port_sse=node_port_sse
)
node_client = NodeClient(node_connection)
connection = await node_client.create_connection()
Step 4: Consume Events from the Node
Once you have a connection to the node, you can consume events from the node by creating an event channel. You can do this using the NodeClient class. The NodeClient class provides several methods for consuming events from a node, including get_block_commit, get_block_finalize, get_block_import, get_block_propose, and get_contract_events. In this tutorial, we will demonstrate how to use the get_contract_events method to consume contract events. To use this method, you need to pass the connection and the event type to the method.
event_type = NodeEventType.CONTRACT
async for event in node_client.get_contract_events(connection, event_type):
print(event)
Full Code Example
Here is the complete code example, which demonstrates how to use the pycspr library to consume SSE events from a node:
Here is the complete code example, which demonstrates how to use the pycspr library to consume SSE events from a node:
import asyncio
from pycspr.node_client import NodeClient
from pycspr.node_connection import NodeConnection
from pycspr.node_event_type import NodeEventType
async def main():
node_host = "localhost"
node_port_rest = 40401
node_port_rpc = 40404
node_port_sse = 40406
node_connection = NodeConnection(
host=node_host,
port_rest=node_port_rest,
port_rpc=node_port_rpc,
port_sse=node_port_sse
)
node_client = NodeClient(node_connection)
connection = await node_client.create_connection()
event_type = NodeEventType.CONTRACT
async for event in node_client.get_contract_events(connection, event_type):
print(event)
if __name__ == '__main__':
asyncio.run(main())
In conclusion, this tutorial demonstrated how to use the pycspr library to consume SSE events from a node using the NodeClient class to create a connection and the NodeEventType class to define the type of events to be consumed. We hope that you found this tutorial helpful.
To experiment and learn more about the pycspr library, we recommend that you try to consume different types of events from a node. However, if you have any issues, please submit a ticket to the Casper Labs Support team via the link found here: https://support.casperlabs.io/hc/en-gb/requests/new?ticket_form_id=4419952551067
Comments
0 comments
Please sign in to leave a comment.