NAME
EV::Nats::JetStream - JetStream API client for EV::Nats
SYNOPSIS
use EV;
use EV::Nats;
use EV::Nats::JetStream;
my $nats = EV::Nats->new(host => '127.0.0.1');
my $js = EV::Nats::JetStream->new(nats => $nats);
$js->stream_create({ name => 'ORDERS', subjects => ['orders.>'] },
sub {
my ($info, $err) = @_;
die $err if $err;
$js->js_publish('orders.new', '{"item":"widget"}', sub {
my ($ack, $err) = @_;
print "stored at seq=$ack->{seq}\n";
});
});
EV::run;
DESCRIPTION
Thin async wrapper over the JetStream $JS.API.* request/reply endpoints. Each method is a single request whose callback is invoked with the decoded JSON response (or an error string). The $nats connection passed to "new" handles all the actual I/O.
EV::Nats::KV and EV::Nats::ObjectStore build on top of this module -- see those for higher-level KV / blob APIs.
METHODS
All methods are async. Callbacks fire on the EV loop.
new
new(%opts)
my $js = EV::Nats::JetStream->new(
nats => $nats,
prefix => '$JS.API', # default API subject prefix
timeout => 5000, # ms; default 5000
with_status => 0, # see below
);
With with_status, callbacks receive a third argument when the server reported a status: a JetStream API error's object, { code => 404, err_code => 10014, description => 'consumer not found' }, or the status that ended a "fetch".
Stream management
stream_create
stream_create($config, $cb)
Create a stream. $config is passed verbatim as the StreamConfig request body. Callback: ($info, $err).
stream_update
stream_update($config, $cb)
Update an existing stream. Same shape as stream_create.
stream_delete
stream_delete($name, $cb)
Delete the stream by name.
stream_info
stream_info($name, [\%opts], $cb)
Fetch stream config + state. Optional \%opts may include subjects_filter (e.g. >) to populate state.subjects; without it the server omits that field for performance.
stream_list
stream_list($cb)
List all streams' info.
stream_purge
stream_purge($name, $cb)
Purge all messages from the stream.
stream_msg_get
stream_msg_get($stream, \%opts, $cb)
Fetch a single message from $stream. \%opts selects the message:
{ seq => $n } # by sequence number
{ last_by_subj => $subject } # latest matching subject
{ next_by_subj => $subject, seq => $start } # next at-or-after $start
The message body and headers in the response are base64-encoded under $resp->{message}{data} and $resp->{message}{hdrs}.
Consumer management
consumer_create
consumer_create($stream, $config, $cb)
Create a consumer (push or pull). $config is the consumer config hashref; durable_name makes it durable, ack_policy controls ack semantics.
consumer_delete
consumer_delete($stream, $consumer, $cb)
consumer_info
consumer_info($stream, $consumer, $cb)
consumer_list
consumer_list($stream, $cb)
Publishing and fetching
js_publish
js_publish($subject, $payload, $cb)
Publish with JetStream acknowledgment. Callback: ($ack, $err) where $ack is { stream, seq, duplicate }.
js_publish_h
js_publish_h($subject, $headers, $payload, $cb)
Like "js_publish" but sends $headers (a full NATS/1.0 header block, e.g. "NATS/1.0\r\nNats-Rollup: sub\r\n\r\n") with the message, waiting for the PubAck on a one-shot reply inbox. Used for rollup publishes where the acknowledged sequence still matters (EV::Nats::ObjectStore metadata). Callback: ($ack, $err).
fetch
my $fetch = fetch($stream, $consumer, \%opts, $cb)
Pull messages from a pull-mode consumer. Options:
batch-
Maximum number of messages to fetch (default 1).
expires-
Server-side wait time in nanoseconds (default 5_000_000_000 = 5s). Ignored with
no_wait. no_wait-
If true, the server answers at once with what is available; a fetch nobody answers ends after
timeout.
Callback: (\@messages, $err). Each message is a hashref:
{
subject => 'orders.new',
payload => '...',
reply => '$JS.ACK....', # for ack/nak/in_progress/term
headers => "...", # raw NATS/1.0 header block, or undef
}
A 404 (no messages) or 408 (expired) status ends the batch; any other status fails the fetch ("fetch failed: NATS/1.0 409 Server Shutdown") and -NAKs the messages received so far. A dropped connection fails it with "disconnected". With with_status the ending status is passed as { code => 409, description => 'Server Shutdown' }. An unknown stream or consumer may get no answer at all: the fetch then ends with an empty batch.
Returns a handle: $fetch->cancel ends the fetch without calling $cb; messages that still arrive for the pull are -NAKed.
ack
ack($msg)
nak
nak($msg, [$delay_ns])
Redeliver, after $delay_ns nanoseconds if given.
in_progress
in_progress($msg)
Extend the message's ack_wait.
term
term($msg, [$reason])
Never redeliver the message.
These take a fetched message hashref or its reply subject, and return true once the acknowledgement is sent or queued, or false when it cannot be ($@ holds why). They die on a message without a reply subject.
INTERNAL
These are exposed for sibling modules (EV::Nats::KV, EV::Nats::ObjectStore) -- not part of the end-user API and subject to change.
decode_json_or_error
decode_json_or_error($json)
Decode $json. Returns ($decoded, $error_or_undef); the error string already includes a "JSON decode error: " prefix when set. Gates on $@ so falsy-but-valid JSON (null, 0, false, empty string) is reported as a clean decode rather than a failure.
msg_is_tombstone
msg_is_tombstone($msg)
True if a STREAM.MSG.GET response message carries a KV-Operation: DEL or KV-Operation: PURGE header. $msg is the decoded message hash from the response. Used by EV::Nats::KV and EV::Nats::ObjectStore to surface deleted/purged entries as clean misses rather than as malformed payloads.
SEE ALSO
EV::Nats, EV::Nats::KV, EV::Nats::ObjectStore, JetStream API reference.