NATS::Connection
Inherits Reference / Object
Constructors
new(host, port, user : String | Nil = nil, pass : String | Nil = nil, name : String | Nil = nil, echo : Bool = true, pedantic : Bool = false)
Creates a new connection to a NATS Server.
nc = NATS::Connection.new("demo.nats.io")
nc = NATS::Connection.new("tls://demo.nats.io")
nc = NATS::Connection.new("nats://#{user}:#{pass}@127.0.0.1:4222")
nc = NATS::Connection.new(4222, name: "Sample App", user: "derek", pass: "s3cr3t")
nc = NATS::Connection.new(4222)
nc = NATS::Connection.new
Instance methods
close
Close a connection to the NATS server.
nc = NATS::Connection.new("demo.nats.io")
nc.close
closed?
Sourceflush(timeout = 2.second)
Flush will flush the connection to the server. Can specify a timeout.
max_payload
Sourcenew_inbox
Sourceon_close
Setup a callback for when the connection closes.
nc = NATS::Connection.new("demo.nats.io")
nc.on_close { puts "Connection closed!" }
nc.close
on_error
Setup a callback for an async errors that are received.
nc = NATS::Connection.new("demo.nats.io")
nc.on_error { |e| puts "Received an error #{e}" }
publish(subject : String, msg)
Publishes a messages to a given subject.
nc = NATS::Connection.new("demo.nats.io")
nc.publish("foo", "Hello!")
publish(subject : String)
Publishes an empty message to a given subject.
nc = NATS::Connection.new("demo.nats.io")
nc.publish("foo")
publish_with_reply(subject, reply : String, msg = nil)
Publishes a messages to a given subject with a reply subject.
nc = NATS::Connection.new("demo.nats.io")
nc.publish_with_reply("foo", "reply", "Hello!")
request(subject : String, msg?, timeout = 2.second)
Request will send a request to the given subject and wait up to timeout for a response.
nc = NATS::Connection.new("demo.nats.io")
answer = nc.request("req", "Help!")
puts "Received a response '#{answer}'!"
subscribe(subject, queue : String, &callback : Msg -> )
Subscribe to a given subject with the queue group. Will yield to the callback provided with the message received.
nc = NATS::Connection.new("demo.nats.io")
nc.subscribe("foo", "group1") { |msg| puts "Received '#{msg}'" }
subscribe(subject : String, queue : String | Nil, &callback : Msg -> )
Sourcesubscribe(subject : String, &callback : Msg -> )
Subscribe to a given subject. Will yield to the callback provided with the message received.
nc = NATS::Connection.new("demo.nats.io")
nc.subscribe("foo") { |msg| puts "Received '#{msg}'" }