Skip to main content

A RabbitMQ publisher implementation for Swarmauri.

Project description

Swarmauri Logo

PyPI - Downloads Hits PyPI - Python Version PyPI - License PyPI - swarmauri_publisher_rabbitmq


Swarmauri RabbitMQ Publisher

RabbitMQPublisher is the Swarmauri PublishBase implementation for RabbitMQ. The publisher opens a pika.BlockingConnection, ensures the target exchange exists, and emits JSON payloads using persistent delivery semantics.

Highlights

  • Configure using either a full AMQP URI or discrete host/port credential fields. Supplying both styles raises ValueError.
  • Declares the target exchange as a durable direct exchange during instantiation.
  • Serializes payloads with json.dumps and publishes with delivery_mode=pika.spec.PERSISTENT_DELIVERY_MODE so messages survive broker restarts.
  • On pika.exceptions.AMQPConnectionError or ChannelClosedByBroker the publisher reconnects once, re-declares the exchange, and retries the publish.

Installation

Choose the tool that fits your workflow:

# pip
pip install swarmauri_publisher_rabbitmq

# Poetry
poetry add swarmauri_publisher_rabbitmq

# uv
uv add swarmauri_publisher_rabbitmq

Configuration

RabbitMQPublisher extends PublishBase and supports the following fields:

Field Required Description
exchange The exchange name to declare and publish to. Declared as a durable direct exchange.
uri ⚪️ Full AMQP URI. When provided, omit host/port/username/password.
host ⚪️ RabbitMQ host. Required when uri is omitted.
port ⚪️ RabbitMQ port. Supply the broker port (for example 5672); the publisher does not insert a default when constructing the URI.
username ⚪️ Username used in the URI when uri is omitted. Automatically URL-encoded.
password ⚪️ Password used in the URI when uri is omitted. Automatically URL-encoded.

The publish(channel, payload) method treats channel as the RabbitMQ routing key. payload must be JSON serialisable because it is serialized with json.dumps before publishing.

Quickstart

Ensure RabbitMQ is reachable and that you have permission to declare the exchange. The example below shows the host/port configuration path.

# README Quickstart Example
from swarmauri_publisher_rabbitmq import RabbitMQPublisher

publisher = RabbitMQPublisher(
    host="localhost",
    port=5672,
    username="guest",
    password="guest",
    exchange="demo_exchange",
)

publisher.publish(
    channel="demo.routing.key",
    payload={"message": "Hello RabbitMQ!"},
)

The constructor builds the AMQP URI from the supplied parts, opens a blocking connection, and declares demo_exchange as a durable direct exchange. Each call to publish JSON-encodes the payload and requests persistent delivery so the message survives broker restarts.

Connecting with an AMQP URI

If you already have an AMQP URI, provide it directly and omit the individual connection fields:

from swarmauri_publisher_rabbitmq import RabbitMQPublisher

publisher = RabbitMQPublisher(
    uri="amqp://guest:guest@localhost:5672/",
    exchange="demo_exchange",
)

publisher.publish(
    channel="demo.routing.key",
    payload={"message": "Hello RabbitMQ via URI!"},
)

Behavior notes

  • Credentials are URL-encoded via urllib.parse.quote_plus when constructing an AMQP URI from discrete fields, and a username can be supplied without a password if the broker permits it.
  • pika.BasicProperties is called with delivery_mode=pika.spec.PERSISTENT_DELIVERY_MODE.
  • If RabbitMQ closes the channel or the connection drops, the publisher closes the connection, recreates it with the original URI, re-declares the exchange, and retries the publish once.
  • When the publisher instance is garbage-collected it closes the open connection if it is still active.

Want to help?

If you want to contribute to swarmauri-sdk, read up on our guidelines for contributing that will help you get started.

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

swarmauri_publisher_rabbitmq-0.3.0.dev46.tar.gz (8.4 kB view details)

Uploaded Source

Built Distribution

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

File details

Details for the file swarmauri_publisher_rabbitmq-0.3.0.dev46.tar.gz.

File metadata

  • Download URL: swarmauri_publisher_rabbitmq-0.3.0.dev46.tar.gz
  • Upload date:
  • Size: 8.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.10.12 {"installer":{"name":"uv","version":"0.10.12","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

File hashes

Hashes for swarmauri_publisher_rabbitmq-0.3.0.dev46.tar.gz
Algorithm Hash digest
SHA256 7040f9b0344b98bebfc02c12ca277bccafe09a077a315b53206c19406c2449b4
MD5 7f4013a853cbb742b3e01afbe49834e5
BLAKE2b-256 f8b798ffbab443010744538edb65b6932c5d2fc1e3648c6451e6932d2e722b7f

See more details on using hashes here.

File details

Details for the file swarmauri_publisher_rabbitmq-0.3.0.dev46-py3-none-any.whl.

File metadata

  • Download URL: swarmauri_publisher_rabbitmq-0.3.0.dev46-py3-none-any.whl
  • Upload date:
  • Size: 9.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.10.12 {"installer":{"name":"uv","version":"0.10.12","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

File hashes

Hashes for swarmauri_publisher_rabbitmq-0.3.0.dev46-py3-none-any.whl
Algorithm Hash digest
SHA256 44197fe7b5e398f762c2132aff2e534f831c53f193b9473ced6336fe4530ada4
MD5 72c5922f2c845566db8cde8e66184ad2
BLAKE2b-256 1d87fa9f0ff1cc31c5561ded27ddd47169c9837c251b76694ac6647ec9b5880d

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