Subscription Basics#
FastStream provides a Message Broker agnostic way to subscribe to event streams.
You need not even know about topics/queues/subjects or any broker inner objects you use. The basic syntax is the same for all brokers:
Tip
If you want to use Message Broker specific features, please visit the corresponding broker documentation section. In the Tutorial section, the general features are described.
Also, synchronous functions are supported as well:
Message Body Serialization#
Generally, FastStream uses your function type annotation to serialize incoming message body with Pydantic. This is similar to how FastAPI works (if you are familiar with it).
You can also access some extra features through the function arguments, such as Depends and Context if required.
However, you can easily disable Pydantic validation by creating a broker with the following option Broker(apply_types=False)
This way FastStream still consumes json.loads
result, but without pydantic validation and casting.
Warning
Setting the apply_types=False
flag not only disables type casting but also Depends
and Context
. If you want to disable only type casting, use validate=False
instead.
Multiple Subscriptions#
You can also subscribe to multiple event streams at the same time with one function. Just wrap it with multiple @broker.subscriber(...)
decorators (they have no effect on each other).