Examples
The examples are intentionally short. They show the API the gem is expected to support without turning each file into a test harness.
Core lifecycle
Section titled “Core lifecycle”Source: examples/core_lifecycle.rb
bundle exec ruby examples/core_lifecycle.rbShows NatsAsync::Client, explicit start(task:), a subscription callback, one publish,
and close in ensure.
Queue group
Section titled “Queue group”Source: examples/queue_group.rb
bundle exec ruby examples/queue_group.rbShows subscribe(subject, queue:) and drain(timeout:) for a worker-style subscriber.
Request/reply
Section titled “Request/reply”Source: examples/request_reply.rb
bundle exec ruby examples/request_reply.rbShows a service publishing to message.reply, async request callbacks, and a
message-oriented request promise with wait.data.
Headers and binary payloads
Section titled “Headers and binary payloads”Source: examples/headers_and_binary.rb
bundle exec ruby examples/headers_and_binary.rbShows NATS headers through HPUB / HMSG and byte-preserving payload handling.
JetStream management
Section titled “JetStream management”Source: examples/jetstream_management.rb
bundle exec ruby examples/jetstream_management.rbShows add_stream?, stream_exists?, add_consumer?, and consumer_info.
JetStream publish
Section titled “JetStream publish”Source: examples/jetstream_publish.rb
bundle exec ruby examples/jetstream_publish.rbShows js.publish returning a publish acknowledgement with stream and seq.
JetStream pull consumer
Section titled “JetStream pull consumer”Source: examples/jetstream_pull_consumer.rb
bundle exec ruby examples/jetstream_pull_consumer.rbShows durable pull subscription setup, fetch(batch:, timeout:), and message ack/nak.
Backend detection
Section titled “Backend detection”Source: examples/backend_detection.rb
bundle exec ruby examples/backend_detection.rbShows client.resolve_backend(mode: :auto, stream:) selecting :jetstream when the
stream exists and falling back to :core otherwise.
Full JetStream round trip
Section titled “Full JetStream round trip”Source: examples/jetstream_roundtrip.rb
bundle exec ruby examples/jetstream_roundtrip.rbShows stream creation, durable pull subscription, publish ack, fetch, message metadata, ack, and cleanup.