Skip to content

Implemented the RabbitMQ service broker#6

Merged
CaptainAril merged 1 commit into
Developer-s-Foundry:mainfrom
Ifechukwu001:feat/rabbitmq
Apr 20, 2025
Merged

Implemented the RabbitMQ service broker#6
CaptainAril merged 1 commit into
Developer-s-Foundry:mainfrom
Ifechukwu001:feat/rabbitmq

Conversation

@Ifechukwu001
Copy link
Copy Markdown
Contributor

@Ifechukwu001 Ifechukwu001 commented Apr 19, 2025

The PR implements FastStream for RabbitMQ communications.
Closes #3

To publish a message to RabbitMQ,

   from src.config.asgi import broker
   
   async publish_message(queue: str, message: [JSON serializable object (str, int, dict, list)]) -> None:
       await broker.publish(message, queue, persist=True)
       # Successfully published

To register a consumer from RabbitMQ,

    # src/api/services/ExampleService.py
    from faststream.rabbit import RabbitRouter
    
    EXAMPLE_ROUTER = RabbitRouter()
    
    # In a function
    @EXAMPLE_ROUTER.subscriber("queue")
    async def consume(message: [JSON serializable object (it will be checked as it uses pydantic to decode the message)]) -> None:
        print(message)
        
    # In a class
    class Example:
        
        @staticmethod
        @EXAMPLE_ROUTER.subscriber("queue")
        async def (message: [JSON serializable object (it will be checked as it uses pydantic to decode the message)]) -> None:
            print(message)

Then register the consumer in the src.api.services.external.RabbitMQRoutes as below

    # src/api/services/external/RabbitMQRoutes.py
    from src.config.asgi import broker
    
    from ..ExampleService import EXAMPLE_ROUTER
    
    
    broker.include_router(EXAMPLE_ROUTER)

@CaptainAril CaptainAril merged commit d3c7584 into Developer-s-Foundry:main Apr 20, 2025
2 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

RabbitMQ Service

2 participants