https://github.com/ametion/dyffi-bus-client
A lightweight Python client library for interacting with Dyffi Bus, enabling seamless message publishing and subscription in an asynchronous pub/sub system.
https://github.com/ametion/dyffi-bus-client
dyffi-bus library messaging pubsub python
Last synced: about 1 year ago
JSON representation
A lightweight Python client library for interacting with Dyffi Bus, enabling seamless message publishing and subscription in an asynchronous pub/sub system.
- Host: GitHub
- URL: https://github.com/ametion/dyffi-bus-client
- Owner: Ametion
- Created: 2025-02-25T23:33:51.000Z (over 1 year ago)
- Default Branch: develop
- Last Pushed: 2025-03-06T17:21:36.000Z (over 1 year ago)
- Last Synced: 2025-04-13T13:05:07.353Z (about 1 year ago)
- Topics: dyffi-bus, library, messaging, pubsub, python
- Language: Python
- Homepage:
- Size: 12.7 KB
- Stars: 1
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# 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
```bash
pip install dyffi-bus-client
```
## Usage
### 1. Create a Client
```python
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
```python
message_id = client.publish("orders", {"order_id": 123, "customer": "Alice"})
print("Sent message with ID:", message_id)
```
### 2.1. Async Publish Messages (Optional)
```python
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
```python
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 set `blocking=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:
```python
client.listen()
```
This starts a simple loop that keeps your script running indefinitely. Press Ctrl+C to stop.
## Full Examples
### Example: Publishing
```python
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
```python
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 `/publish` with the given topic and payload.
- **`subscribe(topic, handler, blocking=False)`**
Opens a WebSocket connection to `/ws/` in a separate thread, calling `handler(message)` for every new message.
If `blocking=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](LICENSE). Feel free to open issues or submit pull requests if you encounter any problems or have ideas for new features.