Skip to content

Publishing#

FastStream NatsBroker supports all regular publishing usecases. You can use them without any changes.

However, if you wish to further customize the publishing logic, you should take a deeper look at specific NatsBroker parameters.

NATS Publishing#

NatsBroker also uses the unified publish method (from a publisher object) to send messages.

import asyncio
from faststream.nats import NatsBroker

async def pub():
    async with NatsBroker() as broker:
        await broker.publish(
            "Hi!",
            subject="test",
        )

asyncio.run(pub())

Basic Arguments#

The publish method accepts the following arguments:

  • message = "" - message to send.
  • subject: str - subject where the message will be sent.

Message Parameters#

  • headers: dict[str, str] | None = None - headers of the message being sent (used by consumers).
  • correlation_id: str | None = None - message id, which helps to match the original message with the reply to it (generated automatically).

NatsJS Parameters#

  • stream: str | None = None - validate that the subject is in the stream.
  • timeout: float | None = None - wait for the NATS server response.

Last update: 2023-10-02