NATS JetStream Extensions
JetStream Extensions is a set of utilities providing additional features to jetstream package in nats-py client.
Installation
uv add jetstreamext
Utilities
get_batch and get_last_msgs_for
get_batch and get_last_msgs_for are utilities that allow you to fetch multiple messages from a JetStream stream.
Responses are returned as async iterators, which you can iterate over using async for to receive messages.
get_batch
get_batch fetches a batch of messages from a provided stream, starting from
either the lowest matching sequence, from the provided sequence, or from the
given time. It can be configured to fetch messages from matching subject (which
may contain wildcards) and up to a maximum byte limit.
Examples:
see examples.py for a runnable version of all snippets below.
- fetching 10 messages from the beginning of the stream:
import nats
import jetstreamext
nc = await nats.connect()
js = nc.jetstream()
async for msg in jetstreamext.get_batch(js, "stream", batch=10):
print(msg.data)
- fetching 10 messages from the stream starting from sequence 100 and matching subject:
import nats
import jetstreamext
nc = await nats.connect()
js = nc.jetstream()
async for msg in jetstreamext.get_batch(js, "stream", batch=10, seq=100, subject="foo"):
print(msg.data)
- fetching 10 messages from the stream starting from time 1 hour ago:
from datetime import datetime, timedelta, timezone
import nats
import jetstreamext
nc = await nats.connect()
js = nc.jetstream()
async for msg in jetstreamext.get_batch(
js,
"stream",
batch=10,
start_time=datetime.now(timezone.utc) - timedelta(hours=1)
):
print(msg.data)
- fetching 10 messages or up to provided byte limit:
import nats
import jetstreamext
nc = await nats.connect()
js = nc.jetstream()
async for msg in jetstreamext.get_batch(js, "stream", batch=10, max_bytes=1024):
print(msg.data)
get_last_msgs_for
get_last_msgs_for fetches the last messages for the specified subjects from the specified stream. It can be optionally configured to fetch messages up to the provided sequence (or time), rather than the latest messages available. It can also be configured to fetch messages up to a provided batch size.
The provided subjects may contain wildcards, however it is important to note that the NATS server will match a maximum of 1024 subjects.
Responses are returned as async iterators, which you can iterate over using async for to receive messages.
Examples:
- fetching last messages from the stream for the provided subjects:
import nats
import jetstreamext
nc = await nats.connect()
js = nc.jetstream()
async for msg in jetstreamext.get_last_msgs_for(js, "stream", ["foo", "bar"]):
print(msg.data)
- fetching last messages from the stream for the provided subjects up to stream sequence 100:
import nats
import jetstreamext
nc = await nats.connect()
js = nc.jetstream()
async for msg in jetstreamext.get_last_msgs_for(js, "stream", ["foo", "bar"], up_to_seq=100):
print(msg.data)
- fetching last messages from the stream for the provided subjects up to time 1 hour ago:
from datetime import datetime, timedelta, timezone
import nats
import jetstreamext
nc = await nats.connect()
js = nc.jetstream()
async for msg in jetstreamext.get_last_msgs_for(
js,
"stream",
["foo", "bar"],
up_to_time=datetime.now(timezone.utc) - timedelta(hours=1)
):
print(msg.data)
- fetching last messages from the stream for the provided subjects up to a batch size of 10:
import nats
import jetstreamext
nc = await nats.connect()
js = nc.jetstream()
async for msg in jetstreamext.get_last_msgs_for(js, "stream", ["foo.*"], batch=10):
print(msg.data)
Release files for jetstreamext 0.4.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| jetstreamext-0.4.0.tar.gz | 5.9 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| jetstreamext-0.4.0-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 13.2 kB
Release files / jetstreamext-0.4.0.tar.gz
| Download URL | jetstreamext-0.4.0.tar.gz |
|---|---|
| Size | 5.9 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
3b49ed20e626258731a0f32ce15657ff7d1dde3c146e0048cddee0c4219cf492
|
|
BLAKE2b-256 checksum How to use checksums |
bd18c04b0739b513fb578a7d7867812b080a8c1dba39e84773b7734af9f5ec29
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
uv/0.8.15
|
Release files / jetstreamext-0.4.0-py3-none-any.whl
| Download URL | jetstreamext-0.4.0-py3-none-any.whl |
|---|---|
| Size | 7.4 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
5a6fae5fd86e7d10d5d65d45eda190f437e6ba60b8ec7931d4fa197aac928dca
|
|
BLAKE2b-256 checksum How to use checksums |
a4d75ceaea36d15c6c1d904776d316767305e36e4993199737b2cf87698d9ee2
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
uv/0.8.15
|