Skip to content

Replies#

A message published with reply_to="<queue>" gets the handler's return value sent to that queue.

service.py
from faststream import FastStream

from faststream_sqs_sns import CapoSqsBroker

broker = CapoSqsBroker(region="eu-central-1")
app = FastStream(broker)


@broker.subscriber("jobs")
async def run(job: dict[str, int]) -> dict[str, int]:
    # sent to the queue named by the message's `reply_to`
    return {"job": job["id"]}


@broker.subscriber("results")
async def collect(result: dict[str, int]) -> None: ...


async def submit() -> None:
    await broker.publish({"id": 7}, "jobs", reply_to="results")

reply_to is a queue name, a queue URL or a topic ARN. The reply carries the correlation_id of the request. It is an ordinary message: the caller reads it from a subscriber of the reply queue, as collect does.

SqsResponse#

Return SqsResponse to set the headers, the FIFO fields and the delay of the reply. The same options apply to every publisher the handler is decorated with. A reply to a FIFO queue needs message_group_id and a deduplication_id.

service.py
from faststream import FastStream

from faststream_sqs_sns import CapoSqsBroker, SqsResponse

broker = CapoSqsBroker(region="eu-central-1")
app = FastStream(broker)


@broker.subscriber("jobs.fifo")
async def run(job: dict[str, str]) -> SqsResponse:
    return SqsResponse(
        {"done": True},
        headers={"worker": "w1"},
        message_group_id=job["owner"],
        deduplication_id=job["id"],
    )


@broker.subscriber("results.fifo")
async def collect(result: dict[str, bool]) -> None: ...

No request/reply#

broker.request() raises FeatureNotSupportedException. A request needs a reply queue for each caller, and creating those is resource administration: a queue, its policy and its deletion for every broker process. Publish with reply_to naming a queue your service consumes, and read the answer there.