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.dev33.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.dev33.tar.gz.

File metadata

  • Download URL: swarmauri_publisher_rabbitmq-0.3.0.dev33.tar.gz
  • Upload date:
  • Size: 8.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.10.3 {"installer":{"name":"uv","version":"0.10.3","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.dev33.tar.gz
Algorithm Hash digest
SHA256 fd2bb23fd2e08caa69982467e63f39c4fa3c947c73fd639f31a5d93a14cab5d3
MD5 36cfeb0a24b43639e5605568b7c7b0ae
BLAKE2b-256 1cc71d97be6513d7962b71fad078a06bc00b624c24967e29f40ca4153d6c1a9f

See more details on using hashes here.

File details

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

File metadata

  • Download URL: swarmauri_publisher_rabbitmq-0.3.0.dev33-py3-none-any.whl
  • Upload date:
  • Size: 9.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.10.3 {"installer":{"name":"uv","version":"0.10.3","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.dev33-py3-none-any.whl
Algorithm Hash digest
SHA256 0f9d85a8f8dbf66d81d7e63206ed90ae031cba23c3d331df67f25ab0e330fabe
MD5 82b6b43bd42e15c4a0d957082b96ed96
BLAKE2b-256 d581df9bab125e4578dc8138c20816c6d672ea445f47991b02064cb40389b5c1

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