Pull Subscriber#
Overview#
NATS JetStream supports two different ways to consume messages: Push and Pull consumers.
The Push consumer is used by default to consume messages with FastStream. It means that the NATS server delivers messages to your consumer as fast as possible by itself. However, it also means that NATS should control all current consumer connections and increase server load.
Thus, the Pull consumer is the recommended way to consume JetStream messages by the NATS TEAM. Using it, you simply ask NATS for new messages at some interval. It may sound a little less convenient than automatic message delivery, but it provides several advantages, such as:
- Consumer scaling without a queue group
- Handling messages in batches
- Reducing NATS server load
So, if you want to consume a large flow of messages without strict time limitations, the Pull consumer is the right choice for you.
FastStream Details#
The Pull consumer is just a regular Stream consumer, but with the pull_sub argument, which controls consuming messages with batch size and block interval.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | |
The batch size doesn't mean that your msg argument is a list of messages, but it means that you consume up to 10 messages for one request to NATS and call your handler for each message concurrently in an anyio task group.
Tip
If you want to consume a list of messages, just set the batch=True in PullSub class.
Batch Pull Subscriber Example#
This example demonstrates how to use batch=True with a Pull subscriber. The message bodies are passed as a list into bodies, while the native NATS messages are available via msg.raw_message (type NatsBatchMessage), allowing per‑message ack() inside a batch.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 | |
So, your subject will be processed much faster, without blocking for each message processing.
With batch=True, FastStream collects up to batch_size messages before calling the handler. It waits until the batch is full or timeout expires (5 seconds by default), then delivers any collected messages as a partial batch. Set timeout=None to wait until the batch is full.
Note
nats-py can return a partial batch before its timeout expires. FastStream makes additional fetch requests to fill the batch; see nats-py issue #1034.