Skip to content

voro6yov/message-flow

Repository files navigation

Message Flow

CI Coverage
pypi downloads
license

Message Flow is a first Web framework for building event-driven applications with Python 3.10+.

Asynchronous APIs, which allow servers to push notifications and data directly to the client as soon as available, are becoming increasingly important in modern applications that offer instant feedback and streaming data. Message Flow gives you all the tools needed to implement asynchronous APIs.

Why use Message Flow

  • Fully-featured: Supports pub-sub and request-async response communication patterns.
  • Easy: Designed to be easy to use and learn. Less time reading docs.
  • Reliable: Production-ready framework, equipped with automated, interactive documentation.
  • Standards-based: Based on (and fully compatible with) the open standard AsyncAPI and JSON Schema.

Installation

Installing Message Flow is as simple as:

pip install message-flow

Requirements

Python 3.10+

  • Pydantic for the messages serialization and deserialization.

Message Flow Examples

To see Message Flow at work, let's start with a simple example, creating a consumer and publisher of OrderCreated event:

  • Create a file publisher.py with:
from message_flow import MessageFlow, Message, Payload, Header


class OrderCreated(Message):
    order_id: str = Payload()
    tenant_id: str = Header()


if __name__ == "__main__":
    app = MessageFlow()

    app.publish(OrderCreated(order_id="order_id", tenant_id="tenant_id"), channel_address="orders")
    
  • Create a file dispatcher.py with:
from message_flow import MessageFlow, Message, Payload, Header


class OrderCreated(Message):
    order_id: str = Payload()
    tenant_id: str = Header()


if __name__ == "__main__":
    app = MessageFlow()

    @app.subscribe(address="orders", message=OrderCreated)
    def order_created_handler(order_created: OrderCreated) -> None:
        print("Event received", order_created.order_id, order_created.tenant_id)

    app.dispatch()
  • Run the dispatcher with:
$ python dispatcher.py
  • Open another terminal and publish message with:
$ python publisher.py
  • In terminal with running dispatcher you should see the following message:
Event received order_id tenant_id