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

File metadata

  • Download URL: swarmauri_publisher_rabbitmq-0.3.0.dev38.tar.gz
  • Upload date:
  • Size: 8.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.10.4 {"installer":{"name":"uv","version":"0.10.4","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.dev38.tar.gz
Algorithm Hash digest
SHA256 a6bf66fd8531e0c9b8135ad93d64f390dc3d9a92d1c744b18b458227fbd3fc35
MD5 e1c811c6675535cfed85f068322e4736
BLAKE2b-256 4cdd70c7fa7a8da7a3fb33b927059519e155fc21c956856cd527fb5ed7bbc5c1

See more details on using hashes here.

File details

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

File metadata

  • Download URL: swarmauri_publisher_rabbitmq-0.3.0.dev38-py3-none-any.whl
  • Upload date:
  • Size: 9.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.10.4 {"installer":{"name":"uv","version":"0.10.4","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.dev38-py3-none-any.whl
Algorithm Hash digest
SHA256 4831e2ea19a7c1b1e63147805e1138cea084d76e76c7ee0ffc07d8d260449327
MD5 fd4ce69dddf3425b8092e6074d850063
BLAKE2b-256 25dfa025c347cedb7f47d2f942ca913d087ce4c3eb0924189175ccb6399eefab

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