Replies#
A message published with reply_to="<topic>" gets the handler's return value published to that
topic.
service.py
from faststream import FastStream
from faststream_gcp_pubsub import PubSubBroker
broker = PubSubBroker(project_id="my-project")
app = FastStream(broker)
@broker.subscriber("jobs-worker", topic="jobs")
async def run(job: dict[str, int]) -> dict[str, int]:
# published to the topic named by the message's `reply_to`
return {"done": True, "job": job["id"]}
@broker.subscriber("results-client", topic="results")
async def collect(result: dict[str, int]) -> None: ...
async def submit() -> None:
await broker.publish({"id": 7}, "jobs", reply_to="results")
The reply is an ordinary message: the caller reads it from a subscription of the reply topic,
as collect does.
PubSubResponse#
Return PubSubResponse to set the ordering key and the attributes (headers) of the reply. The
same options apply to every publisher the handler is decorated with.
service.py
from faststream import FastStream
from faststream_gcp_pubsub import PubSubBroker, PubSubResponse
broker = PubSubBroker(project_id="my-project")
app = FastStream(broker)
@broker.subscriber("jobs-worker", topic="jobs")
async def run(job: dict[str, str]) -> PubSubResponse:
return PubSubResponse(
{"done": True},
headers={"worker": "w1"},
ordering_key=job["owner"],
)
@broker.subscriber("results-client", topic="results")
async def collect(result: dict[str, int]) -> None: ...
No request/reply#
broker.request() raises FeatureNotSupportedException. Pub/Sub has no reply channel of its
own: an answer to a request needs a reply subscription for each caller, and creating those is
resource administration (a subscription and its permissions per caller). Publish with
reply_to naming a topic your service subscribes to, and read the answer there.