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

File metadata

  • Download URL: swarmauri_publisher_rabbitmq-0.3.0.dev45.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.dev45.tar.gz
Algorithm Hash digest
SHA256 3c2a74a33debcab6c09821d8d2fcbf644bf79d13565a762a36ed49e69fea47e8
MD5 83bdbb07aed47e24202883478d5be68d
BLAKE2b-256 f9cc4170dcc10166d9ac7381d9e06178b1855912b2450ee6c848c816a8865a70

See more details on using hashes here.

File details

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

File metadata

  • Download URL: swarmauri_publisher_rabbitmq-0.3.0.dev45-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.dev45-py3-none-any.whl
Algorithm Hash digest
SHA256 3e49c71130eac4895e14f96aaa5338a12ebd677d826468cee84fe758ce968cf1
MD5 250a18c4a44045d6aa73552ae86518b5
BLAKE2b-256 e08e29fe2258eec112d2ff8c2c00a3d491ff0695714e27a3a6744dfaf02b60cb

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