Not affiliated with the lua-nats package; this is an independent client built to the NATS ADR specs.
A NATS client written in plain Lua on top of LuaSocket. It runs on Lua 5.1, 5.2, 5.3, 5.4 and LuaJIT. It covers core NATS (pub/sub, queue groups, headers, request/reply, keepalive, reconnect, drain, TLS, WebSocket, nkey/JWT auth) plus JetStream (streams, pull/push/ordered consumers), Key-Value buckets, Object Store buckets and the Service API (micro).
Source: https://github.com/SamOrlando/nats-lua
The default runtime is blocking LuaSocket. One coroutine owns the socket.
Connection owner is where wait, step, bounded queues
and start() are described.
From LuaRocks:
luarocks install nats-lua # pulls luasocket and nuidFrom a clone:
git clone https://github.com/SamOrlando/nats-lua.git
cd nats-lua
luarocks make rockspecs/nats-lua-0.4.6-1.rockspecOr copy the nats*.lua files onto your package.path and install the
dependencies yourself (luarocks install luasocket nuid).
require("nats") is the core client. nats-jetstream, nats-kvs, nats-objs
and nats-micro each extend that module and return it.
nats.use("jetstream", "kvs", "objs", "micro") loads several at once (js is
an alias for jetstream). nats-nkeys and nats-sha256 are standalone.
An unknown name passed to nats.use raises.
Requirements:
luasocket(>= 3.0)nuid>= 1.0.0 (pure Lua, luarocks packagenuid), used for inboxes and sids- optional:
lua-cjsonfor faster JSON. Without it, a built-in pure Lua codec is used (nats.json.backendtells you which one is active). - optional:
luasecfor TLS. It is only loaded when TLS is needed. - optional:
luaossl(openssl.digest) for faster SHA-256 in the object store. Otherwise a pure Lua SHA-256 is used: native integers on 5.3+,biton LuaJIT,bit32on 5.2, lookup tables on 5.1. Rough speed per MB: LuaJIT 0.07 s, 5.4 0.17 s, 5.3 0.26 s, 5.2 1.1 s, 5.1 4.2 s.
local nats = require("nats")
local nc = nats.client({ servers = { "nats://127.0.0.1:4222" }, name = "demo" })
nc:connect()
-- callback runs inside wait()
local sid = nc:subscribe("greet.*", function(msg)
print(msg.subject, msg.data)
if msg.reply then msg:respond("hi " .. msg.data) end
end)
nc:publish("greet.joe", "joe")
nc:publish("greet.ann", "ann", nil, { ["X-Trace"] = "1" }) -- with headers (HPUB)
nc:wait({ maxcount = 2, timeout = 1 }) -- dispatch 2 messages
-- nil, "no responders" or nil, "timeout" on failure
local reply, err = nc:get("greet.bob", "bob")
print(reply and reply.data or err)
nc:unsubscribe(sid)
nc:drain() -- unsubscribe all, deliver in-flight messages, closenats.client returns a client and does not connect. nats.connect(opts) and
nats.connect(host, port) connect and return that client. The positional form
nats.client(nil, host, port) still works.
One coroutine owns the socket. Subscription callbacks, keepalive PINGs,
reconnects, tickers, JetStream heartbeats, KV and object watchers, and service
handlers run on that coroutine, inside wait, step, get, flush, drain,
or a JetStream call that waits for a reply. A program that connects, subscribes
and calls wait is unchanged. The same coroutine may call back in: a handler
may publish or get. Any other coroutine raises 'connection is in use',
unless start() is running a background owner. Then the call is queued and
runtime.park waits.
nc:wait() -- run forever, dispatching callbacks
nc:wait({ timeout = 5 }) -- stop after 5 seconds
nc:wait({ sid = sid, timeout = 2 }) -- until a message arrives on sid
nc:wait({ maxcount = 10, timeout = 2 }) -- until 10 messages were processedwait with sid or lid returns the matching message. If the callback returns
a value, wait returns that value instead of the message. With maxcount, the
result comes from the last message once that many were processed. A timeout
returns nil, "timeout". If the connection fails and reconnect is off, or
reconnect gives up, wait and step raise 'connection error: ...'.
step(timeout) is one maintenance pass and at most one read. wait, flush,
get and drain loop on it. Pass 0 to poll. Omit the timeout to block.
It returns the callback result, the message and the op, or nil, "timeout"
when nothing arrived. nc:add_ticker(interval, fn) runs on this same owner.
while true do
local res, msg, op = nc:step(0.1) -- nil, "timeout" when the slice is idle
endqueue_subscribe stores messages instead of calling a callback. The queue holds
at most max_chan_len messages (default 65536). A full queue drops the newest
message, increments stats.dropped, and calls error_cb with
'subscription queue full'. next on the owner calls step. While start()
is running, next from another coroutine waits on runtime.sleep and leaves
the read to the owner.
local sub = nc:queue_subscribe("orders.>")
local msg, err = sub:next(1) -- nil, "timeout" or nil, "closed"
sub:stop()start() runs that owner in the background. It needs runtime.spawn and
runtime.park. With the default runtime, start() raises
'runtime.spawn is required to start a background owner'. host below is the
embedder's scheduler. This repository does not include one.
local nc = nats.client({
servers = "nats://127.0.0.1:4222",
runtime = {
now = host.now,
sleep = host.yield, -- must yield, or other coroutines never run
current = host.current, -- default is coroutine.running()
read_file = host.read_file, -- creds, nkey files, contexts
spawn = host.spawn,
park = host.park, -- caller waits until the owner finishes the call
wakeup = host.wakeup, -- optional; stop() uses it
poll_interval = 0.05, -- read slice when no command is queued
},
})
nc:connect()
nc:start()
-- from another coroutine:
nc:publish("greet.joe", "joe") -- queued; park waits
nc:stop() -- the owner exits on its next iterationruntime.resume is optional. When it is set, the owner wakes the parked caller
after each command so the next call can queue before the owner reads. Hosts
that schedule coroutines themselves omit it. On Lua 5.1 a yield cannot cross
pcall, so drive step from the owner there.
nats.feed(buf) parses protocol bytes a host already has. It returns the
operations and the unparsed suffix. Socket methods and the yield rule are under
Connection owner.
local m, err = nc:get(subject, data, header, nil, timeout) -- synchronous
nc:request(subject, function(reply) ... end, data, header) -- callback runs on the owner
nc:poll(subject, function(reply) ... end, data, header, 2, 5)-- up to 5 replies, over 2 s, during wait()All requests share one wildcard subscription, client.inbox_muxed .. ".*".
That subject is _INBOX.<nuid>.*. The nuid is 22 characters: 12 of prefix and
10 of sequence. client.inbox_prefix is the bare prefix (default _INBOX).
The mux subscription id is "1". Ids from subscribe start at "2".
When nobody is listening, the server answers with a 503 (no_responders, on
by default) and get returns nil, "no responders".
request returns the published message. Its callback runs on the owner for the
first reply. poll returns immediately; replies arrive while the owner is in
step or wait. request_many blocks and gathers replies.
Pass headers to publish either as a table ({ Key = "v", Multi = { "a", "b" } })
or as an already-encoded "Key: v\r\n" string. On received messages,
msg.header is the raw header block without the NATS/1.0 line.
msg:headers() returns it decoded (repeated keys become arrays), and
msg:status() returns the status code and description (503, 404, 408, ...).
The raw fields are msg.status_code and msg.status_description.
local ec = nc:encoded_client() -- JSON by default (cjson or the built-in codec)
ec:publish("cfg.update", { level = 3 })
ec:subscribe("cfg.update", function(m) print(m.data.level) end)local nc = nats.client({
servers = "nats://a:4222,nats://b:4222", -- string or table; tls://, user:pass@ and token@ supported
reconnect = true, max_reconnect = 60, -- -1 = forever
reconnect_wait = 2, reconnect_wait_max = 30, reconnect_jitter = 0.1,
ping_interval = 120, max_pings_out = 2,
error_cb = function(nc, err) end, -- async -ERR, callback errors, disconnects
disconnected_cb = function(nc, reason) end,
reconnected_cb = function(nc) end,
closed_cb = function(nc) end,
lame_duck_cb = function(nc) end, -- INFO with ldm=true (ADR-5)
})
nc:status_string() -- "connected" | "reconnecting" | "closed" | "disconnected"
nc:buffered() -- bytes waiting in the reconnect buffer
nc:reconnect() -- on-demand reconnect (ADR-40, blocking)
nc:set_server_pool({ "nats://c:4222" })Reconnect attempts run on the connection owner, from wait, step and the
other calls that read. While the client is reconnecting, publishes are held in
the reconnect buffer (reconnect_buf_size, default 8 MiB). Keepalive PINGs run
from those same calls.
- user/password: set
user/pass, or put them in the URL (nats://u:p@host). - token: set
auth_token, or usenats://token@host. - TLS: TLS is used when the server requires it, or when you set
tls_required = true, use atls://URL, or settls_first = true.tls = {...}takes LuaSec parameters. The default is{ mode="client", protocol="any", verify="none" }, so setverify = "peer", cafile = ...for real verification. - nkey / creds:
creds = "/path/user.creds"(or the creds text),nkey_seed = "SU...", ornkey_file = "/path/seed.nk". The server nonce is signed with the bundled ed25519 (nats-nkeys, checked against RFC 8032 and nats.go vectors); luasodium / luatweetnacl are used for sign and verify when installed.signature_cbstill overrides.nats-nkeyscan also create key pairs, verify withfrom_publicorverify(raw 64-byte signatures), parse/format creds and issue user JWTs.decode_jwtdoes not check the signature. - Contexts:
nats.connect_context("name")reads~/.config/nats/context/name.json(nats CLI). - WebSocket:
ws://host:port/pathandwss://URLs (binary frames over LuaSocket, no permessage-deflate).ws_headersadds upgrade headers;ws_mask = trueuses random masks.
local nats = require("nats-jetstream") -- extends the client
local nc = nats.client(); nc:connect()
local NS = nats.jetstream.NS -- durations are nanoseconds
local stream = nc:stream({ name = "ORDERS", subjects = { "orders.>" }, max_age = 3600 * NS }, "create")
local ack = stream:publish("orders.new", "data", { id = "order-1" }) -- {stream=, seq=, duplicate=}
nc:js_publish("orders.new", "data")
local con = stream:consumer({ durable_name = "worker", ack_policy = "explicit" }, "create")
for _, m in ipairs(con:fetch(10, nil, 2)) do -- batch 10, expire after 2 s
print(m.data, m:metafields().stream_seq)
m:ack() -- also nak(delay_s), term(reason), in_progress(), ack_ack()
end
con:delete(); stream:delete()fetch blocks on the owner until the batch fills, the timeout expires, or the server ends the pull.
consume, ordered consumers and push subscriptions deliver while that owner
is inside wait or step.
local ctx = consumer:consume(function(m) m:ack() end, { max_messages = 100, expires = 30, idle_heartbeat = 5 })
ctx:stop() -- or ctx:drain()
local it = consumer:messages(); local m = it:next(5); it:stop()
local oc = stream:ordered_consumer({ filter_subjects = { "orders.>" } }) -- ADR-17
oc:fetch(10); oc:next(5) -- also oc:consume(cb), oc:messages()
local sub = stream:subscribe({ deliver_subject = nc:new_inbox(), flow_control = true, idle_heartbeat = 5 }, cb) -- push (ADR-9)
consumer:pause(os.time() + 60); consumer:resume(); consumer:unpin() -- ADR-42
consumer:fetch(10, nil, 5, nil, { group = "g1" }) -- priority groups
stream:direct_get({ multi_last = { "a.>" }, batch = 10 }) -- ADR-31
nc:js_publish(subj, data, { ttl = 60 }) ; nc:js_counter_add(subj, 5) ; nc:js_publish_batch(msgs)require("nats-kvs")
local kv = nc:kvs({ bucket = "config", history = 5, ttl = 3600 }) -- ttl in seconds
local rev, m = kv:put("app.level", "3") -- revision (integer), pub ack msg | nil, err
local e = kv:get("app.level") -- {value=, revision=, op=, created=} | nil, "key not found"
rev = kv:update("app.level", "4", e.revision) -- compare-and-set: revision, msg | nil, "wrong last sequence" if stale
kv:create("app.once", "x") -- revision, msg | nil, "key exists"
kv:delete("app.level"); kv:purge("app.once") -- true, msg | nil, err
local w = kv:watch("app.*", function(e) if e then print(e.key, e.value, e.op) end end,
{ include_history = false, ignore_deletes = false, meta_only = false, updates_only = false })
w:wait_initial(5) -- runs the owner until the nil "initial values done" marker
w:stop()
local keys = kv:keys() -- list of key names
local hist = kv:history("app.level") -- list of entries
kv:purge_deletes({ older_than = 1800 })
kv:create("tmp", "x", { ttl = 10 }) -- per key TTL (bucket created with limit_marker_ttl)
nc:create_key_value({ bucket = "copy", mirror = { name = "config" } }) -- mirrors / sources
kv:destroy()require("nats-objs")
local obs = nc:create_object_store({ bucket = "files", description = "d", ttl = 0,
storage = "file", replicas = 1, compression = true, metadata = { owner = "me" } })
-- also: nc:object_store(bucket) (bind), nc:objs(cfg) (create or update),
-- nc:update_object_store(cfg), nc:delete_object_store(bucket), nc:object_store_names()
local info = obs:put("a.txt", "hello") -- string
obs:put({ name = "big.bin", description = "x", headers = { ["X-A"] = { "1" } },
options = { max_chunk_size = 64 * 1024 } }, io.open("big.bin", "rb")) -- file handle
obs:put("gen", function() return next_piece() end) -- reader function (nil = EOF)
obs:put_file("/tmp/photo.jpg") -- name = base name
local data, info = obs:get("a.txt") -- whole object; size + SHA-256 verified
obs:get("big.bin", { callback = function(chunk) ... end }) -- streaming
obs:get_file("big.bin", "/tmp/out.bin")
obs:get_info("a.txt"); obs:get_info("a.txt", true) -- true = include deleted
obs:update_meta("a.txt", { name = "b.txt", description = "renamed" })
obs:delete("b.txt") -- deleted marker + chunk purge
obs:list(); obs:list({ show_deleted = true })
obs:add_link("alias", obs:get_info("big.bin")); obs:add_bucket_link("other", other_obs)
obs:watch(function(info) ... end, { timeout = 10 }) -- blocks on the owner until timeout, or until the callback returns false; nil ends the initial values
local w = obs:watcher(function(info) ... end) -- delivers while the owner is in step or wait; w:stop()
obs:status(); obs:seal(); obs:destroy()Chunks default to 128 KiB (capped below the server max_payload) and are
published with up to 32 acks outstanding. Reads use an ephemeral ack-none
pull consumer filtered on the chunk subject. Name encoding (padded base64url),
the SHA-256=<base64url> digest and the ObjectInfo JSON match nats.go: an
interop test (tests/interop/) checks that Go reads Lua objects and links and
that Lua reads Go objects and links.
Handlers run on the connection owner, inside wait or step.
require("nats-micro")
local svc = nc:add_service({ name = "calc", version = "1.0.0", description = "d" })
svc:add_group("math"):add_endpoint("add", function(req) req:respond_json({ sum = 3 }) end)
nats.micro.ping(nc, "calc"); nats.micro.info(nc, "calc"); nats.micro.stats(nc, "calc")
svc:stop()Full reference for every public function, object and constant: docs/reference/ (nats, jetstream, kvs, objs, micro, nkeys, sha256).
- With the default runtime, push flow control, heartbeats, watchers and service
endpoints run while the owner is inside
steporwait.start()needs a hostruntime.spawn. See Connection owner. - ADR-50 fast-ingest batch publishing, ADR-51 scheduler helpers (headers only), ADR-63 backup/restore, codecs and tracing are not implemented.
- WebSocket has no permessage-deflate compression.
- TLS defaults to
verify = "none". Settlsfor certificate checks.
The full per-ADR list is in docs/ADR-COMPLIANCE.md.
| Area | ADR | Status |
|---|---|---|
INFO → CONNECT → PING/PONG handshake, async INFO, discovery (+ discovered_servers_cb) |
ADR-40 | ✅ |
| Lame duck mode notification | ADR-5 | ✅ callback + client.lame_duck (no automatic migration) |
| Server pool, randomization, DNS resolve-all + shuffle | ADR-40/11 | ✅ |
| Reconnect with backoff, jitter and max attempts, on the connection owner | ADR-40 | ✅ |
| Reconnect buffer (publishes while disconnected) | ADR-40 | ✅ reconnect_buf_size |
| Resubscribe, callbacks (disconnected/reconnected/closed/error/lame duck) | ADR-40 | ✅ |
| Reconnect-to-specific-server handler | ADR-40 | ✅ reconnect_delay_cb + set_server_pool (no per-attempt server picker) |
Headers, status line, header/status constants (nats.headers, nats.status) |
ADR-4 | ✅ |
| No responders (503) | ADR-4/40 | ✅ |
| Subject / name validation | ADR-6 | ✅ |
TLS, tls_first |
ADR-40 | ✅ via LuaSec |
| nkey / JWT creds auth with bundled ed25519 | ADR-14 | ✅ |
| Contexts (nats CLI context files) | ADR-21 | ✅ |
| Request many (max messages, max wait, stall, sentinel) | ADR-47 | ✅ request_many |
| WebSocket transport | — | ✅ ws:// wss:// (no compression) |
| JetStream API, errors, paging, API prefix/domain | ADR-1/7/19 | ✅ |
| Streams, msg get/delete, direct get, batch / multi-last direct get | ADR-1/10/31 | ✅ |
| JetStream publish with expectations, per message TTL | ADR-22/43 | ✅ |
| Pull consumers fetch/next, status 100/404/408/409/423 | ADR-13/9 | ✅ |
| Consume / messages (simplified API), heartbeats, re-pull thresholds | ADR-37 | ✅ |
| Ordered consumer (pull and push) | ADR-17 | ✅ |
| Push consumers with flow control and idle heartbeats | ADR-9 | ✅ (owner must step or wait) |
| Consumer pause / resume, priority groups (pinned, overflow), unpin | ADR-42 | ✅ |
Counters (Nats-Incr) |
ADR-49 | ✅ |
| Atomic batch publish | ADR-50 | ✅ (fast-ingest batches ❌) |
| Message scheduling | ADR-51 | ◐ header passthrough (schedule, schedule_target) |
| KV full API: watch (history, meta only, ignore deletes, updates only, resume), keys, history, purge_deletes, create/update with revision | ADR-8 | ✅ |
| KV limit markers / per key TTL, mirrors and sources | ADR-48/8 | ✅ |
| Object store (create/bind/put/get/info/meta/delete/list/links/seal/status), watcher | ADR-20 | ✅ wire compatible with nats.go |
| Service API (micro) with PING/INFO/STATS, groups, errors | ADR-32 | ✅ |
| Backup/restore, codecs, tracing | ADR-63/54/41 | ❌ |
nats.client(opts) accepts these options. The same names can also be set
globally in nats.defaults.
| Option | Default | Notes |
|---|---|---|
servers / url |
– | string (comma separated) or table of URLs |
host, port |
127.0.0.1, 4222 | used if no servers |
path, unix |
– | UNIX domain socket (LuaSocket built with unix support) |
name, user, pass, auth_token, jwt, nkey, sig, creds, signature_cb |
– | auth / CONNECT |
echo |
true |
receive own publishes |
headers, no_responders |
true |
|
verbose, pedantic |
false |
CONNECT flags |
protocol |
1 | CONNECT protocol |
timeout |
5 | seconds; request timeout and socket write timeout |
tcp_nodelay |
true | |
connect_timeout |
timeout |
|
ping_interval, max_pings_out |
120, 2 | keepalive while the owner is in step or wait |
reconnect, max_reconnect, reconnect_wait, reconnect_wait_max, reconnect_jitter, reconnect_jitter_tls, reconnect_delay_cb |
false, 60, 2, 30, 0.1, 1, – | |
no_randomize, ignore_discovered_servers, retry_on_failed_connect |
false | |
tls_required, tls_first, tls |
false, false, – | |
validate_subjects |
true | |
inbox_prefix |
_INBOX |
|
drain_timeout |
30 | |
error_cb, disconnected_cb, reconnected_cb, closed_cb, lame_duck_cb, discovered_servers_cb |
– | |
reconnect_buf_size |
8 MiB | <= 0 disables buffering (blocking reconnect as in 0.2) |
max_chan_len |
65536 | message count for queue_subscribe; a full queue drops the newest |
runtime |
LuaSocket | now, sleep, current, read_file. Optional: spawn, park, resume, wakeup, poll_interval (0.05) |
nkey_seed, nkey_file |
– | nkey auth with the bundled signer |
resolve_all |
true | try every resolved address, shuffled (ADR-11) |
ws_mask, ws_headers |
false, – | WebSocket options |
The test scripts need nuid on LUA_PATH: luarocks install nuid, or a clone
of lua-nuid passed as NUID_DIR.
tests/test_runtime.lua needs no server. It covers the parser, ownership,
bounded queues and start(). tests/test_nats.lua talks to a NATS server on
127.0.0.1:4222 and starts a short-lived nats-server on 127.0.0.1 for
authentication and lame duck. tests/test_functions.lua starts its own
single-node JetStream nats-server on 127.0.0.1:14422 (monitor 18422,
WebSocket 18480, TLS 14423) and exercises the client against that server.
Integration tests run when NATS_URL is set (default nats://127.0.0.1:4222).
test_local.lua starts its own nats-server on 127.0.0.1.
NUID_DIR=/path/to/lua-nuid tests/run.sh # tests + docs check on every Lua found
(cd tests/interop && go build -o interop .) && NATS_URL=... lua tests/interop/objs_interop.lua # nats.go interop
NATS_URL=nats://127.0.0.1:4222 NUID_DIR=... tests/run.sh # + integration tests
NATS_NO_CJSON=1 NATS_URL=... lua tests/test_integration.lua # with the pure Lua JSON codec
NATS_SERVER_BIN=/path/nats-server lua tests/test_local.lua # nkey, creds, websocket, restart buffering
lua tests/test_nkeys.lua # SHA-512, ed25519 sign/verify, nkeys vectors
lua tests/bench_nkeys.lua [path/to/nkeys.so] # optional compare with lua-nkeys
LUA_PATH="./?.lua;$NUID_DIR/?.lua;;" lua tests/check_docs.lua # every exported function is in docs/reference
lua tests/bench.lua 200000 # parser/publish benchmarktests/test_nats.lua uses subjects under nats-lua.unit.<random>.
tests/test_functions.lua uses fn.<random> and deletes the streams and
buckets it creates. Integration tests use nats-lua.test.<random>.> and
delete the streams and buckets they create. tests/fakesock.lua feeds the
benchmarks.
nats._VERSIONand the CONNECTversionfield are0.4.6.- One coroutine owns the socket.
client:step(timeout)is one maintenance pass and at most one read.wait,flush,get, anddrainloop on it. client.runtime(now,sleep,current,read_file) is the clock, the wait, and file reads for creds, nkeys, and contexts. LuaSocket loads when the default runtime is first used.nats.now,nats.sleep, andnats.feedare public.nats.feed(buf)parses protocol bytes with no socket.- A second coroutine raises
'connection is in use'.client:start()andclient:stop()run a background owner when the host setsruntime.spawnandruntime.park. client:queue_subscribebounds the inbound queue atmax_chan_lenmessages (default 65536). A full queue drops the newest message and incrementsstats.dropped.- A failed write inside a subscription callback is a connection error, reported
through
error_cb. It is not labeled'subscription callback error'. Once the callback returns, a later read on that owner raises if reconnect is off or has given up. tests/test_runtime.luacovers the parser, ownership, bounded queues, andstart()without a NATS server.
client.inbox_sidis"1". User subscription ids are short numeric strings and start at"2".
- Inbox handling repaired: the muxed request inbox is
<inbox_prefix>.<22-char nuid>, letter ids are an 8-character sequence, andclient:create_inbox()returns<inbox_muxed>.<lid>, lid.client:new_inbox()is a standalone<prefix>.<token>subject (honoursinbox_prefix) and remains the inbox helper used by JetStream. nats.use(...)loads several optional layers in one call (jetstream/js,kvs,objs,micro).nats._VERSIONand the CONNECTversionfield are0.4.4.tests/test_functions.luaproves the client against a local nats-server.
nats._VERSIONis now'nats-lua 0.4.2'(was'lua-nats-nx 0.4.1'); the project is named nats-lua everywhere. The CONNECTversionfield is0.4.2.- No API changes.
- First release on LuaRocks (
luarocks install nats-lua); documentation and URLs updated (GitHub). No API changes from 0.4.0.
KV writes follow ADR-8 / nats.go:
| Call | 0.3.x | 0.4.0 |
|---|---|---|
kv:put(k, v) |
reply msg (msg.revision) |
revision, msg |
kv:create(k, v [, opts]) |
reply msg | revision, msg / nil, "key exists" |
kv:update(k, v, last) |
reply msg; last = nil acted like put |
revision, msg; last required (nil, "revision required"), stale → nil, "wrong last sequence", msg, api_error |
kv:delete(k [, opts]), kv:purge(k [, opts]) |
reply msg | true, msg |
Errors are returned (nil, err), never raised. On Lua 5.3+ numbers without a
fraction decoded from JSON (sequences, revisions, counts, pub acks, stream and
consumer info) are integers with lua-cjson too (before: floats such as 1.0);
nats.int(v) is the helper.
Migration:
-- 0.3.x
local m = kv:put("k", "v"); local rev = m.revision
kv:update("k", "v2") -- silently a put
-- 0.4.0
local rev, m = kv:put("k", "v") -- m.revision / m.ack still available
kv:put("k", "v2") -- unconditional write
local e = kv:get("k")
local r, err = kv:update("k", "v3", e.revision)
if not r and err == "wrong last sequence" then --[[ re-read and retry ]] endstream:nextmsg(subject [, seq])returns the first message onsubjectwith sequence>= seq(default 1), ADR-31next_by_subj;nextmsg(seq, subject)is accepted too. Before it returned the last message (likelastmsg).client:new_inbox()honours theinbox_prefixoption; JetStream pull, consume, ordered and push consumer inboxes use it (request/mux, KV, object store and micro replies already did).- nkeys xkeys:
create_pair('curve')makes x25519 keys (X.../SX...) withseal/openin the Go nkeysxkv1format (x25519 + XSalsa20-Poly1305, pure Lua), Go interop tested (tests/interop/xkeys_interop.sh). New primitivesnkeys.x25519,box_seal,box_open,poly1305, ... consumer:stop()/consumer:drain()implemented (stop/drain every consume, messages and push context of the consumer); realctx:drain,iter:drain,ordered:stop/drain,ordered_ctx:drain,push_sub:drain.- KV get, object store info and
counter_getwork on streams withoutallow_direct(newstream:get_stored). tests/run.shexits non-zero when any suite fails.
Breaking: nextmsg semantics (see above); new_inbox() subjects change when a
custom inbox_prefix is set; create_pair('curve') keys are x25519 (old
ed25519-with-X-prefix seeds give different public keys); iter:drain() is no
longer an alias of stop().
New modules nats-nkeys and nats-micro. Breaking changes:
kv:get()on a deleted or purged key now returnsnil, "key not found", entry(before: the entry withop = "DEL"/"PURGE").kv:watch()/kv:watch_all()now return a Watcher object and call the callback with decoded entries plus anilmarker after the initial values. Pass{ raw = true }to get the 0.2 behaviour (sid + raw messages, live only).consumer:consume()/messages()were stubs and are now the ADR-37 API.list_streams()/consumer_list()return all pages aggregated.- Publishes while reconnecting are buffered instead of blocking the caller in
a reconnect loop. Set
reconnect_buf_size = 0for the old behaviour. - Creds files whose JWT contains
-were cut at the first-; this is fixed.
- New object store (
nats-objs) andnats-sha256modules. Before,require("nats-objs")returned a stub table; it now returns thenatsmodule, likenats-kvsdoes.
echonow defaults totrue(the NATS default). Before, a connection never saw its own publishes. Setecho = falseto get the old behaviour.wait({maxcount = n})now processes exactlynmessages. Before, it processedn - 1.consumer:fetch()/next()no longer JSON-decode message payloads. Use an encoded client or decode them yourself.- Subscription ids are now short numeric strings instead of 22-character NUIDs (they are still opaque strings).
- Invalid subjects and payloads larger than
max_payloadraise an error before anything is sent. Before, the server disconnected the client. msg.headeron received messages no longer ends with an extra blank line.- Assigning unknown fields on client and msg objects now stores them. Before, a debug line was printed and the value was dropped.
- JetStream:
stream:publish(subject, data, opts)now returns the pub ack table (the old version did not work). The KVttloption is in seconds.
MIT, Copyright (c) 2023-2026 Sam Orlando. See LICENSE.