Getting Started
nats-async is a Ruby gem for using NATS from Async tasks. It focuses on a small
client API for core NATS messaging and JetStream workflows.
Install the gem
Section titled “Install the gem”gem install nats-asyncOr add it to your bundle:
gem "nats-async"Start a client
Section titled “Start a client”require "nats-async"
Async do |task| client = NatsAsync::Client.new(url: "nats://127.0.0.1:4222") client.start(task: task)
client.subscribe("demo.subject") do |message| puts "received: #{message.data}" end
client.publish("demo.subject", "hello") client.flushensure client&.closeendThe client is long-running. start(task:) opens the TCP connection, starts the read loop,
performs the first ping round trip, and starts the periodic idle ping loop.
Run local examples
Section titled “Run local examples”The repository includes a bundled bin/nats-server used by the integration specs. With
your own local NATS server, run:
bundle exec ruby examples/core_lifecycle.rbbundle exec ruby examples/request_reply.rbbundle exec ruby examples/jetstream_roundtrip.rbSet NATS_URL when the server is not on nats://127.0.0.1:4222.
Development commands
Section titled “Development commands”bundle installbundle exec rspecBuild the documentation site:
cd docs-sitenpm installnpm run build