NATS Publishing¶
To send messages, NatsBroker
uses the unified publish
method.
import asyncio
from propan 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
: bytes | str | dict | Sequence[Any] | pydantic.BaseModel - message to sendsubject
: str - subject, where the message will be sent.
Message Parameters¶
headers
: dict[str, Any] | None = None - headers of the message being sent (used by consumers)
RPC arguments¶
Also publish
supports common arguments for creating RPC queries:
reply_to
: str = "" - which channel to send the response to (used for asynchronous RPC requests)callback
: bool = False - whether to expect a response to the messagecallback_timeout
: float | None = 30.0 - timeout waiting for a response. In the case ofNone
- waits indefinitelyraise_timeout
: bool = FalseFalse
- return None in case of timeoutTrue
- errorTimeoutError
in case of timeout