Client API
NatsAsync::Client
Section titled “NatsAsync::Client”Constructor:
NatsAsync::Client.new( url: "nats://127.0.0.1:4222", verbose: true, js_api_prefix: "$JS.API", ping_interval: 30, ping_timeout: 5, # Flush batching options flush_delay: 0.01, # Max time to buffer before flush (10ms default) flush_max_buffer: 5000, # Safety limit: flush if buffer exceeds this tls: nil, tls_verify: true, tls_ca_file: nil, tls_ca_path: nil, tls_hostname: nil, tls_handshake_first: false, user: nil, password: nil, nkey_seed: nil, nkey_seed_file: nil, nkey_public_key: nil, reconnect: false, reconnect_interval: 1, max_reconnect_attempts: nil)Lifecycle:
start(task:)flush(timeout: 2)drain(timeout: 5)closestopconnected?closed?last_errorstatus
Core messaging:
publish(subject, payload = "", reply: nil, headers: nil)subscribe(subject, queue: nil, handler: nil, &block)unsubscribe(sid)request(subject, payload = "", timeout: 0.5, headers: nil, &block)
request returns NatsAsync::Client::RequestPromise immediately. If a block is given,
the block is called asynchronously when the response arrives. The implementation is
event-driven through the existing read loop, one shared request timeout loop, and one
shared callback task. The promise resolves to the reply NatsAsync::Message.
JetStream:
jetstreamjs_api_subject(*tokens)resolve_backend(mode: :auto, stream: nil)
Operational fields:
server_infojs_api_prefixsent_pingsreceived_pongsreceived_pingsstatus
Connection statuses:
:closed:connecting:connected:reconnecting:disconnected
NatsAsync::Client::RequestPromise
Section titled “NatsAsync::Client::RequestPromise”Returned from client.request.
wait returns the reply NatsAsync::Message when fulfilled and raises the stored error when
rejected. value returns the fulfilled message, returns nil while pending, and raises the
stored error when rejected.
Methods:
idstatuspending?fulfilled?rejected?wait(timeout: nil)valueerror
Statuses:
:pending:fulfilled:rejected
NatsAsync::Message
Section titled “NatsAsync::Message”Fields:
subjectsidreplydataheadersheaderstatusdescription
headers contains actual NATS header fields. Protocol status lines from header-capable
control replies are exposed separately as status and description.
Ack methods:
ackack_sync(timeout: 0.5)nak(delay: nil, timeout: nil)term(timeout: nil)in_progress(timeout: nil)ackable?acked?metadata
Errors
Section titled “Errors”NatsAsync::ErrorNatsAsync::ConnectionErrorNatsAsync::AckErrorNatsAsync::MsgAlreadyAckedNatsAsync::NotAckableNatsAsync::RequestErrorNatsAsync::ProtocolErrorTimeout::Error