Skip to main content

Python

pubsub-python is a Python example illustrating the use of the Synternet Data Layer project, which facilitates subscription to existing data streams or publishing new ones. This example employs the NATS messaging system and offers a simpler starting point for integrating Python applications with the Synternet Data Layer platform.

Installation

To install the Python SDK for Data Layer, you can use pip, the Python package manager. Here's an example of how to install it:

pip install syntropynet-pubsub

Getting Started

Before you begin using the Python SDK, make sure you have the necessary credentials and access tokens from the Synternet Developer Portal platform. These credentials will allow you to connect to the Data Layer and subscribe to or publish data streams.

Example

For detailed usage examples, please refer to the examples in the repository. These examples cover various scenarios and demonstrate how to utilize the SDK's features effectively.

The preferred authentication method is using an access token from the developer portal.

import asyncio
import nats
import tempfile
import os

from helper import create_app_jwt

access_token = "SAAGNJOZTRPYYXG2NJX3ZNGXYUSDYX2BWO447W3SHG6XQ7U66RWHQ3JUXM"

async def main():
jwt = create_app_jwt(access_token)

# Write the JWT to a temporary file with correct format
with tempfile.NamedTemporaryFile(mode='w+t', delete=False) as temp:
temp.write(jwt)
temp_path = temp.name

nc = await nats.connect("nats://127.0.0.1", user_credentials=temp_path)

async def message_handler(msg):
subject = msg.subject
data = msg.data.decode()
print("Received a message on '{subject}: {data}".format(
subject=subject, data=data))
# await nc.publish("syntropy.test.subject", msg.data)

await nc.subscribe("syntropy.bitcoin.tx", cb=message_handler)

# run infinitely
while True:
await asyncio.sleep(1)

# Terminate connection to NATS.
await nc.drain()

# Delete the temporary file
os.unlink(temp_path)

if __name__ == '__main__':
asyncio.run(main())

Support

If you encounter any difficulties or have questions regarding the Python SDK for Data Layer, please reach out to our support team at Discord #developer-discussion. We are here to assist you and ensure a smooth experience with our SDK.

We hope this documentation provides you with a comprehensive understanding of the Python SDK for the Data Layer. Happy coding with real-time data streams and enjoy the power of the Data Layer in your Python applications!