This module provides interfaces to work with any kind of message publisher and consumer like rabbitmq pubsub.
Project description
Awesome Messages Generic PubSub
Generic Message Listeners and Publishers.
This module provides interfaces to work with any kind of message publisher and consumer.
This module also provides currently the following implementations:
- RabbitMQ Message Listener
- RabbbitMQ Message Publisher
Currently only rabbitmq support is implemented but in a future other infrastructure pieces could be supported too.
The infrastructure of this package rely on abstractions so it is highly
decoupled. Apps code should depend only in MessageHandler
,
MessagePublisher
and MessageListener
.
Installation
pip install awesome_messages
Create a MessageHandler to handle the received messages
from awesome_messages.domain.handler import MessageHandler
class PrintRequestHandler(MessageHandler):
def on_msg(self, message: dict):
print(message)
Configure a listener
I'm using here the RabbitMQ listener implementation.
You can write your custom listener using MessageListener
interface.
if you write your own listener please make a pull request so that the
rest of the community can use it too.
# Import infrastructure specific listener
from awesome_messages.infra.rabbitmq.listener import RabbitMessageListener
connection_string = "amqps://user:password@host/.."
queue = "my_awesome_queue"
# Declare our listener with our previously defined MessageHandler
msg_listener = RabbitMessageListener(
connection_string, queue, PrintRequestHandler()
)
# Currently RabbitMQ listener is blocking
try:
msg_listener.start_listening()
finally:
msg_listener.stop_listening()
Configure a publisher
I'm using here the RabbitMQ publisher implementation.
You can write your custom publisher using MessagePublisher
interface.
if you write your own publisher please make a pull request so that the
rest of the community can use it too.
# Import infrastructure specific publisher
from awesome_messages.infra.rabbitmq.publisher import RabbitMessagePublisher
msg_publisher = RabbitMessagePublisher(connection_string, queue)
try:
# Publish a message
msg_publisher.publish({"my msg": "my msg data"})
finally:
msg_publisher.stop_publishing()
Author notes
The way this module is structured allows anyone to easily replace one implementation by another without changing the rest of the application code.
This module invites the developer to use dependency injection to build their app. Relying on defined interfaces rather than implementations allows changes in dependencies be possible without changing the code.
Best practices in big projects
You can use a dependency injection library for configure your message handler, listener and publisher like the following one.
Support my work
If this module has been useful to you, it would mean a lot to me if you could support my work. This way you help to keep this and other projects alive.
Contributors
- Samuel López Saura - Linkedin Contact
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
File details
Details for the file awesome_messages-1.0.0.tar.gz
.
File metadata
- Download URL: awesome_messages-1.0.0.tar.gz
- Upload date:
- Size: 4.5 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.1 CPython/3.9.13
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | b82297bc7aee342e5c460a92929b04c5f6abb85c0bbe26ec0845de80aef27838 |
|
MD5 | 9e9619c9b284279d1134b403741a5b7f |
|
BLAKE2b-256 | 0125a3fbb2d8d11a3655e31e8ee9129147aa4f085ab2712cfc3c851af20969a3 |