Skip to main content

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 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:

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>/publish with the given topic and payload.
  • subscribe(topic, handler, blocking=False)
    Opens a WebSocket connection to <api_url>/ws/<topic> 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. 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

dyffi_bus_client-0.1.3.tar.gz (3.5 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

dyffi_bus_client-0.1.3-py3-none-any.whl (3.7 kB view details)

Uploaded Python 3

File details

Details for the file dyffi_bus_client-0.1.3.tar.gz.

File metadata

  • Download URL: dyffi_bus_client-0.1.3.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

Hashes for dyffi_bus_client-0.1.3.tar.gz
Algorithm Hash digest
SHA256 b9c9ce9f297a6d4769f208a3204401346a56b26b4e7faadaa52d57bd8c3c0d8d
MD5 9cec95c3ed5ede5d3eab9a34d7eb3150
BLAKE2b-256 4cc2e4ee95ef836115486f33d4c2a33c7655a0305ed36a6f8014b1b58ff941cf

See more details on using hashes here.

File details

Details for the file dyffi_bus_client-0.1.3-py3-none-any.whl.

File metadata

File hashes

Hashes for dyffi_bus_client-0.1.3-py3-none-any.whl
Algorithm Hash digest
SHA256 876d686018f1f4fd478667b12eec34d1488f12693164538f152295f6e11eac82
MD5 5f569d7d706b798d0bd88d8b11f23967
BLAKE2b-256 d0cb74b127ad4a96aabd233136c2cfdf8da6924f6564334b05e2425d74f0ca7b

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page