Skip to main content

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

Project logo

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

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

awesome_messages-1.0.0.tar.gz (4.5 kB view hashes)

Uploaded Source

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page