This application subscribes to the PubNub Market Orders Stream via Python client and stores the market trades in YugabyteDB.
Check this blog post for a quick app overview.
-
Sign up for YugabyteDB Managed and create a free cluster. Additionally, follow this guide to download your cluster CA certificate and set up your cluster IP allow list.
-
Run the following to create virtual environment for app and install application dependencies (first time only):
$ python3 -m venv market_order_app_venv # create the virtual environment
$ source market_order_app_venv/bin/activate # activate the virtual environment
$ cd market-orders-app-python/app/
$ pip install -r ../requirements.txt- Configure your environment variables in the .env file (create a .env file if it doesn't exist):
DB_HOST=127.0.0.1
PORT=5433
DB_NAME=yugabyte
USERNAME=username
PASSWORD=password- Run the following to initialize the database with schema:
$ python db.pydb.py will create tables and relations using schema/default_schema.sql
- Seed the user data in the
Usertable. Run the following to insert the default user data for User table:
$ python seed_user_data.pyseed_user_data.py will create user data using the faker module and inserts it into the User table.
- Run the application:
$ python flask_app.pyYou can see the app running on http://127.0.0.1:5001
-
Test the application (You can use Postman or cURL to send the requests)
- Subscribe to a channel -
curl --location --request GET 'http://127.0.0.1:5001/subscription/add?channel=pubnub-market-orders' - Unsubscribe from a channel -
curl --location --request GET 'http://127.0.0.1:5001/subscription/remove?channel=pubnub-market-orders' - Get subscribed channels -
curl --location --request GET 'http://127.0.0.1:5001/subscription/list' - Ingest the market order stream to YugabyteDB -
curl --location --request GET 'http://127.0.0.1:5001/ingeststreamdata' - Get Trade stats -
curl --location --request GET 'http://127.0.0.1:5001/tradestats'
Here is the screenshot of how to call the API request in Postman.
- Subscribe to a channel -
-
Follow the same instructions as above (from step 2)

