NatsRouter
faststream.nats.broker.NatsRouter
#
NatsRouter(
prefix="",
handlers=(),
*,
dependencies=(),
middlewares=(),
routers=(),
parser=None,
decoder=None,
include_in_schema=None,
)
Bases: NatsRegistrator
, BrokerRouter[Msg]
Includable to NatsBroker router.
Initialize the NatsRouter instance.
PARAMETER | DESCRIPTION |
---|---|
prefix
|
String prefix to add to all subscribers subjects. Defaults to "".
TYPE:
|
handlers
|
Route object to include. Defaults to ().
TYPE:
|
dependencies
|
Dependencies list (
TYPE:
|
middlewares
|
Router middlewares to apply to all routers' publishers/subscribers. Defaults to ().
TYPE:
|
routers
|
Routers to apply to broker. Defaults to ().
TYPE:
|
parser
|
Parser to map original IncomingMessage Msg to FastStream one. Defaults to None.
TYPE:
|
decoder
|
Function to decode FastStream msg bytes body to python objects. Defaults to None.
TYPE:
|
include_in_schema
|
Whetever to include operation in AsyncAPI schema or not. Defaults to None.
TYPE:
|
Source code in faststream/nats/broker/router.py
add_middleware
#
Append BrokerMiddleware to the end of middlewares list.
Current middleware will be used as a most inner of the stack.
Source code in faststream/_internal/broker/registrator.py
insert_middleware
#
Insert BrokerMiddleware to the start of middlewares list.
Current middleware will be used as a most outer of the stack.
Source code in faststream/_internal/broker/registrator.py
subscriber
#
subscriber(
subject: str = "",
queue: str = "",
pending_msgs_limit: int | None = None,
pending_bytes_limit: int | None = None,
max_msgs: int = 0,
durable: None = None,
config: None = None,
ordered_consumer: Literal[False] = False,
idle_heartbeat: None = None,
flow_control: None = None,
deliver_policy: None = None,
headers_only: None = None,
pull_sub: Literal[False] = False,
kv_watch: None = None,
obj_watch: Literal[False] = False,
inbox_prefix: bytes = INBOX_PREFIX,
stream: None = None,
dependencies: Iterable[Dependant] = (),
parser: Optional[CustomCallable] = None,
decoder: Optional[CustomCallable] = None,
persistent: bool = True,
ack_first: bool = EMPTY,
middlewares: Sequence[SubscriberMiddleware[Any]] = (),
no_ack: bool = EMPTY,
max_workers: None = None,
ack_policy: AckPolicy = EMPTY,
no_reply: bool = False,
title: str | None = None,
description: str | None = None,
include_in_schema: bool = True,
) -> CoreSubscriber
subscriber(
subject: str = "",
queue: str = "",
pending_msgs_limit: int | None = None,
pending_bytes_limit: int | None = None,
max_msgs: int = 0,
durable: None = None,
config: None = None,
ordered_consumer: Literal[False] = False,
idle_heartbeat: None = None,
flow_control: None = None,
deliver_policy: None = None,
headers_only: None = None,
pull_sub: Literal[False] = False,
kv_watch: None = None,
obj_watch: Literal[False] = False,
inbox_prefix: bytes = INBOX_PREFIX,
stream: None = None,
dependencies: Iterable[Dependant] = (),
parser: Optional[CustomCallable] = None,
decoder: Optional[CustomCallable] = None,
persistent: bool = True,
ack_first: bool = EMPTY,
middlewares: Sequence[SubscriberMiddleware[Any]] = (),
no_ack: bool = EMPTY,
max_workers: int = ...,
ack_policy: AckPolicy = EMPTY,
no_reply: bool = False,
title: str | None = None,
description: str | None = None,
include_in_schema: bool = True,
) -> ConcurrentCoreSubscriber
subscriber(
subject: str = "",
queue: str = "",
pending_msgs_limit: int | None = None,
pending_bytes_limit: int | None = None,
max_msgs: int = 0,
durable: str | None = None,
config: Optional[ConsumerConfig] = None,
ordered_consumer: bool = False,
idle_heartbeat: float | None = None,
flow_control: bool | None = None,
deliver_policy: Optional[DeliverPolicy] = None,
headers_only: bool | None = None,
pull_sub: Literal[False] = False,
kv_watch: None = None,
obj_watch: Literal[False] = False,
inbox_prefix: bytes = INBOX_PREFIX,
stream: Union[str, JStream] = ...,
dependencies: Iterable[Dependant] = (),
parser: Optional[CustomCallable] = None,
decoder: Optional[CustomCallable] = None,
persistent: bool = True,
ack_first: bool = EMPTY,
middlewares: Sequence[SubscriberMiddleware[Any]] = (),
no_ack: bool = EMPTY,
max_workers: None = None,
ack_policy: AckPolicy = EMPTY,
no_reply: bool = False,
title: str | None = None,
description: str | None = None,
include_in_schema: bool = True,
) -> PushStreamSubscriber
subscriber(
subject: str = "",
queue: str = "",
pending_msgs_limit: int | None = None,
pending_bytes_limit: int | None = None,
max_msgs: int = 0,
durable: str | None = None,
config: Optional[ConsumerConfig] = None,
ordered_consumer: bool = False,
idle_heartbeat: float | None = None,
flow_control: bool | None = None,
deliver_policy: Optional[DeliverPolicy] = None,
headers_only: bool | None = None,
pull_sub: Literal[False] = False,
kv_watch: None = None,
obj_watch: Literal[False] = False,
inbox_prefix: bytes = INBOX_PREFIX,
stream: Union[str, JStream] = ...,
dependencies: Iterable[Dependant] = (),
parser: Optional[CustomCallable] = None,
decoder: Optional[CustomCallable] = None,
persistent: bool = True,
ack_first: bool = EMPTY,
middlewares: Sequence[SubscriberMiddleware[Any]] = (),
no_ack: bool = EMPTY,
max_workers: int = ...,
ack_policy: AckPolicy = EMPTY,
no_reply: bool = False,
title: str | None = None,
description: str | None = None,
include_in_schema: bool = True,
) -> ConcurrentPushStreamSubscriber
subscriber(
subject: str = "",
queue: str = "",
pending_msgs_limit: int | None = None,
pending_bytes_limit: int | None = None,
max_msgs: int = 0,
durable: str | None = None,
config: Optional[ConsumerConfig] = None,
ordered_consumer: bool = False,
idle_heartbeat: float | None = None,
flow_control: bool | None = None,
deliver_policy: Optional[DeliverPolicy] = None,
headers_only: bool | None = None,
pull_sub: Literal[True] = ...,
kv_watch: None = None,
obj_watch: Literal[False] = False,
inbox_prefix: bytes = INBOX_PREFIX,
stream: Union[str, JStream] = ...,
dependencies: Iterable[Dependant] = (),
parser: Optional[CustomCallable] = None,
decoder: Optional[CustomCallable] = None,
persistent: bool = True,
ack_first: bool = EMPTY,
middlewares: Sequence[SubscriberMiddleware[Any]] = (),
no_ack: bool = EMPTY,
max_workers: None = None,
ack_policy: AckPolicy = EMPTY,
no_reply: bool = False,
title: str | None = None,
description: str | None = None,
include_in_schema: bool = True,
) -> PullStreamSubscriber
subscriber(
subject: str = "",
queue: str = "",
pending_msgs_limit: int | None = None,
pending_bytes_limit: int | None = None,
max_msgs: int = 0,
durable: str | None = None,
config: Optional[ConsumerConfig] = None,
ordered_consumer: bool = False,
idle_heartbeat: float | None = None,
flow_control: bool | None = None,
deliver_policy: Optional[DeliverPolicy] = None,
headers_only: bool | None = None,
pull_sub: Literal[True] = ...,
kv_watch: None = None,
obj_watch: Literal[False] = False,
inbox_prefix: bytes = INBOX_PREFIX,
stream: Union[str, JStream] = ...,
dependencies: Iterable[Dependant] = (),
parser: Optional[CustomCallable] = None,
decoder: Optional[CustomCallable] = None,
persistent: bool = True,
ack_first: bool = EMPTY,
middlewares: Sequence[SubscriberMiddleware[Any]] = (),
no_ack: bool = EMPTY,
max_workers: int = ...,
ack_policy: AckPolicy = EMPTY,
no_reply: bool = False,
title: str | None = None,
description: str | None = None,
include_in_schema: bool = True,
) -> ConcurrentPullStreamSubscriber
subscriber(
subject: str = "",
queue: str = "",
pending_msgs_limit: int | None = None,
pending_bytes_limit: int | None = None,
max_msgs: int = 0,
durable: str | None = None,
config: Optional[ConsumerConfig] = None,
ordered_consumer: bool = False,
idle_heartbeat: float | None = None,
flow_control: bool | None = None,
deliver_policy: Optional[DeliverPolicy] = None,
headers_only: bool | None = None,
pull_sub: PullSub = ...,
kv_watch: None = None,
obj_watch: Literal[False] = False,
inbox_prefix: bytes = INBOX_PREFIX,
stream: Union[str, JStream] = ...,
dependencies: Iterable[Dependant] = (),
parser: Optional[CustomCallable] = None,
decoder: Optional[CustomCallable] = None,
persistent: bool = True,
ack_first: bool = EMPTY,
middlewares: Sequence[SubscriberMiddleware[Any]] = (),
no_ack: bool = EMPTY,
max_workers: None = None,
ack_policy: AckPolicy = EMPTY,
no_reply: bool = False,
title: str | None = None,
description: str | None = None,
include_in_schema: bool = True,
) -> Union[PullStreamSubscriber, BatchPullStreamSubscriber]
subscriber(
subject: str = "",
queue: str = "",
pending_msgs_limit: int | None = None,
pending_bytes_limit: int | None = None,
max_msgs: int = 0,
durable: None = None,
config: None = None,
ordered_consumer: Literal[False] = False,
idle_heartbeat: None = None,
flow_control: None = None,
deliver_policy: None = None,
headers_only: None = None,
pull_sub: Literal[False] = False,
kv_watch: Union[str, KvWatch] = ...,
obj_watch: Literal[False] = False,
inbox_prefix: bytes = INBOX_PREFIX,
stream: None = None,
dependencies: Iterable[Dependant] = (),
parser: Optional[CustomCallable] = None,
decoder: Optional[CustomCallable] = None,
persistent: bool = True,
ack_first: bool = EMPTY,
middlewares: Sequence[SubscriberMiddleware[Any]] = (),
no_ack: bool = EMPTY,
max_workers: None = None,
ack_policy: AckPolicy = EMPTY,
no_reply: bool = False,
title: str | None = None,
description: str | None = None,
include_in_schema: bool = True,
) -> KeyValueWatchSubscriber
subscriber(
subject: str = "",
queue: str = "",
pending_msgs_limit: int | None = None,
pending_bytes_limit: int | None = None,
max_msgs: int = 0,
durable: None = None,
config: None = None,
ordered_consumer: Literal[False] = False,
idle_heartbeat: None = None,
flow_control: None = None,
deliver_policy: None = None,
headers_only: None = None,
pull_sub: Literal[False] = False,
kv_watch: None = None,
obj_watch: Union[Literal[True], ObjWatch] = ...,
inbox_prefix: bytes = INBOX_PREFIX,
stream: None = None,
dependencies: Iterable[Dependant] = (),
parser: Optional[CustomCallable] = None,
decoder: Optional[CustomCallable] = None,
persistent: bool = True,
ack_first: bool = EMPTY,
middlewares: Sequence[SubscriberMiddleware[Any]] = (),
no_ack: bool = EMPTY,
max_workers: None = None,
ack_policy: AckPolicy = EMPTY,
no_reply: bool = False,
title: str | None = None,
description: str | None = None,
include_in_schema: bool = True,
) -> ObjStoreWatchSubscriber
subscriber(
subject: str = "",
queue: str = "",
pending_msgs_limit: int | None = None,
pending_bytes_limit: int | None = None,
max_msgs: int = 0,
durable: str | None = None,
config: Optional[ConsumerConfig] = None,
ordered_consumer: bool = False,
idle_heartbeat: float | None = None,
flow_control: bool | None = None,
deliver_policy: Optional[DeliverPolicy] = None,
headers_only: bool | None = None,
pull_sub: Union[bool, PullSub] = False,
kv_watch: Union[str, KvWatch, None] = None,
obj_watch: Union[bool, ObjWatch] = False,
inbox_prefix: bytes = INBOX_PREFIX,
stream: Union[str, JStream, None] = None,
dependencies: Iterable[Dependant] = (),
parser: Optional[CustomCallable] = None,
decoder: Optional[CustomCallable] = None,
persistent: bool = True,
ack_first: bool = EMPTY,
middlewares: Sequence[SubscriberMiddleware[Any]] = (),
no_ack: bool = EMPTY,
max_workers: int | None = None,
ack_policy: AckPolicy = EMPTY,
no_reply: bool = False,
title: str | None = None,
description: str | None = None,
include_in_schema: bool = True,
) -> LogicSubscriber[Any]
subscriber(
subject="",
queue="",
pending_msgs_limit=None,
pending_bytes_limit=None,
max_msgs=0,
durable=None,
config=None,
ordered_consumer=False,
idle_heartbeat=None,
flow_control=None,
deliver_policy=None,
headers_only=None,
pull_sub=False,
kv_watch=None,
obj_watch=False,
inbox_prefix=INBOX_PREFIX,
stream=None,
dependencies=(),
parser=None,
decoder=None,
persistent=True,
ack_first=EMPTY,
middlewares=(),
no_ack=EMPTY,
max_workers=None,
ack_policy=EMPTY,
no_reply=False,
title=None,
description=None,
include_in_schema=True,
)
Creates NATS subscriber object.
You can use it as a handler decorator @broker.subscriber(...)
.
PARAMETER | DESCRIPTION |
---|---|
subject
|
NATS subject to subscribe.
TYPE:
|
queue
|
Subscribers' NATS queue name. Subscribers with same queue name will be load balanced by the NATS server.
TYPE:
|
pending_msgs_limit
|
Limit of messages, considered by NATS server as possible to be delivered to the client without been answered. In case of NATS Core, if that limits exceeds, you will receive NATS 'Slow Consumer' error. That's literally means that your worker can't handle the whole load. In case of NATS JetStream, you will no longer receive messages until some of delivered messages will be acked in any way.
TYPE:
|
pending_bytes_limit
|
The number of bytes, considered by NATS server as possible to be delivered to the client without been answered. In case of NATS Core, if that limit exceeds, you will receive NATS 'Slow Consumer' error. That's literally means that your worker can't handle the whole load. In case of NATS JetStream, you will no longer receive messages until some of delivered messages will be acked in any way.
TYPE:
|
max_msgs
|
Consuming messages limiter. Automatically disconnect if reached.
TYPE:
|
durable
|
Name of the durable consumer to which the the subscription should be bound.
TYPE:
|
config
|
Configuration of JetStream consumer to be subscribed with.
TYPE:
|
ordered_consumer
|
Enable ordered consumer mode.
TYPE:
|
idle_heartbeat
|
Enable Heartbeats for a consumer to detect failures.
TYPE:
|
flow_control
|
Enable Flow Control for a consumer.
TYPE:
|
deliver_policy
|
Deliver Policy to be used for subscription.
TYPE:
|
headers_only
|
Should be message delivered without payload, only headers and metadata.
TYPE:
|
pull_sub
|
NATS Pull consumer parameters container. Should be used with
TYPE:
|
kv_watch
|
KeyValue watch parameters container.
TYPE:
|
obj_watch
|
ObjectStore watch parameters container.
TYPE:
|
inbox_prefix
|
Prefix for generating unique inboxes, subjects with that prefix and NUID.
TYPE:
|
ack_first
|
Whether to
TYPE:
|
stream
|
Subscribe to NATS Stream with
TYPE:
|
dependencies
|
Dependencies list (
TYPE:
|
parser
|
Parser to map original nats-py Msg to FastStream one.
TYPE:
|
decoder
|
Function to decode FastStream msg bytes body to python objects.
TYPE:
|
middlewares
|
Subscriber middlewares to wrap incoming message processing.
TYPE:
|
max_workers
|
Number of workers to process messages concurrently.
TYPE:
|
no_ack
|
Whether to disable FastStream auto acknowledgement logic or not.
TYPE:
|
ack_policy
|
Whether to
TYPE:
|
no_reply
|
Whether to disable FastStream RPC and Reply To auto responses or not.
TYPE:
|
title
|
AsyncAPI subscriber object title.
TYPE:
|
description
|
AsyncAPI subscriber object description. Uses decorated docstring as default.
TYPE:
|
include_in_schema
|
Whetever to include operation in AsyncAPI schema or not.
TYPE:
|
persistent
|
Whether to make the subscriber persistent or not.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
LogicSubscriber[Any]
|
LogicSubscriber[Any]: The created subscriber object. |
Source code in faststream/nats/broker/registrator.py
640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 |
|
publisher
#
publisher(
subject,
*,
headers=None,
reply_to="",
stream=None,
timeout=None,
persistent=True,
middlewares=(),
title=None,
description=None,
schema=None,
include_in_schema=True,
)
Creates long-living and AsyncAPI-documented publisher object.
You can use it as a handler decorator (handler should be decorated by @broker.subscriber(...)
too) - @broker.publisher(...)
.
In such case publisher will publish your handler return value.
Or you can create a publisher object to call it lately - broker.publisher(...).publish(...)
.
PARAMETER | DESCRIPTION |
---|---|
subject
|
NATS subject to send message.
TYPE:
|
headers
|
Message headers to store metainformation.
content-type and correlation_id will be set automatically by framework anyway.
Can be overridden by
TYPE:
|
reply_to
|
NATS subject name to send response.
TYPE:
|
stream
|
This option validates that the target
TYPE:
|
timeout
|
Timeout to send message to NATS.
TYPE:
|
middlewares
|
Publisher middlewares to wrap outgoing messages.
TYPE:
|
title
|
AsyncAPI publisher object title.
TYPE:
|
description
|
AsyncAPI publisher object description.
TYPE:
|
schema
|
AsyncAPI publishing message type.
Should be any python-native object annotation or
TYPE:
|
include_in_schema
|
Whetever to include operation in AsyncAPI schema or not.
TYPE:
|
persistent
|
Whether to make the publisher persistent or not.
TYPE:
|