Sometimes, you need to process messages as they arrive. You may not know the source of the messages at startup. They could be sent to the service later: via an incoming message, request, or even generated randomly as a temporary queue for processing the response. In these cases, you cannot use the regular FastStream@broker.subscriber() decorators.
However, the framework still allows you to do so in a suitable manner.
Warning
Dynamic subscribers are not supported by TestBroker.
The examples below will not work.
1234567
broker=KafkaBroker()asyncwithTestKafkaBroker(broker)asbr:subscriber=br.subscriber("dynamic-topic",persistent=False)asyncwithsubscriber:message=awaitsubscriber.get_one()# does not work
1234567
broker=KafkaBroker()asyncwithTestKafkaBroker(broker)asbr:subscriber=br.subscriber("dynamic-topic-confluent",persistent=False)asyncwithsubscriber:message=awaitsubscriber.get_one()# does not work
1234567
broker=RabbitBroker()asyncwithTestRabbitBroker(broker)asbr:subscriber=br.subscriber("dynamic-queue",persistent=False)asyncwithsubscriber:message=awaitsubscriber.get_one()# does not work
1234567
broker=NatsBroker()asyncwithTestNatsBroker(broker)asbr:subscriber=br.subscriber("dynamic-subject",persistent=False)asyncwithsubscriber:message=awaitsubscriber.get_one()# does not work
1234567
broker=RedisBroker()asyncwithTestRedisBroker(broker)asbr:subscriber=br.subscriber("dynamic-channel",persistent=False)asyncwithsubscriber:message=awaitsubscriber.get_one()# does not work
12345678
broker=MQTTBroker("localhost",port=1883)asyncwithTestMQTTBroker(broker)asbr:subscriber=br.subscriber("dynamic-topic",persistent=False)awaitsubscriber.start()message=awaitsubscriber.get_one()# does not workawaitsubscriber.stop()
To process a single message, you should create a subscriber and call the appropriate method on it. Don't forget to start the subscriber.
1 2 3 4 5 6 7 8 910
fromfaststream.kafkaimportKafkaBroker,KafkaMessageasyncdefmain():asyncwithKafkaBroker()asbroker:# connect the brokersubscriber=broker.subscriber("dynamic-topic",persistent=False)awaitsubscriber.start()message:KafkaMessage|None=awaitsubscriber.get_one(timeout=3.0)awaitsubscriber.stop()
Important
Do not forget to start and stop the subscriber manually
fromfaststream.confluentimportKafkaBroker,KafkaMessageasyncdefmain():asyncwithKafkaBroker()asbroker:# connect the brokersubscriber=broker.subscriber("dynamic-topic-confluent",persistent=False)awaitsubscriber.start()message:KafkaMessage|None=awaitsubscriber.get_one(timeout=3.0)awaitsubscriber.stop()
Important
Do not forget to start and stop the subscriber manually
fromfaststream.rabbitimportRabbitBroker,RabbitMessageasyncdefmain():asyncwithRabbitBroker()asbroker:# connect the brokersubscriber=broker.subscriber("dynamic-queue",persistent=False)awaitsubscriber.start()message:RabbitMessage|None=awaitsubscriber.get_one(timeout=3.0)awaitsubscriber.stop()
Important
Do not forget to start and stop the subscriber manually
fromfaststream.natsimportNatsBroker,NatsMessageasyncdefmain():asyncwithNatsBroker()asbroker:# connect the brokersubscriber=broker.subscriber("dynamic-subject",persistent=False)awaitsubscriber.start()message:NatsMessage|None=awaitsubscriber.get_one(timeout=3.0)awaitsubscriber.stop()
Important
Do not forget to start and stop the subscriber manually
fromfaststream.redisimportRedisBroker,RedisChannelMessageasyncdefmain():asyncwithRedisBroker()asbroker:# connect the brokersubscriber=broker.subscriber("dynamic-channel",persistent=False)awaitsubscriber.start()message:RedisChannelMessage|None=awaitsubscriber.get_one(timeout=3.0)awaitsubscriber.stop()
Important
Do not forget to start and stop the subscriber manually
fromfaststream.mqttimportMQTTBroker,MQTTMessageasyncdefmain():asyncwithMQTTBroker()asbroker:# connect the brokersubscriber=broker.subscriber("dynamic-topic",persistent=False)awaitsubscriber.start()message:MQTTMessage|None=awaitsubscriber.get_one(timeout=3.0)awaitsubscriber.stop()
Important
Do not forget to start and stop the subscriber manually
However, if you want to process a stream of messages in a dynamic way, you should not use such unattractive methods as this one:
ugly_example.py
1234
whileTrue:msg=awaitsubscriber.get_one(timeout=3.0)ifmsg:...# do message process
It would be much better to use the built-in iteration mechanism:
123456789
fromfaststream.kafkaimportKafkaBrokerasyncdefmain():asyncwithKafkaBroker()asbroker:subscriber=broker.subscriber("dynamic-topic",persistent=False)asyncwithsubscriber:asyncformsginsubscriber:# msg is KafkaMessage type...# do message process
123456789
fromfaststream.confluentimportKafkaBrokerasyncdefmain():asyncwithKafkaBroker()asbroker:subscriber=broker.subscriber("dynamic-topic-confluent",persistent=False)asyncwithsubscriber:asyncformsginsubscriber:# msg is KafkaMessage type...# do message process
123456789
fromfaststream.rabbitimportRabbitBrokerasyncdefmain():asyncwithRabbitBroker()asbroker:subscriber=broker.subscriber("dynamic-queue",persistent=False)asyncwithsubscriber:asyncformsginsubscriber:# msg is RabbitMessage type...# do message process
123456789
fromfaststream.natsimportNatsBrokerasyncdefmain():asyncwithNatsBroker()asbroker:subscriber=broker.subscriber("dynamic-subject",persistent=False)asyncwithsubscriber:asyncformsginsubscriber:# msg is NatsMessage type...# do message process
123456789
fromfaststream.redisimportRedisBrokerasyncdefmain():asyncwithRedisBroker()asbroker:subscriber=broker.subscriber("dynamic-channel",persistent=False)asyncwithsubscriber:asyncformsginsubscriber:# msg is RedisMessage type...# do message process
1 2 3 4 5 6 7 8 91011
fromfaststream.mqttimportMQTTBroker,MQTTMessageasyncdefmain():asyncwithMQTTBroker()asbroker:subscriber=broker.subscriber("dynamic-topic",persistent=False)awaitsubscriber.start()asyncformsginsubscriber:# msg is MQTTMessage type...# do message processawaitsubscriber.stop()