A async Pub/Sub client for Dyffi-Bus
Project description
Dyffi-Bus-Client
Dyffi-Bus-Client is a lightweight Python library for interacting with Dyffi-Bus via HTTP and WebSockets. It abstracts away the details of publishing messages and subscribing to topics, letting you focus on your application logic.
Installation
pip install dyffi-bus-client
Usage
1. Create a Client
from dyffi_bus_client import DyffiBusClient
# Initialize the client with the base URL of your pub/sub service
client = DyffiBusClient("http://127.0.0.1:8000")
2. Publish Messages
message_id = client.publish("orders", {"order_id": 123, "customer": "Alice"})
print("Sent message with ID:", message_id)
2.1. Async Publish Messages (Optional)
message_id = client.publish_async("orders", {"order_id": 123, "customer": "Alice"}) #Its just async version of publish
print("Sent message with ID:", message_id)
topic: The topic name to publish to.payload: A dictionary containing the message data.
3. Subscribe to a Topic
def order_handler(message):
print("Got Message:", message)
print("Order ID:", message["payload"]["order_id"])
client.subscribe("orders", order_handler, blocking=False)
topic: The topic name to subscribe to.handler: A callback function that receives the message as a Python dictionary.blocking=False: The subscription runs in a separate thread, so your main program can continue doing other things.
If you setblocking=True, the subscription loop will block the current thread until you stop it (e.g., with Ctrl+C).
4. Keep the Main Thread Alive (Optional)
If you used non-blocking subscriptions, you can call:
client.listen()
This starts a simple loop that keeps your script running indefinitely. Press Ctrl+C to stop.
Full Examples
Example: Publishing
from dyffi_bus_client import DyffiBusClient
client = DyffiBusClient("http://127.0.0.1:8000")
message_id = client.publish("orders", {"order_id": 123, "customer": "Alice"})
print("Sent message with ID:", message_id)
Example: Subscribing to Multiple Topics
from dyffi_bus_client import DyffiBusClient
def order_handler(message):
print("Got Message:", message)
print("Order ID:", message["payload"]["order_id"])
def topic_handler(message):
print("Got Message:", message)
client = DyffiBusClient("http://127.0.0.1:8000")
# Subscribe to 'orders' in a non-blocking thread
client.subscribe("orders", order_handler, blocking=False)
# Subscribe to 'myTopic' in a blocking manner
client.subscribe("myTopic", topic_handler, blocking=True)
In this example:
- We listen to orders in a separate thread (non-blocking).
- We then subscribe to myTopic in blocking mode, so the program stays alive until we stop it.
How It Works
publish(topic, payload)
Sends an HTTP POST request to<api_url>/publishwith the given topic and payload.subscribe(topic, handler, blocking=False)
Opens a WebSocket connection to<api_url>/ws/<topic>in a separate thread, callinghandler(message)for every new message.
Ifblocking=True, it runs on the main thread until stopped.listen()
A simple blocking loop that keeps the main thread alive if you used non-blocking subscriptions.
License
This library is provided under the MIT License. Feel free to open issues or submit pull requests if you encounter any problems or have ideas for new features.
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file dyffi_bus_client-0.1.4.tar.gz.
File metadata
- Download URL: dyffi_bus_client-0.1.4.tar.gz
- Upload date:
- Size: 3.5 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.12.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
276ac96925f4ec2ec255ea86fd8bec2114b0f8b5db8602964428052e0240a617
|
|
| MD5 |
a8ce145d45bcc1c1fcac64e1480d07a7
|
|
| BLAKE2b-256 |
1f8acfc5dc708c36f1d6a1eca0799465b6cb4d706b744644a0e7170242e860a0
|
File details
Details for the file dyffi_bus_client-0.1.4-py3-none-any.whl.
File metadata
- Download URL: dyffi_bus_client-0.1.4-py3-none-any.whl
- Upload date:
- Size: 3.7 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.12.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
970c7e95855fe964fd770e0da532ecc8e37cc09b3dbf83b0814a7dd58daf912a
|
|
| MD5 |
01bdec1f391605c25445d3f243c9a0e1
|
|
| BLAKE2b-256 |
0eb4ab0dc82a2f664e75a2fd42a8f5f2dec4df51e3ec6746b5e546d29757b529
|