Skip to content

Redis List Basic Subscriber#

To start consuming from a Redis list, simply decorate your consuming function with the @broker.subscriber(...) decorator, passing a string as the list key.

In the following example, we will create a simple FastStream app that will consume messages from a "test-list" Redis list.

The full app code looks like this:

from faststream import FastStream, Logger
from faststream.redis import RedisBroker

broker = RedisBroker()
app = FastStream(broker)


@broker.subscriber(list="test-list")
async def handle(msg: str, logger: Logger):
    logger.info(msg)

Import FastStream and RedisBroker#

To use the @broker.subscriber(...) decorator, first, we need to import the base FastStream app and RedisBroker to create our broker.

from faststream import FastStream, Logger
from faststream.redis import RedisBroker

Create a RedisBroker#

Next, we will create a RedisBroker object and wrap it into the FastStream object so that we can start our app using CLI later.

broker = RedisBroker()
app = FastStream(broker)

Create a Function that will Consume Messages from a Redis list#

Let’s create a consumer function that will consume messages from "test-list" Redis list and log them.

1
2
3
@broker.subscriber(list="test-list")
async def handle(msg: str, logger: Logger):
    logger.info(msg)

The function decorated with the @broker.subscriber(...) decorator will be called when a message is pushed to the Redis list.

The message will then be injected into the typed msg argument of the function, and its type will be used to parse the message.

In this example case, when the message is pushed to a "test-list" list, it will be received by the handle function, and the logger will log the message content.