Skip to main content

A async Pub/Sub client for Dyffi-Bus

Project description

DyffiClient

DyffiClient 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-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)
  • 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.1.tar.gz (3.4 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.1-py3-none-any.whl (3.5 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: dyffi_bus_client-0.1.1.tar.gz
  • Upload date:
  • Size: 3.4 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.1.tar.gz
Algorithm Hash digest
SHA256 966709d7af46fadaf98a029d37e334853dea8948e5b0951e3d61fa8580c1f5cb
MD5 0898f941f3e880ba5c108fb97edccdac
BLAKE2b-256 fdbdbc9c9e06440cc037fd9cd4276d8691219398bd2986390d6eab68e96522d0

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for dyffi_bus_client-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 5564b81f01b56790c401e3bf72ae8c382379c24d7a0f60cd5a1ae9ff064e79ff
MD5 ed5d32d5ca5d98f35bfc0e81b00a08a4
BLAKE2b-256 5f9e650b64313e8fc49890d8895322b419354a52128f52081594485946730274

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