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. Async Publish Messages (Optional)

message_id = client.publish_async("orders", {"order_id": 123, "customer": "Alice"}) #Its just async publish to the topic
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.2.tar.gz (3.6 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.2-py3-none-any.whl (3.7 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: dyffi_bus_client-0.1.2.tar.gz
  • Upload date:
  • Size: 3.6 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.2.tar.gz
Algorithm Hash digest
SHA256 71d99e1932f3040fca2493bd683be4e2554d13b6a0286c7e8997b6ad8de7e364
MD5 5c2a25fda0135d7522a1a713b79cc2dd
BLAKE2b-256 dc038b4a9a2986595fe6b315666072c2db2bc2ed2b727aac04774831633352bf

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for dyffi_bus_client-0.1.2-py3-none-any.whl
Algorithm Hash digest
SHA256 62255bba265ce4a717708820858c5ce05cc587b852be58b04db814ab596d03ff
MD5 435ad4d135b21bb896fe78904e77872c
BLAKE2b-256 a300f3ac42de364b8baa1c7e404fbd3647af6659a0badb4a09d903053595cad1

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